插入具有两个参数的选择

时间:2016-09-21 12:30:22

标签: sql sql-server

我想将学生分配到各自的考试中心。

有没有办法做到这一点?

我有两张桌子:

学生

*

ExamCenter

| StudentID  | StudentName | AllocatedCenter |

我正在尝试| CenterID | Center Name |<br> 语句,但有两个不同的参数,如下所示:

正确的查询应该是:

Insert Into Select

我想做这样的事情:

INSERT INTO Student (AllocatedCenter)
    SELECT CenterName 
    FROM ExamCenter 
    WHERE CenterID = 5;

但这是语法错误的陈述,虽然我只想插入特定的INSERT INTO Student (AllocatedCenter) WHERE StudentID = 1 SELECT CenterName FROM ExamCenter WHERE CenterID = 5;

有没有解决方案?

提前谢谢

2 个答案:

答案 0 :(得分:0)

您可以寻找简单的更新

UPDATE S
SET AllocatedCenter =(SELECT TOP 1 [Center Name] FROM ExamCenter WHERE  CenterID= 5 ORDER BY CenterID)
FROM Student s
WHERE StudentID= 1 

答案 1 :(得分:0)

如果要更改现有记录,则必须使用UPDATE而不是INSERT

如果要更新记录的centername,其中studentId = 1     查询是

UPDATE Student set AllocatedCenter = 
(select CenterName from ExamCenter
 where CenterID=5)
WHERE StudentID = 1;