【问题标题】:Using a case statement to set the values of declared variables使用 case 语句设置声明变量的值
【发布时间】:2015-06-17 15:21:40
【问题描述】:

我想知道是否可以通过使用 case 语句来设置声明变量的值。 示例:

CASE
    WHEN fieldValue ='stringValue1' THEN SET @DeclaredVar1 = 100 
END

【问题讨论】:

  • 你能把你真正想做的事情的完整陈述或描述包括进来吗?如果这是针对多行表的 select 语句,结果应该是什么?

标签: sql sql-server database tsql case


【解决方案1】:

您在正确的轨道上,但您使用的语法不正确。应该是

select @DeclaredVar1 = case 
                         when fieldValue ='stringValue1' then 100 
                         else --another option here-- 
                       end

【讨论】:

  • 致 OP:如果您希望此分配在 fieldValue 不是 'stringValue1' 的情况下无效,您可以将 @DeclaredVar1 放入 --another option here--
  • @Andy:谢谢。像魅力一样工作。我有一种感觉,我快到了。只是不太明白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多