从select语句更新多行

时间:2013-10-17 07:43:25

标签: asp.net sql

我正在尝试根据此SELECT语句将“A”分配给[学生详细信息] .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。

由于

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

这是你想要的吗?

Update top (10) Percent [Student Details] set [group] = 'A' 
where [Location Cd] = 'PAR' AND [group] is null

答案 2 :(得分: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'