如何在单个存储过程中合并两个不同的 SQL Server 2012 数据库表?
How do you merge two different SQL Server 2012 database tables in single stored procedure?
MERGE [160.80.3.220].[sample].[dbo].[Products] AS TARGET
USING UpdatedProducts AS SOURCE ON (TARGET.ProductID = SOURCE.ProductID)
-- When records are matched, update
-- the records if there is any change
WHEN MATCHED AND TARGET.ProductName <> SOURCE.ProductName
OR TARGET.Rate <> SOURCE.Rate THEN
UPDATE
SET TARGET.ProductName = SOURCE.ProductName,
TARGET.Rate = SOURCE.Rate
-- When no records are matched, insert
-- the incoming records from source
-- table to target table
WHEN NOT MATCHED BY TARGET THEN
INSERT (ProductID, ProductName, Rate)
VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)
-- When there is a row that exists in target table and
-- same record does not exist in source table
-- then delete this record from target table
WHEN NOT MATCHED BY SOURCE THEN
DELETE
-- $action specifies a column of type nvarchar(10)
-- in the OUTPUT clause that returns one of three
-- values for each row: 'INSERT', 'UPDATE', or 'DELETE',
-- according to the action that was performed on that row
OUTPUT $action,
DELETED.ProductID AS TargetProductID,
DELETED.ProductName AS TargetProductName,
DELETED.Rate AS TargetRate,
INSERTED.ProductID AS SourceProductID,
INSERTED.ProductName AS SourceProductName,
INSERTED.Rate AS SourceRate;
SELECT @@ROWCOUNT;
GO
开始于:
target_table cannot be a remote table. target_table cannot have any
rules defined on it.
您可以做的是首先使用四部分查询将链接服务器中的所有数据插入到当前服务器数据库 table,然后执行 Merge
.
或:
使用源 table 作为远程 table,因为 USING
支持远程 table。所以你可以做的是:
首先将连接更改为 [160.80.3.220].[sample]
然后:
MERGE [dbo].[Products] AS TARGET
USING [linked server instance].[database].[schema].UpdatedProducts AS SOURCE
MERGE [160.80.3.220].[sample].[dbo].[Products] AS TARGET
USING UpdatedProducts AS SOURCE ON (TARGET.ProductID = SOURCE.ProductID)
-- When records are matched, update
-- the records if there is any change
WHEN MATCHED AND TARGET.ProductName <> SOURCE.ProductName
OR TARGET.Rate <> SOURCE.Rate THEN
UPDATE
SET TARGET.ProductName = SOURCE.ProductName,
TARGET.Rate = SOURCE.Rate
-- When no records are matched, insert
-- the incoming records from source
-- table to target table
WHEN NOT MATCHED BY TARGET THEN
INSERT (ProductID, ProductName, Rate)
VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)
-- When there is a row that exists in target table and
-- same record does not exist in source table
-- then delete this record from target table
WHEN NOT MATCHED BY SOURCE THEN
DELETE
-- $action specifies a column of type nvarchar(10)
-- in the OUTPUT clause that returns one of three
-- values for each row: 'INSERT', 'UPDATE', or 'DELETE',
-- according to the action that was performed on that row
OUTPUT $action,
DELETED.ProductID AS TargetProductID,
DELETED.ProductName AS TargetProductName,
DELETED.Rate AS TargetRate,
INSERTED.ProductID AS SourceProductID,
INSERTED.ProductName AS SourceProductName,
INSERTED.Rate AS SourceRate;
SELECT @@ROWCOUNT;
GO
开始于:
target_table cannot be a remote table. target_table cannot have any rules defined on it.
您可以做的是首先使用四部分查询将链接服务器中的所有数据插入到当前服务器数据库 table,然后执行 Merge
.
或:
使用源 table 作为远程 table,因为 USING
支持远程 table。所以你可以做的是:
首先将连接更改为 [160.80.3.220].[sample]
然后:
MERGE [dbo].[Products] AS TARGET
USING [linked server instance].[database].[schema].UpdatedProducts AS SOURCE