CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2008
    Posts
    26

    [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.

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    50

    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

  3. #3
    Join Date
    Jun 2008
    Posts
    26

    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.

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    50

    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.

  5. #5
    Join Date
    Jun 2008
    Posts
    26

    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.

  6. #6
    Join Date
    Jun 2006
    Posts
    437

    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.

  7. #7
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    50

    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

  8. #8
    Join Date
    Jun 2008
    Posts
    26

    Re: SQL Question

    Hello Kevin,

    Your UPDATE statement works great! Thank you very much for your help.

    belebala

  9. #9
    Join Date
    Jun 2006
    Posts
    437

    Re: SQL Question

    Quote Originally Posted by kevin.horgan View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured