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)
 Using insert in a case statement

Author  Topic 

Carat
Yak Posting Veteran

92 Posts

Posted - 2009-10-26 : 07:38:33
I have a stored procedure where I want to use a Case statement to execute several insert statements. Is this possible? In my example there is an error. I know that it is possible to use an If statement but I want to know whether it is also possible using a Case statement.

create procedure usp_Test
@Var char(1)
as
create table #Temp (
...
)

begin
if (@Var = '1')
begin
case @Variable
when '>'
then insert into #Temp values (...)
when '<'
then insert into #Temp values (...)
end

/*
-- another way
if (@Variable = '>')
begin
insert into #Temp values (...)
end
else if (@Variable = '<')
begin
insert into #Temp values (...)
end
else
begin
print 'nothing'
end
*/
end
go

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-26 : 08:01:08
Not possible.
CASE is an expression and can be used only inside a statement like select...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -