我想在我的存储过程中使用CASE
。我的代码中出现一些语法错误:
select
case @Temp
when 1 then (@selectoneCount=@selectoneCount+1)
when 2 then (@selectoneCount=@selectoneCount+1)
end
运行时,我得到:
incorrect syntax near '='.
在这一行:
@selectoneCount = @selectoneCount + 1
几乎相等。
实际上,我从另一个 sp 获取返回值到 @temp 中,然后如果 @temp =1 那么我想将 @SelectoneCount 的计数增加 1,依此类推。 请让我知道正确的语法是什么。
最佳答案
CASE
只是一个返回值的“开关”,而不是执行整个代码块。
您需要将代码更改为如下所示:
SELECT
@selectoneCount = CASE @Temp
WHEN 1 THEN @selectoneCount + 1
WHEN 2 THEN @selectoneCount + 1
END
如果@temp
未设置为这些值中的任何一个(1或2),那么您将返回NULL
关于sql-server - 如何在 SQL Server 中使用 Switch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11220226/