我基本上有两个表 t1 和 t2,t1 包含针对 ind_ref 86034 的代码 3299,其中 t2 缺少该代码。我想在 t2 中插入并从 t1 获取代码 3299 。
这是 t1
IND_REF Code
86034 3299
这是 t2
IND_REF Code
86034 1212
我想看到输出
IND_REF Code
86034 1212
86034 3299 <-- as insert from t1
我怎样才能做到这一点,
这是我的查询,但它没有更新。
INSERT INTO test.DBO.ATTRIBUTE (ATTR_CODE_REF)
select ((SELECT att.ATTR_CODE_REF
FROM individual ind
join contact c on c.individual_ref=ind.individual_ref
join organisation org on org.organisation_Ref=c.ORGANISATION_REF and c.main_organisation='y' and c.valid_to is null --contact_ref
join attribute att on att.organisation_ref=org.organisation_ref and att.code_type=3299
where iND.individual_ref=86034))--@indref)
from ATTRIBUTE
WHERE ATTRIBUTE.INDIVIDUAL_REF=86034
您可以尝试使用insert into .... select
with NOT exists
。
插入数据t2
,其中丢失Code
的t1
。
insert into t2 (IND_REF,Code)
SELECT IND_REF,Code
FROM t1
WHERE NOT exists
(
SELECT IND_REF,Code
FROM t2
where t1.Code = t2.Code
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句