• Supporting different versions
of SQL is a daily challenge for
a DBA
• One of our Microsoft SQL
Server consultants discov...
I start writing this on my new SQL
Server 2012 box and quickly decide
to use the Dynamic Management
Views (DMV’s)
-- using DMV os Performance stats
SELECT instance_name
,cntr_value 'Log File(s) Used Size (KB)'
FROM sys.dm_os_performance...
• This runs well on my SQL Server
2012, 2008r2 and SQL 2005 servers
• SQL Server 2000 server simply
comes back with “objec...
createtable#dbccresults
(
DBName[varchar](255),
LogSize_MBdecimal(18,
8),
LogSpaceUsed
decimal(18,8),
statusint)
--GettheL...
• Write our scripts to ensure they check
the version of SQL Server first
• run the relevant piece of SQL based
on the vers...
This will output the
following based on
the current version of
SQL Server:
8 – SQL Server 2000
9 – SQL Server 2005
10 – SQ...
If we use this in our script with an IF
statement then we can allow our DMV
script to work on SQL Server 2005
onwards and ...
--RunifSQLServer2000
IFSELECT
CAST(LEFT(SERVERPROPERTY
('productversion'),2)asint)=8
BEGIN
--usingDBCCSQLPERF(LOGSPACE);
-...
-- Run if SQL Server 2005 or above
IF SELECT CAST(LEFT(SERVERPROPERTY
('productversion'), 2) as int) = 8
BEGIN
-- using DM...
You can use this method to do any
number of checks, and it’s always
there in the kitbag for when
needed
1 SCRIPT TO RULE T...
dsp: One Script to Rule Them All - an SQL Script for all Versions
dsp: One Script to Rule Them All - an SQL Script for all Versions
Upcoming SlideShare
Loading in …5
×

dsp: One Script to Rule Them All - an SQL Script for all Versions

170
-1

Published on

One of our Microsoft SQL Server consultants gives us a handy tip for writing a SQL script for many different versions of SQL Server.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
170
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

dsp: One Script to Rule Them All - an SQL Script for all Versions

  1. 1. • Supporting different versions of SQL is a daily challenge for a DBA • One of our Microsoft SQL Server consultants discovered a handy tip • One script for many different versions of SQL server SQL SERVER
  2. 2. I start writing this on my new SQL Server 2012 box and quickly decide to use the Dynamic Management Views (DMV’s)
  3. 3. -- using DMV os Performance stats SELECT instance_name ,cntr_value 'Log File(s) Used Size (KB)' FROM sys.dm_os_performance_counters WHERE counter_name = 'Log File(s) Used Size (KB)' I START WITH SOMETHING LIKE…
  4. 4. • This runs well on my SQL Server 2012, 2008r2 and SQL 2005 servers • SQL Server 2000 server simply comes back with “object not found” • In 2000 days, I have to use the DBCC command SQLPERF to get the log size OF COURSE, SQL SERVER 2000 DIDN’T HAVE DMV’S…
  5. 5. createtable#dbccresults ( DBName[varchar](255), LogSize_MBdecimal(18, 8), LogSpaceUsed decimal(18,8), statusint) --GettheLogfilesizes insertinto#dbccresults exec('dbcc sqlperf(logspace)') --Outputtheresults select*from#dbccresults --USINGDBCCSQLPERF(LOGSPACE); CREATEATABLE
  6. 6. • Write our scripts to ensure they check the version of SQL Server first • run the relevant piece of SQL based on the version of SQL that part of the script supports • To do this we use SERVERPROPERTY this is supported in all version of SQL and likely to be ever more. 1 SCRIPT THAT DOES BOTH
  7. 7. This will output the following based on the current version of SQL Server: 8 – SQL Server 2000 9 – SQL Server 2005 10 – SQL Server 2008 11 – SQL Server
  8. 8. If we use this in our script with an IF statement then we can allow our DMV script to work on SQL Server 2005 onwards and our DBCC script to run on SQL Server 2000 servers.
  9. 9. --RunifSQLServer2000 IFSELECT CAST(LEFT(SERVERPROPERTY ('productversion'),2)asint)=8 BEGIN --usingDBCCSQLPERF(LOGSPACE); --Createatemptable createtable#dbccresults ( DBName[varchar](255), LogSize_MBdecimal(18,8), LogSpaceUseddecimal(18,8), statusint) --GettheLogfilesizes insertinto#dbccresults exec('dbccsqlperf(logspace)') --Outputtheresults select*from#dbccresults END
  10. 10. -- Run if SQL Server 2005 or above IF SELECT CAST(LEFT(SERVERPROPERTY ('productversion'), 2) as int) = 8 BEGIN -- using DMV os Performance stats SELECT instance_name ,cntr_value 'Log File(s) Used Size (KB)' FROM sys.dm_os_performance_counters WHERE counter_name = 'Log File(s) Used Size (KB)' END
  11. 11. You can use this method to do any number of checks, and it’s always there in the kitbag for when needed 1 SCRIPT TO RULE THEM ALL…

×