-3

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.

Joël Salamin
  • 3,538
  • 3
  • 22
  • 33
user543489
  • 101
  • 8

4 Answers4

1

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
Community
  • 1
  • 1
TechDo
  • 18,398
  • 3
  • 51
  • 64
0
UPDATE Students
SET FirstName='John'
WHERE ID = 12

That should work for you :)

wotney
  • 1,039
  • 3
  • 21
  • 34
0

To update FirstName of a Student with a specific Id do the following:

update Students 
set FirstName='John'
where Id=12
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
0

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
SAM
  • 825
  • 1
  • 7
  • 15
  • Note that this syntax will be different for different DBMSes, because it is not in the SQL standard. The example you gives looks like MS SQL Server to me, but there's no clue in the question that this is the system the questioner is using. – IMSoP May 05 '13 at 18:21