我想基于查找表更新事实表。事实表的Addressid
需要更新并替换为查询列的newaddress
,并与查找表的oldaddress
列进行比较。
我写了以下SQL语句:
Update [dbo].[fact_P]
Set Address_Id = (Select AddressID_new
From AddressLookup
Where fact_P.Address_Id = Lookup.AddressID_old)
但它引发了错误。
答案 0 :(得分:0)
您使用错误的别名/表名引用了子查询中的AddressID_old
列。如果我没有弄错的话,您应该使用AddressLookup.Address_ID_old
代替。
UPDATE [dbo].[fact_P]
SET Address_Id = (SELECT AddressID_new FROM AddressLookup al
WHERE fact_P.Address_Id = al.AddressID_old)
答案 1 :(得分:0)
使用以下查询获取UPDATE语句:
UPDATE [dbo].[fact_P] SET Address_Id = AddressID_new
FROM AddressLookup al
WHERE fact_P.Address_Id = al.AddressID_old
答案 2 :(得分:0)
in part,子查询仅在返回1值时才被允许。因此,如果我的表名没有错误,因为Lookup是'AddressLookup',你需要加入:
/
答案 3 :(得分:0)
Update tgt
set Address_Id = lu.Address_Id
from [dbo].[fact_P] tgt
inner join AddressLookup lu
on tgt.Address_Id = lu.AddressID_old;