在存储过程中嵌套if语句

时间:2019-02-25 11:03:39

标签: sql-server if-statement stored-procedures

我正在创建一个存储过程,该过程需要检查几个条件,并且根据结果必须执行特定的查询。 对于嵌套的IF语句,我遇到了麻烦,而且我似乎真的无法弄清楚语法出了什么问题。

这是我的代码段(其余部分几乎相同,但带有其他值)

 IF @periodeKD = 'Formiddag'
  IF @Ugedagnr = '1'
        BEGIN TRANSACTION
                    @ModulID = 'S1'
                    @ModulID2 = 'S2'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
        COMMIT TRANSACTION
  IF @Ugedagnr = '2'
        BEGIN TRANSACTION
                    @ModulID = 'S5'
                    @ModulID2 = 'S6'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
        COMMIT TRANSACTION
  IF @Ugedagnr = '3'
        BEGIN TRANSACTION
                    @ModulID = 'S9'
                    @ModulID2 = 'S10'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
        COMMIT TRANSACTION
  IF @Ugedagnr = '4'
        BEGIN TRANSACTION
                    @ModulID = 'S13'
                    @ModulID2 = 'S14'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
        COMMIT TRANSACTION
  IF @Ugedagnr = '5'
        BEGIN TRANSACTION
                    @ModulID = 'S17'
                    @ModulID2 = 'S18'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
        COMMIT TRANSACTION

它一直在抱怨第一个IF声明之后的第二个IF声明(IF @Ugedagnr ='1')。

如何编写嵌套的IF语句?

3 个答案:

答案 0 :(得分:0)

IF @periodeKD = 'Formiddag'
BEGIN
  IF @Ugedagnr = '1'
  BEGIN
        BEGIN TRANSACTION
                    SET @ModulID = 'S1'
                    SET @ModulID2 = 'S2'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
        COMMIT TRANSACTION
  END
  ELSE
  IF @Ugedagnr = '2'
  BEGIN
        BEGIN TRANSACTION
                    SET @ModulID = 'S5'
                    SET @ModulID2 = 'S6'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
        COMMIT TRANSACTION
  END
  ELSE
  ...
END

请注意,语法@ModulID = 'S1'是非法的。使用SET @ModulID = 'S1'

@Ugedagnr@Modul*之间的映射可以通过映射表和单选而不是一堆IF轻松实现。

答案 1 :(得分:0)

因此,由于您要针对多个条件检查一个变量,因此,您应该使用类似这样的代码。此处将执行条件为true的代码块,而其他块将被忽略。

IF (@ParentVar1 Condition1)
Begin
       IF(@Var1 Condition1)
             BEGIN
              /*Your Code Goes here*/
             END

       ELSE IF(@Var1 Condition2)
              BEGIN
                /*Your Code Goes here*/ 
              END 
       ELSE IF(@Var1 Condition2)
              BEGIN
                /*Your Code Goes here*/ 
              END 
       ELSE IF(@Var1 Condition2)
              BEGIN
                /*Your Code Goes here*/ 
              END 
       ELSE      --<--- Default Task if none of the above is true
             BEGIN
               /*Your Code Goes here*/
             END
End

答案 2 :(得分:0)

如果您的插入语句相同,则可以使用以下代码段:

DECLARE @Ugedagnr VARCHAR(10) = '1',
    @ModulID VARCHAR(10),
    @ModulID2 VARCHAR(10);

IF @Ugedagnr = '1'
BEGIN
    IF @Ugedagnr = '1'
    BEGIN
        SET @ModulID = 'S1';
        SET @ModulID2 = 'S2';
    END;

    IF @Ugedagnr = '2'
    BEGIN
        SET @ModulID = 'S5';
        SET @ModulID2 = 'S6';
    END;

    IF @Ugedagnr = '3'
    BEGIN
        SET @ModulID = 'S9';
        SET @ModulID2 = 'S10';
    END;
    IF @Ugedagnr = '4'
    BEGIN
        SET @ModulID = 'S13';
        SET @ModulID2 = 'S14';
    END;
    IF @Ugedagnr = '5'
    BEGIN
        SET @ModulID = 'S17';
        SET @ModulID2 = 'S18';
    END;

    SELECT @ModulID, @ModulID2


    BEGIN TRANSACTION
                    @ModulID = 'S9'
                    @ModulID2 = 'S10'
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber)
                    INSERT INTO Undervisning (ModulID, SkemaDato, LokaleID, HoldID, FagID, LaererID, Aar, Uge)
                    VALUES (@ModulID2, GETDATE(), @lokaleID, @holdID, @fagID, @laererID, @CurrentYear, @WeekNumber) 
    COMMIT TRANSACTION
END;