how to use switch case in stored procedure

Posted by Gow.Net under Sql Server on 10/13/2012 | Points: 10 | Views : 19174 | Status : [Member] | Replies : 2
how to use switch case in stored procedure

gowthaman8870226416


Responses

Posted by: Pandians on: 10/13/2012 [Member] [MVP] Silver | Points: 25

Up
0
Down
Create Proc Usp_SampleProcedure
(
@InParam Int
)As
Begin
Set Nocount On

Select Case @InParam When 1 Then 'One'
When 2 Then 'Two'
When 3 Then 'Three'
Else '...'
End [Result]
End
Go

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 10/14/2012 [Member] Starter | Points: 25

Up
0
Down

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
Create proc [PROC_SAMPLE_Case]

as
begin

SET NOCOUNT ON
Declare @temp table(
id int,
name varchar(50)
)

insert into @temp values(1,'A')
insert into @temp values(2,'B')
insert into @temp values(3,'C')
insert into @temp values(4,'D')

Select id,
case name when 'A' then 'Apple'
when 'B' then 'Boy'
when 'C' then 'Cat'
else 'Dog' end name
from @temp

End



you just have to run the above script then see the result....I hope...it will clear your doubt regarding the concept of case statement in the SQL..

and after that execute the following line code in sql...

Exec PROC_SAMPLE_Case

Sourabh07

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response