Solved

What is wrong with this Case statement?

Posted on 2011-03-18
3
240 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
Comment Utility
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 250 total points
Comment Utility
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 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now