Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Understanding IF BEGIN END
Message
 
 
À
01/10/2012 05:21:21
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Divers
Thread ID:
01553972
Message ID:
01554031
Vues:
53
I will adopt the same approach; will put the BEGIN and END everywhere. I agree this will make the code more readable.
Thank you.

>Hi Dimitry
>
>Like Sergey I like to put a BEGIN and END after my if statement even if its only one line. In fact I usually enter the BEGIN and END before I put any statements in the BEGIN END block.
> I've got some pretty big stored procedures and some to the IF statement blocks are pretty big and have nested IF block in them.
>
>>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform