我有两个表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发布于 2018-10-23 09:57:09
您可以尝试将insert into .... select与NOT exists结合使用。
插入到t2中的数据,这从t1中丢失了Code。
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
)发布于 2018-10-23 09:44:43
使用Union All运算符垂直组合两个列的相同no的表:
SELECT [IND_REF],[CODE] FROM t1 WHERE [IND_REF]=86034
UNION ALL
SELECT [IND_REF],[CODE] FROM t2 WHERE [IND_REF]=86034发布于 2018-10-23 09:47:29
--您可以根据选择使用insert
INSERT INTO t2 (IND_REF ,Code)
SELECT IND_REF ,Code
FROM t2
WHERE IND_REF = 86034https://stackoverflow.com/questions/52945902
复制相似问题