0

我正在尝试根据此 SELECT 语句将“A”分配给 [Student Details].group。

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

我不知道如何在 UPDATE 语句中使用 SELECT 语句。有人可以解释一下如何做到这一点吗?

我正在使用 ASP .NET 和 MsSQL Server,如果它有所作为的话。

谢谢

4

3 回答 3

1

我假设您要更新这些记录,然后返回它们:

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

我还假设人员 ID 是学生详细信息的 PK

于 2013-10-17T08:01:54.883 回答
0

这是你想要的吗?

Update top (10) Percent [Student Details] set [group] = 'A' 
where [Location Cd] = 'PAR' AND [group] is null
于 2013-10-17T07:53:52.870 回答
0

CTE使用(公用表表达式)试试这个:

;WITH CTE AS
(
    SELECT TOP 10 PERCENT [Group]
    FROM      [Student Details]
    WHERE     ([Location Cd] = 'PAR')
    ORDER BY Gpa DESC
)
UPDATE CTE SET [Group] = 'A'
于 2013-10-17T07:56:25.627 回答