Solved

What is wrong with this Case statement?

Posted on 2011-03-18
3
245 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 250 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 40

Assisted Solution

by:Sharath
Sharath earned 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.​
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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