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

2021-09-10 00:00:00 sql tsql sql-server-2008 sql-server

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

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

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

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

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

推荐答案

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

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!

相关文章