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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert else if to case

Status
Not open for further replies.

fxthoth

Programmer
Mar 16, 2008
38
CZ
Hi.

Is in T-SQL possible to do something like switch in PHP?
www.php.net/switch

I have this code:
Code:
declare @variable int
set @variable=3

if @variable=1
	begin
		print 'a'
		print 'another t-sql commands continue...'
	end
else if @variable=2
	begin
		print 'b'
		print 'another t-sql commands continue...'
	end
else if @variable=3
	begin
		print 'c'
		print 'another t-sql commands continue...'
	end

And I want convert it to:
Code:
CASE 
         WHEN @variable=1 THEN 
	  begin
		print 'a'
		print 'another t-sql commands continue...'
	  end
         WHEN @variable=2 THEN 
	  begin
		print 'b'
		print 'another t-sql commands continue...'
	  end
         WHEN @variable=3 THEN 
	  begin
		print 'b'
		print 'another t-sql commands continue...'
	  end
         ELSE 
	  begin
		print 'default'
		print 'another t-sql commands continue...'
	  end
END

In MSDN I found there is this possibility only in SELECT statement. But I dont want to return anything into SELECT. I only want to run T-SQL commands.
Please can you show me how to do it, if it is possible?

Thank you!
 
No, unfortunately T-SQL only supports IF/ELSE and doesn't support CASE as far as I know.
 
T-SQL supports CASE, just not in the same usability as IF/ELSE. CASE is used within a T-SQL query whereas IF/THEN is used to control the flow of your commands.
 
Yes, that's what I meant. You can not use CASE to control programming's flow.
 
I would say, it makes a good wish for SQL Server 2010 :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top