Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding IF BEGIN END
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01553972
Message ID:
01553980
Views:
61
Thank you for the explanation of BEGIN/END and correction of my use of RTRIM().

>1. You don't need BEGIN/END for one statement but you can wrap even one statement in BEGIN/END. I often do for consistency or if I expect to use more than one statement in the future
>2. Yes, you have to use BEGIN/END for more than one statement otherwise only first one of the block after IF will be executed.
>
>BTW, LEN() function retuns length of it's parameter w/o trailing spaces so RTRIM() in len(rtrim(@FoundValue)) is redundant. IOW len(rtrim(@FoundValue)) returns the same result as len(@FoundValue)
>
>>
>>I want to be sure I understand the use of BEGIN / END with IF statement in a stored procedure.
>>
>>Here is how I understand it:
>>
>>1. If there is only one statement after the IF condition, I don't need to have BEGIN / END. That is, this one statement after IF will be executed.
>>
>>2. But if there is more than one statement after IF, I must have these statements enclosed with BEGIN / END? Here is an example:
>>
>>
>>if @SomeVar = 1
>>   BEGIN
>>   select @FoundValue = column_name from table1 where 'some condition'
>>   if len(rtrim(@FoundValue)) > 0
>>      set @Var2 = @FoundValue
>>   END
>>
>>
>>That is, I must have the BEGIN and END above to make sure all lines after the first IF are executed only if @SomeVar =1 ?
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform