2016-09-21 84 views
0

我想分配學生到各自的考試中心。插入兩參數選擇

有沒有辦法做到這一點?

我有2個表:

學生

| StudentID | StudentName | AllocatedCenter | 

ExamCenter

| 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; 

但這是語法錯誤的語句雖然我只想插入特定StudentID

是有沒有解決辦法?

預先感謝您

+2

我想你想'更新'表'Student'而不是插入新記錄,對不對? – Aquillo

+0

是的,我需要使用UPDATE語句插入最後一列中創建表時創建NULL的記錄。 – PushkarRode

回答

0

您可以尋找簡單的更新

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

如果你想改變現有的記錄,那麼你必須使用UPDATE不插入

如果您想更新studentId = 1時記錄的centername,則 查詢爲

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