我正在寻找正确的语法以及直接从SQL执行以下操作的方法:从TableMain
中包含的数据中插入或更新(如果数据已经存在)TableA
,两者都具有相同的复合主要键。
两个表都定义为:
CREATE TABLE TableA (
[TID0] [int] NOT NULL,
[TID1] [int] NOT NULL,
[language] [nvarchar](2) NOT NULL,
[TID2] [nvarchar](200) NOT NULL,
[text] [nvarchar](max) NULL,
[updatedOn] [datetime] NOT NULL DEFAULT (getdate())
PRIMARY KEY (
[TID0],
[TID1],
[language],
[TID2],
)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
TableA
将定期删除并填写。
TableMain
作为相同的定义,但将包含更多行数据,我需要的是将TableA
中从未见过的值插入TableMain
,并更新已存在的行。< / p>
我曾经做过这种插入,但我不知道如何处理更新和复合主键:
INSERT INTO TableMain
SELECT * FROM TableA
编辑:我正在使用 SQL Server 9.00.5000
编辑:另一种受MERGE和mimick it
启发的方式DECLARE @updatedIDs TABLE(
[TID0] [int],
[TID1] [int],
[language] [nvarchar](2),
[TID2] [nvarchar](200),
PRIMARY KEY ([TID0], [TID1], [language], [TID2]) -- as stated by Nikola Markovinović above, thanks
);
-- First update records
update TableMain
set [text] = source.[text],
[updatedOn] = source.[updatedOn]
OUTPUT
inserted.[TID0]
inserted.[TID1]
inserted.[language]
inserted.[TID2]
INTO @updatedIDs
from
TableMain AS main
, TableA AS source
WHERE
TableMain.[TID0] = source.[TID0]
and TableMain.[TID1] = source.[TID1]
and TableMain.[language] = source.[language]
and TableMain.[TID2] = source.[TID2]
-- And then insert
insert into TableMain
select *
from TableA AS source
where not exists
(
select 1
from @updatedIDs AS i
where i.[TID0] = source.[TID0]
and i.[TID1] = source.[TID1]
and i.[language] = source.[language]
and i.[TID2] = source.[TID2]
)
答案 0 :(得分:5)
您应该使用merge声明
类似的东西:
merge TableMain AS target
using TableA as source
ON <join tables here>
WHEN MATCHED THEN <update>
WHEN NOT MATCHED BY TARGET <Insert>
WHEN NOT MATCHED BY SOURCE <delete>
答案 1 :(得分:4)
以下是您可能用于插入数据的脚本:
-- On error transaction is automatically rolled back
set xact_abort on
begin transaction
-- First update records
update TableMain
set [text] = source.[text],
[updatedOn] = source.[updatedOn]
from TableMain
inner join TableA source
on TableMain.[TID0] = source.[TID0]
and TableMain.[TID1] = source.[TID1]
and TableMain.[language] = source.[language]
and TableMain.[TID2] = source.[TID2]
-- And then insert
insert into TableMain ([TID0], [TID1], [language], [TID2], [text], [updatedOn])
select [TID0], [TID1], [language], [TID2], [text], [updatedOn]
from TableA source
where not exists
(
select *
from TableMain
where TableMain.[TID0] = source.[TID0]
and TableMain.[TID1] = source.[TID1]
and TableMain.[language] = source.[language]
and TableMain.[TID2] = source.[TID2]
)
commit transaction
如果效果不理想,您可能会将not exists()重写为left join ... where TableMain.TID0 is null
。
答案 2 :(得分:3)
您可以使用SQLServer 2008中的Merge命令。它允许您将来自其他数据源的数据合并到主数据源中,并在存在密钥匹配时定义特定行为(您可能希望更新您的表格)或不匹配,您将要插入新记录。
您可以访问此链接以获取少量代码示例。