-
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
-
what database are you using.
you may be missing ( after exists
-
Try This
update Table2
Set Table2.Grade = Table1.Grade
From Table1
Where Table1.PupilId = Table2.PupilId
And Table1.Subject = Table2.Subject
-
hi gadgetmanflyer
did u got ur answer or having some problem yet.
plzreply so that i can heklp u.
-
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
-
Forum Rules
|
|