How can I update some fields of a select? (something like the following)
update (select *
from Students s
join Father f on s.fId=f.Id
where s.Id=12)
set f.FirstName='John'
Please help. Thank you.
How can I update some fields of a select? (something like the following)
update (select *
from Students s
join Father f on s.fId=f.Id
where s.Id=12)
set f.FirstName='John'
Please help. Thank you.
Try:
Update Students
set FirstName='John'
where Id=12
No need of a select in this case. Check the link SQL UPDATE Statement for more details.
In your case you can use SQL Server - inner join when updating
UPDATE f
SET FirstName='John'
FROM Students s JOIN Father f
ON s.fId=f.Id
WHERE s.Id=12
UPDATE Students
SET FirstName='John'
WHERE ID = 12
That should work for you :)
To update FirstName
of a Student
with a specific Id
do the following:
update Students
set FirstName='John'
where Id=12
You can use a Join update for this kind of situation. for a example if you need to set the fathers surname for his son, You can use a query like this.
UPDATE SonsDetails SET Surname = Father.Surname
FROM [dbo].[SonDetails] AS SonsDetails INNER JOIN [dbo].[FatherDetails] Father
ON [SonsDetails].[FatherId] = Father.Id