使用 IF..ELSE IF 在 T-SQL SP 中控制流 - 还有其他方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1562160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:58:36  来源:igfitidea点击:

Control flow in T-SQL SP using IF..ELSE IF - are there other ways?

sqlsql-servertsqlsql-server-2008

提问by abatishchev

I need to branch my T-SQL stored procedure (MS SQL 2008) control flow to a number of directions:

我需要将我的 T-SQL 存储过程 (MS SQL 2008) 控制流分支到多个方向:

CREATE PROCEDURE [fooBar]
   @inputParam INT
AS
BEGIN
  IF @inputParam = 1
  BEGIN
    ...
  END
  ELSE IF @inputParam = 3
  BEGIN
    ...
  END
  ELSE IF @inputParam = 3
  BEGIN
    ...
  END
END

Is there any other ways? For example, in C#I shoud use switch-caseblock.

还有其他方法吗?例如,在C#我应该使用switch-case块。

采纳答案by Philip Kelley

IF...ELSE... is pretty much what we've got in T-SQL. There is nothing like structured programming's CASE statement. If you have an extended set of ...ELSE IF...s to deal with, be sure to include BEGIN...END for each block to keep things clear, and always remember, consistent indentation is your friend!

IF...ELSE... 几乎就是我们在 T-SQL 中得到的。没有什么比结构化编程的 CASE 语句更像的了。如果您有一组扩展的 ...ELSE IF... 需要处理,请确保为每个块包含 BEGIN...END 以保持清晰,并始终记住,一致的缩进是您的朋友!

回答by JohnDavid

Also you can try to formulate your answer in the form of a SELECT CASEStatement. You can then later create simple if then's that use your results if needed as you have narrowed down the possibilities.

您也可以尝试以SELECT CASE声明的形式制定您的答案。然后,您可以稍后创建简单的 if then ,如果需要,则可以使用您的结果,因为您已经缩小了可能性。

SELECT @Result =   
CASE @inputParam   
WHEN 1 THEN 1   
WHEN 2 THEN 2   
WHEN 3 THEN 1   
ELSE 4   
END  

IF @Result = 1   
BEGIN  
...  
END  

IF @Result = 2   
BEGIN   
....  
END  

IF @Result = 4   
BEGIN   
//Error handling code   
END   

回答by Stuart Ainsworth

No, but you should be careful when using IF...ELSE...END IF in stored procs. If your code blocks are radically different, you may suffer from poor performance because the procedure plan will need to be re-cached each time. If it's a high-performance system, you may want to compile separate stored procs for each code block, and have your application decide which proc to call at the appropriate time.

不,但是在存储过程中使用 IF...ELSE...END IF 时应该小心。如果您的代码块完全不同,您的性能可能会很差,因为每次都需要重新缓存过程计划。如果它是一个高性能系统,您可能希望为每个代码块编译单独的存储过程,并让您的应用程序决定在适当的时间调用哪个过程。

回答by HLGEM

Nope IF is the way to go, what is the problem you have with using it?

不,如果是要走的路,您使用它有什么问题?

BTW your example won't ever get to the third block of code as it and the second block are exactly alike.

顺便说一句,您的示例永远不会到达第三个代码块,因为它和第二个块完全相同。

回答by Pete Michaud

CASE expression
      WHEN value1 THEN result1
      WHEN value2 THEN result2
      ...
      WHEN valueN THEN resultN

      [
        ELSE elseResult
      ]
END

http://www.4guysfromrolla.com/webtech/102704-1.shtmlFor more information.

http://www.4guysfromrolla.com/webtech/102704-1.shtml了解更多信息。