Solved

What is wrong with this Case statement?

Posted on 2011-03-18
3
241 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

12 Experts available now in Live!

Get 1:1 Help Now