SQL Server 2008可以执行TSQL的MERGE命令。用MERGE命令,只需一个语句就可以对行进行 UPDATE、INSERT或 DELETE。
DECLARE
@t1
table
(t
varchar
(
max
))
DECLARE @t2 table (t varchar ( max ))
INSERT INTO @t1
SELECT ' a ' UNION ALL
SELECT ' b ' UNION ALL
SELECT ' c '
INSERT INTO @t2
SELECT ' b ' UNION ALL
SELECT ' c ' UNION ALL
SELECT ' d '
MERGE @t1 AS t1
USING ( SELECT t FROM @t2 ) AS t2
ON t1.t = t2.t
-- WHEN MATCHED THEN
WHEN NOT MATCHED THEN
INSERT (t) VALUES (t);
SELECT * FROM @t1
DECLARE @t2 table (t varchar ( max ))
INSERT INTO @t1
SELECT ' a ' UNION ALL
SELECT ' b ' UNION ALL
SELECT ' c '
INSERT INTO @t2
SELECT ' b ' UNION ALL
SELECT ' c ' UNION ALL
SELECT ' d '
MERGE @t1 AS t1
USING ( SELECT t FROM @t2 ) AS t2
ON t1.t = t2.t
-- WHEN MATCHED THEN
WHEN NOT MATCHED THEN
INSERT (t) VALUES (t);
SELECT * FROM @t1