Web Develop Wolf
Web Develop Wolf

Reputation: 6326

Simple, standard case statement throwing errors

I have a really simple case statement that should in theory be working unless I've missed something totally obvious. As a small test I'm trying to print the value of a variable based on the value of another separate variable (so for now, no db queries, I'm just using the basic information I've placed in the statement).

I've had a look around at a few other SO questions on the same subject and I'm following them to the letter but this still won't work for some reason.

DECLARE @NEWORGID INT 
SET @NEWORGID = 1

DECLARE @BOOKINGID INT
SET @BOOKINGID = 1

DECLARE @ORGANISATIONID INT
SET @ORGANISATIONID = 2

SELECT
CASE 
    (@NEWORGID) 1 
THEN
    PRINT @bookingid
ELSE 
    PRINT @ORGANISATIONID
END

And the error I'm getting back is:

Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '1'.
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near 'END'.

Apologies if this is a bit of a stupid question but I'm just really struggling to get my head around this one.

Upvotes: 0

Views: 41

Answers (1)

Sparky
Sparky

Reputation: 15075

Try this:

DECLARE @NEWORGID INT 
SET @NEWORGID = 1

DECLARE @BOOKINGID INT
SET @BOOKINGID = 1

DECLARE @ORGANISATIONID INT
SET @ORGANISATIONID = 2




DECLARE @theID INT

SELECT @theID = 
CASE     (@NEWORGID) WHEN 1 THEN  @bookingid ELSE @ORGANISATIONID END

PRINT @theId

or

PRINT 
CASE     (@NEWORGID) WHEN 1 THEN  @bookingid ELSE  @ORGANISATIONID END

Upvotes: 4

Related Questions