1

我必须通过查询联系人表将联系人姓名添加到分配表中。

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

4 回答 4

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
于 2012-12-18T13:00:35.720 回答
2

尝试

Update A
Set A.name = c.First_name + ' ' + c.Last_name
From Assignment A JOIN Contacts C ON A.ContactID = C.ID
于 2012-12-18T13:01:43.253 回答
1

不需要子选择

UPDATE A  
   SET Name = c.first_name + ' ' + c.last_name
FROM Assignments  A 
JOIN contacts c 
   ON c.id = A.contact_id
于 2012-12-18T13:01:58.007 回答
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
于 2012-12-18T13:06:09.430 回答