Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is wrong with this Case statement?

Posted on 2011-03-18
3
Medium Priority
?
251 Views
Last Modified: 2012-05-11
I have function that will return an integer however I need to do some set up before hand.. I'm trying to populate a table variable based on if the paramater passed into the function(@loginID) is null or not.. My Case and my else is being highlighted as invalid syntax.. What is it?
Declare @UserGroup table(UserGroupID int)
 	CASE 
	WHEN @LoginID is not null
	THEN	
	Insert into @UserGroup
		Select UserGroupID from UserGroup_login
		Where LoginID = 103
    ELSE
    Insert into @UserGroup
		Select UserGroupID from UserGroup_login
		Where UserGroupID = @UserGroupID

Open in new window

0
Comment
Question by:cheryl9063
  • 2
3 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35169156
An "IF" structure is more appropriate here:

Declare @UserGroup table(UserGroupID int)
        IF @LoginID is not null
        Insert into @UserGroup
                Select UserGroupID from UserGroup_login
                Where LoginID = 103
        ELSE
        Insert into @UserGroup
                Select UserGroupID from UserGroup_login
                Where UserGroupID = @UserGroupID

Open in new window

0
 
LVL 33

Accepted Solution

by:
knightEknight earned 1000 total points
ID: 35169275
FYI - CASE is useful in SELECT queries like this (for example)

select CASE WHEN @LoginID is not null THEN 103 ELSE @UserGroupID END as myGroupID
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 35169281
You can simply try like this.
DECLARE  @UserGroup  TABLE( 
                           UserGroupID INT 
                           ) 

INSERT INTO @UserGroup 
SELECT UserGroupID 
  FROM UserGroup_login 
 WHERE (LoginID = 103 
        AND @LoginID IS NOT NULL) 
        OR (UserGroupID = @UserGroupI 
            AND @LoginID IS NULL)

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Integration Management Part 2
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question