|
-
April 9th, 2009, 02:49 PM
#1
[RESOLVED] SQL Question
Anyone could help me to create the following sql update statement?
Here is my table:
Primary Key | Foreign Key | Description
1 | 1 | a
2 | 1 | null
3 | 2 | b
4 | 2 | null
5 | 3 | c
6 | 3 | null
I would like to copy the data on Description column if the Foreign Key is the same. Here is the outcome I would like to have:
Primary Key | Foreign Key | Description
1 | 1 | a
2 | 1 | a
3 | 2 | b
4 | 2 | b
5 | 3 | c
6 | 3 | c
Thanks.
-
April 10th, 2009, 06:17 AM
#2
Re: SQL Question
Hi belebala,
Try joining the table on its self. Depending on which database you are using it would be something like this.
UPDATE yourTable t1 SET t1.description = t2.description
FROM yourTable t2
WHERE t1.foreignkey = t2.foreignkey
AND COALESCE(t2.description,'') = ''
If your DB does not support COALESCE look for something like "IS NULL" in the database documentation. Basically you want to join "yourTable" on it's self. Where the foreignkey is the same and the description field is a NULL value. Don't try to do something like "WHERE ...AND t2.description = NULL" This is not valid in many databases as NULL = NULL is invalid.
Cheers,
Kevin
-
April 12th, 2009, 11:48 PM
#3
Re: SQL Question
Thank you for your reply Kevin,
I'm using Oracle SQL Developer and when I executed the above sql statement, I got the following error:
Error at Command Line:1 Column:69
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Not sure is it this software doesn't support "UPDATE .. FROM"...
Thanks.
-
April 13th, 2009, 07:19 AM
#4
Re: SQL Question
Hi belebala,
I am guessing it is the FROM clause in the UPDATE which Oracle does not like. I can not remember if Oracle supports that or not. Here is an alternative to try which might also work if the FROM clause is not supported in PL SQL.
UPDATE yourTable t1 SET t1.description = (SELECT t2.description FROM yourTable t2 WHERE t1.foreignkey = t2.foreignkey AND t2.description IS NULL);
Maybe that will work for you.
Cheers,
Kevin
Last edited by kevin.horgan; April 13th, 2009 at 07:31 AM.
-
April 14th, 2009, 09:28 AM
#5
Re: SQL Question
After I executed this statement, I got this error "single-row subquery returns more than one row". And I also tried to replace the equal sign by "IN" but then I received an error of "missing equal sign". Any idea how to fix this? Thank you.
-
April 14th, 2009, 09:58 AM
#6
Re: SQL Question
Hi all.
I doubt you'll able to solve your problem using a SQL command only.
I suggest to write PL/SQL code; if you're going to run the script once to adjust your table, you can write an anonymous PL/SQL script.
-
April 15th, 2009, 02:44 AM
#7
Re: SQL Question
Hi,
OK here is a corrected version of the UPDATE statement.
UPDATE myTable t1 SET t1.description = (SELECT t2.description FROM myTable t2 WHERE t1.foreignkey = t2.foreignkey AND t2.description IS NOT NULL)
WHERE t1.description IS NULL;
This should work for the test data you provided in the initial question.
Good luck,
Kevin
-
April 15th, 2009, 09:09 AM
#8
Re: SQL Question
Hello Kevin,
Your UPDATE statement works great! Thank you very much for your help.
belebala
-
April 15th, 2009, 12:44 PM
#9
Re: SQL Question
 Originally Posted by kevin.horgan
Hi,
OK here is a corrected version of the UPDATE statement.
UPDATE myTable t1 SET t1.description = (SELECT t2.description FROM myTable t2 WHERE t1.foreignkey = t2.foreignkey AND t2.description IS NOT NULL)
WHERE t1.description IS NULL;
This should work for the test data you provided in the initial question.
Good luck,
Kevin
Good.
I surrendered too soon.
(To tell the true, I tried but I missed the last part, the WHERE condition on t1)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|