我在这里有这个问题
SELECT a.FileTrackingTag, a.DataSetName FROM TempTable a
LEFT JOIN FinalTable b ON a.Title = b.Title
WHERE b.FileTrackingTag IS NULL
AND b.DataSetName IS NULL
我想在finaltable中使用temptable中的值更新Filetrackingtag和datasetname,只要它们为null并且在两个表之间具有相同的Title和SampleID
表结构是这样的, 两个表都有Title,Filetrackingtag和Datasetname
有什么建议吗?
答案 0 :(得分:4)
我认为您正在寻找的陈述是:
UPDATE f SET
Filetrackingtag = isnull(f.Filetrackingtag, t.Filetrackingtag),
Datasetname = isnull(f.Datasetname, t.Datasetname)
FROM FinalTable f
JOIN TempTable t
ON f.Title = t.Title and f.SampleID = t.SampleID
(我没有测试过代码,但我非常有信心它应该工作)
答案 1 :(得分:1)
在UPDATE语句中使用派生表
UPDATE x
SET x.FileTrackingTag = x.newFileTrackingTag,
x.DataSetName = x.newDataSetName
FROM (
SELECT a.FileTrackingTag AS newFileTrackingTag, a.DataSetName AS newDataSetName,
b.FileTrackingTag, b.DataSetName
FROM TempTable a LEFT JOIN FinalTable b ON a.Title = b.Title AND a.SampleID = b.SampleID
WHERE b.FileTrackingTag IS NULL AND b.DataSetName IS NULL
) x