(T)SQL case语句(不是表达式)

时间:2021-12-07 09:10:34

Is there any alternative way to do multiple statements based on conditions: if(expr1) then select this else if(expr2) then select that

有没有其他方法可以根据条件执行多个语句:if(expr1)然后选择其他if(expr2)然后选择

...the case would serve this purpose nice, if it weren't limited to expressions. I'm using sql server

......如果不局限于表达,那么这个案例就可以达到这个目的。我正在使用sql server

Code until now:

代码到现在为止:

if(@code=57) begin (lots of complex statements) end
if(@code=58) begin (lots of other complex statements) end
if(@code=59) begin (lots of other complex statements) end
if(@code=60) begin (lots of other complex statements) end

I can't use a case, because it's an expression and it returns values, it cannot run statements.

我不能使用一个案例,因为它是一个表达式并且它返回值,它不能运行语句。

1 个解决方案

#1


0  

IF is a procedural statement, that is to say it directs the flow of statements in a multi-statement batch or procedure. CASE cannot do this. Though it may look like a procedural statement, CASE is actually a function; it returns different values based on the input. CASE does not redirect the control of program flow. CASE can invoke subqueries; this might be unwise from a performance perspective, but even at that the subqueries could only return a single value.

IF是一个过程语句,也就是说它在多语句批处理或过程中引导语句流。 CASE不能这样做。虽然它可能看起来像一个程序性陈述,但CASE实际上是一个功能;它根据输入返回不同的值。 CASE不会重定向程序流的控制。 CASE可以调用子查询;从性能角度来看,这可能是不明智的,但即使这样,子查询也只能返回一个值。

#1


0  

IF is a procedural statement, that is to say it directs the flow of statements in a multi-statement batch or procedure. CASE cannot do this. Though it may look like a procedural statement, CASE is actually a function; it returns different values based on the input. CASE does not redirect the control of program flow. CASE can invoke subqueries; this might be unwise from a performance perspective, but even at that the subqueries could only return a single value.

IF是一个过程语句,也就是说它在多语句批处理或过程中引导语句流。 CASE不能这样做。虽然它可能看起来像一个程序性陈述,但CASE实际上是一个功能;它根据输入返回不同的值。 CASE不会重定向程序流的控制。 CASE可以调用子查询;从性能角度来看,这可能是不明智的,但即使这样,子查询也只能返回一个值。