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)
 End If statment in T-SQL?

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2008-11-07 : 22:18:24
I am very familiar with programing in Perl and up until now been having my perl scripts do all the leg work. Well now I am moving a lot of the leg work over to stored procs, views, and general t-sql commands. In one of the procs I am making I have a single variable that can have 3 possible values so I was going to do an If else End if statement but can't quite figure out the t-sql equivalent to the END IF part of the statement. The code I have written so far is this:

create proc UpdateStaffWebSites (
@UpdateType nchar(6),
@SiteID smallint,
@UID varchar(50),
@SiteAddress varchar(50),
@SiteName varchar(50),
@OutPut ntext output
) as
if @UpdateType = 'New'
insert into FFAdmin.dbo.StaffWebSites (UID, SiteAddress, SiteName) values ('@UID', '@SiteAddress', '@SiteName')
end if

if @UpdateType = 'Remove'
delete FFAdmin.dbo.StaffWebSites where SiteID = @SiteID
end if

if @UpdateType = 'Update'
begin
update FFAdmin.dbo.StaffWebSites set
SiteAddress = @SiteAddress,
SiteName = @SiteName
where SiteID = @SiteID
end
end if


Now I understand that, in this case, I could do if else if else if else... statements but in a few other scripts that I am working over in my head it would make programing them easier in this format.

So what is the t-sql version of the “end if”?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-11-07 : 23:15:27
[code]
IF <condition>
begin
...
end
else if <condition>
begin
...
end
else
begin
...
end
[/code]

Be One with the Optimizer
TG
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2008-11-08 : 03:17:25
Though using BEGIN END statements is a good practice but it is not necessary when only one statement is being processed in IF condition.
Go to Top of Page
   

- Advertisement -