Results 1 to 5 of 5

Thread: SQL update query

  1. #1
    Join Date
    Oct 2008
    Posts
    1

    SQL update query

    Hi everyone im new to the forums and have searched for solution to this but cannot find an answer to this:

    Im trying to construct a SQL statement and i seem to be coming unstuck:

    Table 1
    Pupilid - Subject- Grade

    123 Maths A



    Table 2
    Pupilid - Subject - Grade

    123 Maths Null


    I trying to populate the grade field in table2 from the grade field in table 1 where pupilid AND subject match and i am struggling with the following syntax:

    update table2

    set grade = (select grade from table1 where table1.pupilid and table1.subject = table2.pupilid and table2.subject)

    where exists

    select grade from table2 where table2.pupilid and table2.subject = table1.pupilid and table1.subject)


    But SQL wont allow me to use AND statement


    Hoping you can helpout or any guidance appreciated

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    what database are you using.

    you may be missing ( after exists

  3. #3
    Join Date
    Oct 2007
    Posts
    22

    Try This

    update Table2
    Set Table2.Grade = Table1.Grade
    From Table1
    Where Table1.PupilId = Table2.PupilId
    And Table1.Subject = Table2.Subject

  4. #4
    Join Date
    Oct 2008
    Posts
    4
    hi gadgetmanflyer

    did u got ur answer or having some problem yet.
    plzreply so that i can heklp u.

  5. #5
    Join Date
    Oct 2007
    Posts
    22

    Another way

    update Table2
    Set Table2.Grade =
    (Select Top 1 Table1.Grade
    From Table1
    Where exists
    (Select null From Table1
    Where Table1.PupilId = Table2.PupilId
    And Table1.Subject = Table2.Subject
    And Table2.Grade is null))
    Where Table2.Grade is null

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •