6

I would like to create computed index on a table if SQL Server is 2008 or newer and a simple index if SQL Server is 2005 or older:

-- check for sql server version
if (select cast(left(cast(serverproperty('productversion') as varchar), 4) as decimal(5, 3))) >= 10 
 CREATE unique nonclustered index ix1_table
 ON table (column1, column2)
 WHERE column1 is not null and column2 is not null
 ELSE
 CREATE nonclustered index ix1_table
 ON table (column1, column2)

The problem is that the whole statement is evaluated and on SQL Server 2005 this throws an error:

Incorrect syntax near the keyword 'WHERE'.

Is it possible to somehow create different index based on SQL Server version?

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
asked Jan 29, 2014 at 12:53
0

3 Answers 3

8

You can use dynamic SQL

I mean first check the version

Then build your SQL statement using a string variable, for example nvarchar(max)

Then execute it by sp_executeSQL

I think following script can work for this task

-- check for sql server version
declare @sql nvarchar(max)
if (select cast(left(cast(serverproperty('productversion') as varchar), 4) as decimal(5, 3))) >= 10 
set @sql = N'CREATE unique nonclustered index ix1_table ON [table] (column1, column2)
 WHERE column1 is not null and column2 is not null'
 ELSE
set @sql = N'CREATE nonclustered index ix1_table ON [table] (column1, column2)'
exec sp_executeSQL @sql
JNK
18.1k6 gold badges63 silver badges98 bronze badges
answered Jan 29, 2014 at 13:03
3
  • 1
    Slightly simpler to only define the repeated portion of the index once, and less prone to mistakes if you later decide to change the name/columns etc. (you might forget to change it in both places). SET @sql = N'CREATE ...'; IF (version check) SET @sql = @sql + ' WHERE ...'; Commented Jan 29, 2014 at 15:17
  • 1
    Why not just exec '<SQL Query>'? What's the difference with exec sp_executeSQL '<Same SQL Query>'? Commented Jan 29, 2014 at 17:46
  • @Massimo exec is a sledgehammer that executes anything that goes. sp_executesql is more strict about the input. See more at SqlSkills' blog Commented Dec 12, 2016 at 7:06
5

As you guessed, the problem with your approach is the query syntax, which is considered invalid if the SQL Server version is not high enough, thus leading to the whole query being refused, even if that code would actually never be executed.

You can use the 'EXECUTE' (or 'EXEC') command to bypass this check:

IF <Version Check>
 EXECUTE('Index creation command for SQL Server 2008')
ELSE
 EXECUTE('Index creation command for SQL Server 2005')

The argument to an 'EXECUTE' statement is not evaluated for correctness beforehand; it's executed as is whenever the statement is reached (potentially incurring errors at runtime).

Full info here.

answered Jan 29, 2014 at 13:30
0
1

Sure, you can do a check for @@VERSION and do conditional code based on that.

If @@VERSION like 'Microsoft SQL Server 2008%' THEN
BEGIN
--'stuff here'
END

You could do it as a case statement, but since @@VERSION returns service pack information it might be providing TMI when all you want is to check for a supported feature.

Good luck!

answered Jan 29, 2014 at 13:06
1
  • 1
    The problem is, the index creation command isn't even considered valid syntax if the version is not high enough, thus the whole query is refused. Commented Jan 29, 2014 at 13:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.