Solved

Problem with Case statement in Stored Procedure

Posted on 2009-07-14
3
158 Views
Last Modified: 2012-05-07
What am I doing wrong here?
Case when @Password <> '' then
	Update vUser_Info
	Set
		[USERID] = @USERID,
		[Password] = @Password,
		[FIRST_NAME] = @FIRST_NAME,
		[LAST_NAME] = @LAST_NAME,
		[EMAIL] = @EMAIL,
		[ROLEID] = @ROLEID,
		[STATUS] = @STATUS,
		[CORPORATE] = @CORPORATE
	Where [UserID] = @UserID	
	End
	
	Case when @Password = '' then
	Update vUser_Info
	Set
		[USERID] = @USERID,
		[FIRST_NAME] = @FIRST_NAME,
		[LAST_NAME] = @LAST_NAME,
		[EMAIL] = @EMAIL,
		[ROLEID] = @ROLEID,
		[STATUS] = @STATUS,
		[CORPORATE] = @CORPORATE
	Where [UserID] = @UserID	
	End

Open in new window

0
Comment
Question by:mattkovo
3 Comments
 
LVL 17

Accepted Solution

by:
pssandhu earned 500 total points
ID: 24852229
You need to use the IF statement:
IF @Password <> ''
BEGIN
	Update vUser_Info
	Set
		[USERID] = @USERID,
		[Password] = @Password,
		[FIRST_NAME] = @FIRST_NAME,
		[LAST_NAME] = @LAST_NAME,
		[EMAIL] = @EMAIL,
		[ROLEID] = @ROLEID,
		[STATUS] = @STATUS,
		[CORPORATE] = @CORPORATE
	Where [UserID] = @UserID	
END
ELSE
BEGIN
         Update vUser_Info
	Set
		[USERID] = @USERID,
		[FIRST_NAME] = @FIRST_NAME,
		[LAST_NAME] = @LAST_NAME,
		[EMAIL] = @EMAIL,
		[ROLEID] = @ROLEID,
		[STATUS] = @STATUS,
		[CORPORATE] = @CORPORATE
	Where [UserID] = @UserID	
END

Open in new window

0
 

Author Closing Comment

by:mattkovo
ID: 31603386
That's exactly what I came up with.  THanks!  I couldn't delete the post fast enough!
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24852257
It should be an IF ELSE construct instead of CASE statement.

Hope you meant @Password IS NULL and @Password IS NOT NULL
instead of @Password = '' and  @Password <> ''

If that is the case, then replace it and that will work.
Hope this helps
If @Password <> ''
begin
Update vUser_Info
Set
        [USERID] = @USERID,
        [Password] = @Password,
        [FIRST_NAME] = @FIRST_NAME,
        [LAST_NAME] = @LAST_NAME,
        [EMAIL] = @EMAIL,
        [ROLEID] = @ROLEID,
        [STATUS] = @STATUS,
        [CORPORATE] = @CORPORATE
Where [UserID] = @UserID        
End
ELSE IF @Password = ''
begin
Update vUser_Info
Set
        [USERID] = @USERID,
        [FIRST_NAME] = @FIRST_NAME,
        [LAST_NAME] = @LAST_NAME,
        [EMAIL] = @EMAIL,
        [ROLEID] = @ROLEID,
        [STATUS] = @STATUS,
        [CORPORATE] = @CORPORATE
Where [UserID] = @UserID        
End

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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