0

I am trying to assign 'A' to [Student Details].group based on this SELECT statement.

SELECT     TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
FROM         [Student Details]
WHERE     ([Location Cd] = 'PAR')
ORDER BY Gpa DESC

I can't figure out how to use a SELECT statement in an UPDATE statement. Can someone please explain how to accomplish this?

I am using ASP .NET and MsSQL Server if it makes a difference.

Thanks

user2427023
  • 107
  • 2
  • 4
  • 9

3 Answers3

1

I'm assuming you want to update these records and then return them :

SELECT TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
INTO #temp
FROM [Student Details]
WHERE     ([Location Cd] = 'PAR')
ORDER BY Gpa DESC

update [Student Details] set group='A' where [person id] in(select [person id] from #temp)

select * from #temp

I'm also assuming person id is the PK of student details

Gary W
  • 1,874
  • 1
  • 14
  • 18
0

Is this you want?

Update top (10) Percent [Student Details] set [group] = 'A' 
where [Location Cd] = 'PAR' AND [group] is null
Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
0

Try this using CTE (Common Table Expression):

;WITH CTE AS
(
    SELECT TOP 10 PERCENT [Group]
    FROM      [Student Details]
    WHERE     ([Location Cd] = 'PAR')
    ORDER BY Gpa DESC
)
UPDATE CTE SET [Group] = 'A'
Kaf
  • 33,101
  • 7
  • 58
  • 78
  • thanks, but I'm getting incorrect syntax near GROUP and FROM :( – user2427023 Oct 17 '13 at 08:00
  • There is no syntax error as far as I can see. [Check this similar example](http://sqlfiddle.com/#!3/046e1/3) with no such syntax errors. May be you are missing angle brackets `'[]'` around group which is a key word. – Kaf Oct 17 '13 at 08:05
  • 1
    ah you're a champion, the problem was that there was 2 of the word BY :) Apart from that, it worked :D Thank you. – user2427023 Oct 17 '13 at 08:09
  • Early morning hear and the engine is still warming up ! :) – Kaf Oct 17 '13 at 08:11