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
 General SQL Server Forums
 New to SQL Server Programming
 Case expression

Author  Topic 

newbie22
Starting Member

2 Posts

Posted - 2012-02-02 : 16:13:13
Hello all,
I am trying to create a case expression with if else nested in it.
Here is what i am tryin to do
-------
Crerate Sp_test
AS
BEGIN

DECLARE @var1,var2,var3
SELECT @var1= ColName FROM Control Where 1=1
Select Case (@var1)
WHEN 0 then
if(var2!=var3)
Begin
{
Statements
}
End
ELSE
Statement
Else
Statement
END
END
Go
-----------
I apologize for such a silly question but i am a total neewbie ...
The error i am getting is Error 0:Syntax error or access violation

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-02-02 : 17:16:26
You can't put IF/ELSE logic in a SELECT. You've generalized your code too much for us to help though.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-02 : 18:19:33
if you want is to vary result set based on condition then you need to use IF ..ELSE and write seperate select statements with each. If you just want to return value based on condition you need to use CASE ...WHEN within select statement just like you've it now

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

newbie22
Starting Member

2 Posts

Posted - 2012-02-03 : 08:06:21
Thank you everyone.. got it figured out.. you were correct, i just used IF ELSE and got what i wanted.
Thank you so much
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-03 : 09:17:25
ok..glad that you got what you wanted

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -