Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested If Then Else

Status
Not open for further replies.

u104741

MIS
Joined
Aug 26, 2003
Messages
94
Location
GB
I was wondering if it is possible to nest an if statement in a stored Procedure...

I want to do someithing like;

If @variable1 =-1 THEN
IF @Variable2 = -1 THEN
select abc from d
ELSE
select efg from h
END IF
ELSE
IF @Variable2 = -1 THEN
select ijk from l
ELSE
select mno from p
END IF
END IF

Anyone got any ideas how i can created a stored proc that can do this or recomend a better way... perhaps with a Case statement???

hope someone can help... cheers
 
Code:
If @variable1 =-1
begin
    IF @Variable2 = -1
    begin 
    select abc from d
    end
    ELSE
    begin
    select efg from h
    end
end
ELSE
begin
    IF @Variable2 = -1 
    begin
    select ijk from l
    end
    ELSE
    begin
    select mno from p
    END
end
 
A case stament is a possibility but we'd need to know more of what you are doing to help you there. If you are truly selecting from differnt tables as your example suggests, the if statment is probably the way to go.

The begin and end on the if statement above are not strictly necessary. IF in SQL server only encompases the one statment following it unless begin and end are used, but you can omit them if there is only one statement. I find it easier to use them at all times from a maintenance standpoint and to keep me in practice for when I need them.
 
thanks does the job a treat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top