SQL Server 2000“关键字“CASE”附近的语法不正确”

发布于 2024-08-13 15:22:47 字数 557 浏览 6 评论 0原文

当尝试执行以下存储过程时,将返回错误:

EXECUTE p_someProc
--list of vars
CASE WHEN @var1=1 AND @var2=1 THEN 3 
     WHEN @var2=1 THEN 2 
     WHEN @var1=1 THEN 1 END,
--more vars

还包括其他 CASE 函数,但只出现一个指向第一个 CASE 的错误。

当在 PRINT 函数中运行相同的表达式时,将返回预期结果。

declare @var1 bit set @var1 = 1
declare @var2 bit set @var2 = 1

print(CASE WHEN @var1=1 AND @var2=1 THEN 3 
     WHEN @var1=1 THEN 2 
     WHEN @var2=1 THEN 1 END)

输出是“3”

我认为我以某种方式滥用了 CASE 函数。有人能想到解决方法或修复它的方法吗?

提前致谢。

——斯坦

The error is return when the following stored proc is attempted to be executed:

EXECUTE p_someProc
--list of vars
CASE WHEN @var1=1 AND @var2=1 THEN 3 
     WHEN @var2=1 THEN 2 
     WHEN @var1=1 THEN 1 END,
--more vars

There are other CASE functions included, though only a single error is spewed up which points to the first CASE.

When the identical expression is run within a PRINT function, an expected result is returned.

declare @var1 bit set @var1 = 1
declare @var2 bit set @var2 = 1

print(CASE WHEN @var1=1 AND @var2=1 THEN 3 
     WHEN @var1=1 THEN 2 
     WHEN @var2=1 THEN 1 END)

The output is '3'

I assume that I'm misusing the CASE function somehow. Could anyone think of a workaround or a way to fix it?

Thanks in advance.

--Stan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙味迷妹 2024-08-20 15:22:47

正如 JeremyMcGee 上面提到的,您需要执行分配而不是尝试将案例传递给执行人员,请尝试更像以下的操作

Declare @varx int
select @varx = CASE WHEN @var1=1 AND @var2=1 THEN 3 
     WHEN @var2=1 THEN 2 
     WHEN @var1=1 THEN 1 END
EXECUTE p_someProc @varx

As JeremyMcGee alludes to above you need to perform an assignment rather than trying to pass the case to the exec, try something more like the following

Declare @varx int
select @varx = CASE WHEN @var1=1 AND @var2=1 THEN 3 
     WHEN @var2=1 THEN 2 
     WHEN @var1=1 THEN 1 END
EXECUTE p_someProc @varx
指尖微凉心微凉 2024-08-20 15:22:47

如果没有更多代码,很难说清楚。检查 @var1 和 @var2 是否已声明?

确保您也将 CASE 的结果分配给某些东西!

Hard to tell without more code. Check that @var1 and @var2 have been declared, perhaps?

Make sure you're assigning the result of the CASE to something, too!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文