Sorry, I did type that wrong here...but it's not wrong in my code. It actually is PRGM_ID. That isn't the problem. :)
Main Topics
Browse All TopicsI want to copy the data of two columns from the record with an Enroll_ID of 1234 and paste it into the same columns for the record with an PRGM_ID of 2222. This first query here is only with one column, and it works!! But how do I get it to work with two columns? This is all in the same table...
UPDATE TABLEA
SET OFFER_NUM = (SELECT OFFER_NUM FROM TABLEA WHERE ENROLL_ID = 1234)
WHERE PRGM_ID = 2222
The above statement works! But here's what I'm trying to do below (the same thing, but with multiple columns), and it's not working...
UPDATE TABLEA
SET (OFFER_NUM, PHONE_YN) = (SELECT OFFER_NUM, PHONE_YN FROM TABLEA WHERE ENROLL_ID = 1234)
WHERE PRGM = 2222
I get the error, "Incorrect syntax near '('" and "Incorrect syntax near the keyword 'WHERE'" Please help, this is driving me up the wall!!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
In standard SQL (2003) you could use a MERGE statement:
MERGE INTO TABLEA A
USING TABLEA B
ON A.PRGM = 2222 AND B.ENROLL_ID = 1234
WHEN MATCHED THEN
UPDATE SET OFFER_NUM = B.OFFER_NUM,
PHONE_YN = B.PHONE_YN;
This is supported by SQL Server 2008 but not by earlier versions.
SQL Server 2000 and 2005 have an alternative non-standard "UPDATE... FROM" syntax. The only problem is that it fails to warn you with an error when potentially ambiguous join criteria are used. ie. joins that return more than one possible value for a row being updated. In that case the UPDATE statement given below will update some random value into the columns being update, which is not generally good news!
As long as there is only one row where ENROLL_ID=1234 then the UPDATE below is "safe". And if there was more than one 1234 row then your first UPDATE statement would also have failed. Personally however, I can't wait until MERGE comes along in 2008 and we can consign this lousy "UPDATE FROM" syntax to the trash where it belongs!
UPDATE A
SET OFFER_NUM = B.OFFER_NUM,
PHONE_YN = B.PHONE_YN
FROM TABLEA A
JOIN TABLEA B
ON A.PRGM = 2222 AND B.ENROLL_ID = 1234;
dportas, your solution saved the day! I just graduated in May with a CIS degree, so I still have a ways to go before become an "expert" at SQL! :) I did exactly what your solution said...except for adding another column to join on. It works!
I didn't have to worry about the "safety" of it, because the ENROLL_ID I used is the auto number primary key for the table. Thanks again.
Business Accounts
Answer for Membership
by: danaseamanPosted on 2007-12-04 at 15:05:42ID: 20407499
Typo?
WHERE PRGM = 2222 should be WHERE PRGM_ID = 2222
UPDATE TABLEA
SET (OFFER_NUM, PHONE_YN) = (SELECT OFFER_NUM, PHONE_YN FROM TABLEA WHERE ENROLL_ID = 1234)
WHERE PRGM_ID = 2222