我有两张表,如下所示:
表1(ID,名称,街道,houseNo,DeliveryID)和表2(ID,名称street houseNo,DeliveryID)
我想创建一个检查Table1(name,street,houseNo)和Table2(name,street houseNo)内容的过程。如果内容彼此相等,则应将Table1.DeliveryID插入Table2.DelvieryID。
我该怎么做?
答案 0 :(得分:3)
请尝试使用内部联接更新(SQL Server Update with Inner Join):
UPDATE Table2
SET Table2.DelvieryID = Table1.DelvieryID
FROM Table2, Table1
WHERE
Table2.name = Table1.name AND
Table2.street = Table1.street AND
Table2.houseNo = Table1.houseNo
要考虑NULL值,请尝试
UPDATE Table2
SET Table2.DelvieryID = Table1.DelvieryID
FROM Table2, Table1
WHERE
ISNULL(Table2.name, '') = ISNULL(Table1.name, '') AND
ISNULL(Table2.street, '') = ISNULL(Table1.street, '') AND
ISNULL(Table2.houseNo, '') = ISNULL(Table1.houseNo, '')
答案 1 :(得分:3)
UPDATE Table2
SET Table2.DelvieryID = Table1.DelvieryID
FROM Table1 t1, Table2 t2
WHERE CHECKSUM(t1.name,t1.street,t1.houseNo)=CHECKSUM(t2.name,t2.street,t2.houseNo)
答案 2 :(得分:2)
简单连接将起作用..
UPDATE tab2
SET tab2.DelvieryID = tab1.DelvieryID
FROM Table2 tab2, Table1 tab1
WHERE
tab2.name = tab1.name AND
tab2.street = tab1.street AND
tab2.houseNo = tab1.houseNo