Can we alter the flow of the execution in SQL Server ?

 Posted by PandianS on 5/6/2011 | Category: Sql Server Interview questions | Views: 5056 | Points: 40
Answer:

Yes. We can alter the flow of the execution of the statements using "GOTO" and "Lable name"

Using GOTO statement, we can skip the flow of the execution pointing to Label name.
Declare @Input Int, @Status Varchar(5), @Result Varchar(100)

Select @Input = 10
Select @Result = 'The Input(' + Cast(@Input as varchar(5)) + ') is '
If ((@Input%2) = 0)
Goto Even
Else
Goto Odd
Even:
Select @Status = '"Even"'
Goto Result
Odd:
Select @Status = '"Odd"'
Goto Result
Select @Status = '.....Test.....'
Result:
Select @Result = @Result + @Status
Select @Result
Goto Finish
Finish:


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Mcadeepuraj on: 5/23/2011 | Points: 10
Nice Post, Generally we use for Error handler in sql

Login to post response