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

x
?
Solved

T-SQL CASE UPDATE Statement

Posted on 2013-05-30
4
Medium Priority
?
555 Views
Last Modified: 2013-05-30
Hello:

Below is a script that I'm trying to run that is saying "If the pay code is a salary pay code for employee ELLI0001, then set the field UPR00100.Primary_Pay_Record = 1 else 0".

But, SQL tells me that there are syntax errors.  Can someone please tell me what I'm doing wrong?  It would be much appreciated!

SELECT CASE WHEN UPR00400.PAYRCORD = 'SALY' AND UPR00400.EMPLOYID = 'ELLI0001'
THEN UPDATE UPR00100 SET Primary_Pay_Record = '1'
Else WHEN UPR00400.PAYRCORD <> 'SALY' AND UPR00400.EMPLOYID <> 'ELLI0001'  
THEN UPDATE UPR00100.Primary_Pay_Record = '0' End
INNER JOIN UPR00100 ON UPR00400.EMPLOYID = UPR00100.EMPLOYID

Thanks!

TBSupport
0
Comment
Question by:TBSupport
[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
4 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39207661
You can't do a SELECT ... CASE UPDATE, you can do an UPDATE ... WHERE {criteria}.

<air code, something like this>

UPDATE UPR00100
SET Primary_Pay_Record = '1'
WHERE Pay_code = 'whatever salary pay code is' AND EMPLOYID = 'ELLI0001'

UPDATE UPR00100
SET Primary_Pay_Record = '0'
WHERE Pay_code <> 'whatever salary pay code is' AND EMPLOYID = 'ELLI0001'

btw if Primary_Pay_Record is numeric then lose the single quote marks
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39207722
Hi  TBSupport,

I created this example of case statement on a update

http://sqlfiddle.com/#!3/9454c/4

Script:
Select * from UPR00100;

UPDATE UPR00100
SET Primary_Pay_Record  = 
CASE 
  WHEN Pay_code =  1 AND EMPLOYID = 'ELLI0001' THEN 1
  WHEN Pay_code =  2 AND EMPLOYID = 'ELLI0001' THEN 0
  ELSE Primary_Pay_Record
END;

Select * from UPR00100;

Open in new window


Mock Table:
 create table UPR00100(EMPLOYID varchar(30),Primary_Pay_Record int ,Pay_code int)


INSERT INTO UPR00100(EMPLOYID,Primary_Pay_Record,Pay_code) VALUES('ELLI0001',NULL,'1');
INSERT INTO UPR00100(EMPLOYID,Primary_Pay_Record,Pay_code) VALUES('ELLI0001',NULL,'2');
INSERT INTO UPR00100(EMPLOYID,Primary_Pay_Record,Pay_code) VALUES('BOBY001',NULL,'1');
INSERT INTO UPR00100(EMPLOYID,Primary_Pay_Record,Pay_code) VALUES('BOBY002',NULL,'2');
INSERT INTO UPR00100(EMPLOYID,Primary_Pay_Record,Pay_code) VALUES('BOBY003',NULL,'3');

Open in new window

0
 
LVL 22

Expert Comment

by:Thomasian
ID: 39207778
UPDATE	U1
SET	Primary_Pay_Record = CASE WHEN U4.PAYRCORD = 'SALY' THEN 1 ELSE 0 END
FROM	UPR00100 U1 INNER JOIN
	UPR00400 U4 ON U1.EMPLOYID = U4.EMPLOYID
WHERE	U4.EMPLOYID = 'ELLI0001'

Open in new window

0
 
LVL 7

Accepted Solution

by:
Ross Turner earned 2000 total points
ID: 39207828
i realised i got your requirements wrong try this

WORKING EXAMPLE
http://sqlfiddle.com/#!3/c59b7/1

UPDATE UPR00100
SET Primary_Pay_Record  = 
CASE 
  WHEN UPR00400.PAYRCORD = 'SALY'  AND UPR00400.EMPLOYID = 'ELLI0001'  THEN 1
  WHEN UPR00400.PAYRCORD <> 'SALY' AND UPR00400.EMPLOYID <> 'ELLI0001' THEN 0
END
FROM UPR00100  INNER JOIN
	 UPR00400  ON UPR00100.EMPLOYID = UPR00400.EMPLOYID

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard 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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

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