如果 SQL Server 是 2008 或更新版本,我想在表上创建计算索引,如果 SQL Server 是 2005 或更新版本,我想创建一个简单索引:
-- 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)
Run Code Online (Sandbox Code Playgroud)
问题是整个语句都被评估了,在 SQL Server 2005 上这会引发错误:
关键字“WHERE”附近的语法不正确。
是否可以根据 SQL Server 版本以某种方式创建不同的索引?