Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is wrong with this Case statement?

Posted on 2011-03-18
3
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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