locked
Case Statements based on a condition. RRS feed

  • Question

  • Hi All,

    I have 2 EXEC statements independent of each other which is in the if loop. But based on a condition which I check in the query starting I need to mention whether to execute only @Statment1 or only @Statement2 or both @Statment1  and @Statment2.

    Please let me know if this is not clear.

    IF x<y

    BEGIN

    If SearchCondition = 'Search'

    then EXEC(@Statement1)

    else  

    EXEC (@Statement1)

    EXEC (@Statement2)

    END

    set @x = @x+1 

    Can some one please suggest a way.

    Thanks,


    VSP

    Wednesday, August 6, 2014 12:54 AM

Answers

  • Below code could help you to execute @Statement1 or @Statement2 or Both based on your search condition.

    IF @x < @y
    BEGIN
    
    	IF SearchCondition = 'Search1'
    	   EXEC(@Statement1)
    
    	ELSE IF SearchCondition = 'Search2'
    	   EXEC (@Statment2)
    
    	ELSE
    	  BEGIN
    		 EXEC (@Statement1)
    		 EXEC (@Statement2)
    	  END
    
    END
    
    set @x = @x+1
    Is this what was your concern?

    • Proposed as answer by Simon Hughes UK Thursday, August 7, 2014 12:05 PM
    • Marked as answer by Elvis Long Monday, August 18, 2014 11:21 AM
    Wednesday, August 6, 2014 4:25 AM

All replies

  • First just define the conditional branching, then fill out the dots:

    IF x<y  BEGIN
    
       IF SearchCondition = 'Search'  BEGIN 
       ......   
      END 
    
      else   BEGIN
      ......
      END
    
    ELSE   BEGIN
    
    ......
    
    END
    
    set @x = @x+1 
    


    Kalman Toth Database & OLAP Architect SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012








    Wednesday, August 6, 2014 1:53 AM
  • As i understand you need to execute statement1 always. its only statement2 that you want to execute conditionally ie when serachcondition <> 'Serach'

    so it should be this

    IF x<y
    BEGIN
    EXEC(@Statement1)
    If SearchCondition <> 'Search'
    EXEC (@Statement2)
    END
    set @x = @x+1 

    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Wednesday, August 6, 2014 3:25 AM
  • Are you looking for the below, as you metioned its a loop.

    while @x<@y
    BEGIN
    	EXEC(@Statement1)
    	
    	If SearchCondition <> 'Search'
    		EXEC (@Statement2)
    	END
    	set @x = @x+1 
    End

    Wednesday, August 6, 2014 4:17 AM
  • Below code could help you to execute @Statement1 or @Statement2 or Both based on your search condition.

    IF @x < @y
    BEGIN
    
    	IF SearchCondition = 'Search1'
    	   EXEC(@Statement1)
    
    	ELSE IF SearchCondition = 'Search2'
    	   EXEC (@Statment2)
    
    	ELSE
    	  BEGIN
    		 EXEC (@Statement1)
    		 EXEC (@Statement2)
    	  END
    
    END
    
    set @x = @x+1
    Is this what was your concern?

    • Proposed as answer by Simon Hughes UK Thursday, August 7, 2014 12:05 PM
    • Marked as answer by Elvis Long Monday, August 18, 2014 11:21 AM
    Wednesday, August 6, 2014 4:25 AM