只是想知道是否有人可以帮助我进行非常简单的SQL查询。
我需要将数据从table1传输到table2 where table1.jobID = table2.jobID
我应该使用加入吗?
我只想用table1中的数据填充NULL列,使用jobID匹配。
先谢谢
AC
答案 0 :(得分:0)
由于知识有限,听起来就像一个简单的更新:
UPDATE PreviousJobsTEST
SET dValuationDate = Jobs.dValuationDate
FROM PreviousJobsTEST
JOIN Jobs
ON PreviousJobsTEST.iJobID= Jobs.iJobID
如果你只想更新它,如果它是null,那么你可以这样做:
UPDATE PreviousJobsTEST
SET dValuationDate = CASE WHEN PreviousJobsTEST.dValuationDate IS NULL THEN Jobs.dValuationDate ELSE PreviousJobsTEST.dValuationDate END
FROM PreviousJobsTEST
JOIN Jobs
ON PreviousJobsTEST.iJobID= Jobs.iJobID
或者,根据您的评论,您可以执行您列出的内容,只需将其转换为插入后面的SELECT:
INSERT INTO [DocPrep].[dbo].[PreviousJobsTEST] (dValuationDate)
SELECT [DocPrep].[dbo].[Jobs].[dValuationDate]
FROM [DocPrep].[dbo].[PreviousJobsTEST]
JOIN [DocPrep].[dbo].[Jobs]
ON [DocPrep].[dbo].[Jobs].[iJobID] = [DocPrep].[dbo].[PreviousJobsTEST].[iJobID]
答案 1 :(得分:0)
当t1.column为null时,您需要更新。当t1.column不为null时,你应该将它设置为自己。
SELECT *
INTO #table1
FROM (SELECT '1' AS jobid,
NULL AS column1
UNION ALL
SELECT '2',
NULL
UNION ALL
SELECT '3',
NULL
UNION ALL
SELECT '4',
'A'
UNION ALL
SELECT '5',
NULL) a
SELECT *
INTO #table2
FROM (SELECT '1' AS jobid,
'1' AS column1
UNION ALL
SELECT '2',
'2'
UNION ALL
SELECT '3',
'3'
UNION ALL
SELECT '4',
'4'
UNION ALL
SELECT '5',
'5')b
UPDATE t1
SET t1.column1 = ( CASE
WHEN t1.column1 IS NULL THEN t2.column1
ELSE t1.column1
END )
FROM #table1 t1
LEFT JOIN #table2 t2
ON t1.jobid = t2.jobid
SELECT *
FROM #table1