使用Select语句的SQL更新

时间:2012-12-18 12:57:40

标签: sql sql-server-2005 select

我必须通过查询联系人表格为分配表添加联系人姓名。

Contact表:

 ID     First_name     Last_Name
 -----------------------------------
 1234   John           Jones
 9876   Mary           Smith

Assignment

 ContactID   Name
 -----------------
  1234
  9876

使用此查询我

  

子查询返回的值超过1个。

查询:

 update A  
 set Name = (select  distinct first_name + ' ' + last_name from contacts c join        Assignments A on c.id = A.contact_id where A.contact_id = c.id)
 from Assignments  A 
 join contacts c on c.id = A.contact_id
 where c.id = A.contact_id

我错过了什么?

4 个答案:

答案 0 :(得分:5)

直接

JOIN,如下:

UPDATE a
SET a.Name = c.first_name + ' ' + c.last_name
FROM Assignments a
INNER JOIN Contacts c  ON c.id = A.contact_id

答案 1 :(得分:2)

尝试

Update A
Set A.name = c.First_name + ' ' + c.Last_name
From Assignment A JOIN Contacts C ON A.ContactID = C.ID

答案 2 :(得分:1)

无需子选择

UPDATE A  
   SET Name = c.first_name + ' ' + c.last_name
FROM Assignments  A 
JOIN contacts c 
   ON c.id = A.contact_id

答案 3 :(得分:1)

试试这个

UPDATE a
SET a.Name = c.first_name + ' ' + c.last_name
FROM Assignments a
INNER JOIN Contacts c  ON c.id = A.contact_id