加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – 如何在SQL Server中使用Switch

发布时间:2020-12-12 08:31:02 所属栏目:MsSql教程 来源:网络整理
导读:我想在我的存储过程中使用CASE.我的代码中出现一些语法错误: select case @Temp when 1 then (@selectoneCount=@selectoneCount+1) when 2 then (@selectoneCount=@selectoneCount+1) end 跑步时,我得到: incorrect syntax near ‘=’. 在这行: @selectone
我想在我的存储过程中使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读