Skip to content
Advertisement

Best way to determine Server Product version and execute SQL accordingly?

This is a two-pronged question: Scenario:

I have a script to query MSDB and get me details of job schedules. Obviously, the tables differ from SQL 2000 to SQL 2005. Hence, I want to check the version running on the box and query accordingly. Now the questions:

Question 1:

This is what I am doing.

Is there a better way of doing this?

Question 2:

Though the above script runs fine on both 2000 and 2005 boxes, when I replace the “Print..” statements with my actual code, it runs fine on a 2000 box, but when executed on a 2005 box,tries to run the code block meant for 2000 and returns errors.

Here is the actual code:

This script requires a udf in MSDB. Here is the code for the function:

I have got this far and have spent too much time trying to find out what the problem is. Please help.

Advertisement

Answer

the errors are compile time (I ran on 2005):

I added PRINTs and they never appear.

your code has problems because the column names are not compatible with the database you are running. SQL Server 2005 does not have a “sysjobschedules.freq_type” column.

Make a stored procedure XYZ, put the 2000 version in the 2000 database, put the same XYZ procedure on the 2005 machine and put the 2005 version in it. No IF necessary…

EDIT

run this code:

now run this code

try this:

now try running this, but only highlight the PRINT line:

see how compile errors prevent anything from running

EDIT

you might try something like this…

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement