Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Nested IF statements

Author  Topic 

theboyholty
Posting Yak Master

226 Posts

Posted - 2009-03-27 : 13:05:55
I have a Stored Procedure in which I already have various if statements as follows:

IF @FieldSearch LIKE '[0-9]%'
BEGIN
execute some code
union
more code
union
END
ELSE IF @FieldSearch LIKE '[A-Z]%'
BEGIN
execute some code
union
more code
union
END
ELSE
BEGIN
execute some code
END


And all that runs just fine but now I've found that I need to run all the above code IF another condition is true and something else if it isn't. Should I be wary of banging all that into a big hairy IF block? Will all my IFs, ELSEs and ENDs get all confused if I have an IF at the top and another ELSE at the bottom?

Thanks chaps.

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-03-27 : 13:15:20
In t-sql it is very common to have nested if's. The computer should never get confused if you write the code properly, but if it's sloppy (No indentations, and too much on a line) it will wont be hard for the person writing the code to get confused.

if 1 = 0
Begin
Print 1
End
Else If 1 = 2
Begin
Print 2
END
Else
Begin
If 1 = 1
Begin
Print 'Here is a nested If'
END
End






Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page
   

- Advertisement -