在case块中执行命令。

时间:2020-12-07 01:50:44

Is there any possible way to execute something like this in T-SQL?

在T-SQL中是否有可能执行类似的操作?

CASE @@VERSION 
WHEN 'SQL Server 2005'
THEN Command_A
ELSE Command_B
END

This case block should pick Command_A if the server version is 2005. If not Command_B should get executed.

如果服务器版本是2005,那么这个case块应该选择Command_A。如果不是Command_B应该被执行。

4 个解决方案

#1


1  

Actually the case is a "subcommand" of SELECT you could achieve what you want with something like this:

实际上,这个案例是SELECT的一个“子命令”你可以用这样的方式实现你想要的东西:

declare @s varchar(255)
select @s = case @@VERSION 
when 'SQL Server 2005'
THEN 'command 1'
ELSE 'command 2'
END
exec (@s)

#2


0  

That should work just fine, like this:

这应该没问题,就像这样:

if (@@version like '%SQL Server 2014%') select '2014'
else select 'Other'

See SQL Fiddle.

看到SQL小提琴。

But if you're going to have there code that is not supported by the version you're running it in, you should either create separate procedures that you're going to call, or use dynamic SQL.

但是如果你的代码不支持你正在运行的版本,你应该创建单独的程序,你要调用,或者使用动态SQL。

#3


0  

You can use dynamic SQL for that

您可以为此使用动态SQL。

DECLARE @dynamicSql VARCHAR(max);
CASE @@VERSION
            WHEN 'SQL Server 2005'
                THEN @dynamicSql = 'Command_A'
            ELSE @dynamicSql = 'Command_B'
            END

    EXEC (@dynamicSql)

#4


0  

You can use this CASE to get the sql-server version:

您可以使用这个案例来获得sql-server版本:

SELECT CASE SUBSTRING(CAST(SERVERPROPERTY('productversion')AS nvarchar(128)), 1, CHARINDEX('.', CAST(SERVERPROPERTY('productversion')AS nvarchar(128))) - 1)
       WHEN 7 THEN 'SQL Server 7'
       WHEN 8 THEN 'SQL Server 2000'
       WHEN 9 THEN 'SQL Server 2005'
       WHEN 10 THEN 'SQL Server 2008/2008 R2'
       WHEN 11 THEN 'SQL Server 2012'
       WHEN 12 THEN 'SQL Server 2014'  
       ELSE 'Unsupported' END AS DB_Version

Then you just need to execute dynamic sql according to the result.

然后,只需根据结果执行动态sql。

Demo

演示

#1


1  

Actually the case is a "subcommand" of SELECT you could achieve what you want with something like this:

实际上,这个案例是SELECT的一个“子命令”你可以用这样的方式实现你想要的东西:

declare @s varchar(255)
select @s = case @@VERSION 
when 'SQL Server 2005'
THEN 'command 1'
ELSE 'command 2'
END
exec (@s)

#2


0  

That should work just fine, like this:

这应该没问题,就像这样:

if (@@version like '%SQL Server 2014%') select '2014'
else select 'Other'

See SQL Fiddle.

看到SQL小提琴。

But if you're going to have there code that is not supported by the version you're running it in, you should either create separate procedures that you're going to call, or use dynamic SQL.

但是如果你的代码不支持你正在运行的版本,你应该创建单独的程序,你要调用,或者使用动态SQL。

#3


0  

You can use dynamic SQL for that

您可以为此使用动态SQL。

DECLARE @dynamicSql VARCHAR(max);
CASE @@VERSION
            WHEN 'SQL Server 2005'
                THEN @dynamicSql = 'Command_A'
            ELSE @dynamicSql = 'Command_B'
            END

    EXEC (@dynamicSql)

#4


0  

You can use this CASE to get the sql-server version:

您可以使用这个案例来获得sql-server版本:

SELECT CASE SUBSTRING(CAST(SERVERPROPERTY('productversion')AS nvarchar(128)), 1, CHARINDEX('.', CAST(SERVERPROPERTY('productversion')AS nvarchar(128))) - 1)
       WHEN 7 THEN 'SQL Server 7'
       WHEN 8 THEN 'SQL Server 2000'
       WHEN 9 THEN 'SQL Server 2005'
       WHEN 10 THEN 'SQL Server 2008/2008 R2'
       WHEN 11 THEN 'SQL Server 2012'
       WHEN 12 THEN 'SQL Server 2014'  
       ELSE 'Unsupported' END AS DB_Version

Then you just need to execute dynamic sql according to the result.

然后,只需根据结果执行动态sql。

Demo

演示