我正在编写一个存储过程,以定期将数据从多个源表复制到目标表。我想将数据从源表的一列插入目标表,然后基于该列更新目标表。
我编写了一个存储过程,
BEGIN TRANSACTION;
BEGIN TRY
INSERT INTO DestTbl (DtTm)
SELECT
TblA.DtTm
FROM
TblA
WHERE
TblA.DtTm > DATEADD(DAY, -1, GETDATE())
AND TblA.DtTm NOT IN
(
SELECT
DestTbl.DtTm
FROM
DestTbl
)
ORDER BY
TblA.DtTm ASC;
COMMIT TRANSACTION;
END TRY
-- There is a transaction like this for each source table
BEGIN TRANSACTION;
BEGIN TRY
UPDATE
DestTbl
SET
DestTbl.Col2 = TblB.SomeCol
FROM
DestTbl
INNER JOIN
TblB
ON
TblB.DtTm > DATEADD(DAY, -1, GETDATE())
AND DestTbl.DtTm = TblB.DtTm;
COMMIT TRANSACTION;
END TRY
确保在UPDATE语句之前执行INSERT的最佳方法是什么?还是应该将查询重构为如下形式:https://stackoverflow.com/a/11010548?
谢谢。