SQL Server中有多个单独的IF条件

时间:2013-12-05 23:35:23

标签: sql sql-server if-statement

我的多个IF语句在我的存储过程中彼此独立。但由于某种原因,它们彼此嵌套,好像它们是一个大的if语句的一部分

ELSE IF(SOMETHNGZ)
 BEGIN
  IF(SOMETHINGY)
   BEGIN..END
  ELSE IF (SOMETHINGY)
   BEGIN..END
  ELSE
   BEGIN..END
  --The above works I then insert this below and these if statement become nested----
  IF(@A!= @SA)

  IF(@S!= @SS)

  IF(@C!= @SC) 

  IF(@W!= @SW)
  --Inserted if statement stop here
 END
ELSE <-- final else    

所以它会被这样对待

IF(@A!= @SA){           
        IF(@S!= @SS){           
            IF(@C!= @SC) {      
                IF(@W!= @SW){}
            }
        }
    }

我期待的是这个

IF(@A!= @SA){}          
IF(@S!= @SS){}      
IF(@C!= @SC){}
IF(@W!= @SW){}

我也尝试了这个,它会抛出Incorrect syntax near "ELSE". Expecting "CONVERSATION"

IF(@A!= @SA)
BEGIN..END                  
IF(@S!= @SS)
BEGIN..END      
IF(@C!= @SC) 
BEGIN..END  
IF(@W!= @SW)
   BEGIN..END

请注意,从ELSE <--final else开始,现在嵌套在IF(@W!= @SW)内,即使它之前是外部if语句ELSE IF(SOMETHNGZ)的一部分。

编辑

根据要求我的完整陈述

ALTER Procedure [dbo].[SP_PLaces]  
@ID int, 
..more params
AS
BEGIN
SET NOCOUNT ON
DECLARE @SomeId INT
..more varaible
SET @SomeId = user define function()
..more SETS
IF(@ID IS NULL)
BEGIN
BEGIN TRY
    INSERT INTO Places              
    VAlUES(..Values...)            
    ... more stuff...               
    BEGIN TRY       
        exec Store procedure 
            @FIELD = 15, ... more params...             
    END TRY
    BEGIN CATCH
        SELECT ERROR_MESSAGE() AS 'Message' 
        RETURN -1
    END CATCH                      
    RETURN 0                
END TRY
BEGIN CATCH
    SELECT ERROR_MESSAGE() AS 'Message' 
    RETURN -1
END CATCH   
END 
ELSE IF(@ID IS NOT NULL AND @ID in (SELECT ID FROM Places)) 
BEGIN   
     SELECT @MyName = Name ...  
    ...Some stuff....                       
    IF(SOMETHNG_1)          
        BEGIN TRY               
            UPDATE ....                                                                 
        END TRY
        BEGIN CATCH
            SELECT ERROR_MESSAGE() AS 'Message' 
            RETURN -1
        END CATCH
    ELSE IF(SOMETHNG_2)
        BEGIN TRY
            UPDATE ...                                                      
        END TRY
        BEGIN CATCH
            SELECT ERROR_MESSAGE() AS 'Message' 
            RETURN -1
        END CATCH   
    ELSE  
        BEGIN
            BEGIN TRY
                UPDATE ...                                                              
            END TRY
            BEGIN CATCH
                SELECT ERROR_MESSAGE() AS 'Message' 
                RETURN -1
            END CATCH   
        END             
      --The above works I then insert this below and these if statement become nested----
  IF(@A!= @SA)
    BEGIN
     exec Stored procedure 
            @FIELD = 15,
            ... more params...
    END                 
IF(@S!= @SS)
  BEGIN
     exec Stored procedure 
            @FIELD = 10,
            ... more params...
    END     
IF(@C!= @SC) 
  BEGIN
     exec Stored procedure 
            @FIELD = 17,
            ... more params...
    END 
IF(@W!= @SW)
    BEGIN
     exec Stored procedure 
            @FIELD = 12,
            ... more params...
    END
  --Inserted if statement stop here             
END     
ELSE    
    BEGIN
        SET @ResultMessage = 'Update/Delete Failed. No record found with   ID:'+CONVERT(varchar(50), @ID) 
        SELECT @ResultMessage AS 'Message' 
        RETURN -1
    END
Set NOCOUNT OFF
END

3 个答案:

答案 0 :(得分:30)

如果你在多个条件下检查一个变量,那么你会使用这样的东西 这里将执行条件为真的代码块,并忽略其他块。

IF(@Var1 Condition1)
     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

如果要检查多个变量的条件,则必须使用多个IF 语句,每个代码块将独立于其他块执行。

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


IF(@Var2 Condition1)
 BEGIN
   /*Your Code Goes here*/
 END


IF(@Var3 Condition1)
 BEGIN
   /*Your Code Goes here*/
 END

在每个IF语句之后,如果有多个语句被执行,你必须将它们放入  BEGIN..END Block。无论如何,最好使用BEGIN..END块

<强>更新

在你的代码中找到了一些你想要的BEGIN END

ELSE IF(@ID IS NOT NULL AND @ID in (SELECT ID FROM Places))   -- Outer Most Block ELSE IF
BEGIN   
     SELECT @MyName = Name ...  
    ...Some stuff....                       
    IF(SOMETHNG_1)         -- IF
                 --BEGIN
        BEGIN TRY               
            UPDATE ....                                                                 
        END TRY

        BEGIN CATCH
            SELECT ERROR_MESSAGE() AS 'Message' 
            RETURN -1
        END CATCH
                -- END
    ELSE IF(SOMETHNG_2)    -- ELSE IF
                 -- BEGIN
        BEGIN TRY
            UPDATE ...                                                      
        END TRY
        BEGIN CATCH
            SELECT ERROR_MESSAGE() AS 'Message' 
            RETURN -1
        END CATCH   
               -- END
    ELSE                  -- ELSE
        BEGIN
            BEGIN TRY
                UPDATE ...                                                              
            END TRY
            BEGIN CATCH
                SELECT ERROR_MESSAGE() AS 'Message' 
                RETURN -1
            END CATCH   
         END             
      --The above works I then insert this below and these if statement become nested----
          IF(@A!= @SA)
            BEGIN
             exec Store procedure 
                    @FIELD = 15,
                    ... more params...
            END                 
        IF(@S!= @SS)
          BEGIN
             exec Store procedure 
                    @FIELD = 10,
                    ... more params...

答案 1 :(得分:2)

为避免语法错误,请务必始终将BEGINEND放在IF子句之后,例如:

IF (@A!= @SA)
   BEGIN
   --do stuff
   END
IF (@C!= @SC)
   BEGIN
   --do stuff
   END

......等等。这应该按预期工作。想象一下BEGINEND关键字分别作为开始和结束括号。

答案 2 :(得分:1)

也许这有点多余,但似乎没有人提到这个解决方案。

作为SQL的初学者,我发现在使用<IfModule mod_php5.c> php_value always_populate_raw_post_data -1 </IfModule>BEGIN SSMS时,通常会将带有END的波浪线添加到END,因为它们之间还没有内容。如果您只是设置incorrect syntax near 'END'BEGIN来开始并稍后添加实际查询,那么只需添加一个虚假的END语句,这样SSMS就会停止打扰您。

例如:

PRINT

下面的内容确实会让你走错了路,认为你犯了一个语法错误,在这种情况下只是意味着你还需要在BEGIN和END之间添加内容:

IF (1=1)
BEGIN
  PRINT 'BOGUS'
END