我正在查看MERGE
命令看起来很酷,但仍需要指定列。我正在寻找类似的东西:
MERGE INTO target AS t
USING source AS s
WHEN MATCHED THEN
UPDATE SET
[all t.fields = s.fields]
WHEN NOT MATCHED THEN
INSERT ([all fields])
VALUES ([all s.fields])
有可能吗?
答案 0 :(得分:13)
我很懒...这是我编写的一个便宜的proc,会为表格吐出一般的MERGE命令。它在information_schema.columns中查询列名。我删除了我的源数据库名称 - 所以,你必须更新proc以使用你的数据库(寻找@SourceDB ......我说它很便宜。)无论如何,我知道其他人可以写得更好 - 它服务于我目的很好。 (它做了一些假设,你可以把逻辑放在处理中 - 即关闭IDENTITY_INSERT - 即使表没有标识列。)它更新当前上下文中的表。它是针对sql server 2008编写的,用于同步一些表。当然,使用风险自负。
CREATE PROCEDURE [dbo].[GenerateMergeSQL]
@TableName varchar(100)
AS
BEGIN
SET NOCOUNT ON
declare @sql varchar(5000),@SourceInsertColumns varchar(5000),@DestInsertColumns varchar(5000),@UpdateClause varchar(5000)
declare @ColumnName varchar(100), @identityColName varchar(100)
declare @IsIdentity int,@IsComputed int, @Data_Type varchar(50)
declare @SourceDB as varchar(200)
-- source/dest i.e. 'instance.catalog.owner.' - table names will be appended to this
-- the destination is your current db context
set @SourceDB = '[mylinkedserver].catalog.myDBOwner.'
set @sql = ''
set @SourceInsertColumns = ''
set @DestInsertColumns = ''
set @UpdateClause = ''
set @ColumnName = ''
set @isIdentity = 0
set @IsComputed = 0
set @identityColName = ''
set @Data_Type = ''
DECLARE @ColNames CURSOR
SET @ColNames = CURSOR FOR
select column_name, COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IsIdentity ,
COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsComputed') as IsComputed , DATA_TYPE
from information_schema.columns where table_name = @TableName order by ordinal_position
OPEN @ColNames
FETCH NEXT FROM @ColNames INTO @ColumnName, @isIdentity, @IsComputed, @DATA_TYPE
WHILE @@FETCH_STATUS = 0
BEGIN
if @IsComputed = 0 and @DATA_TYPE <> 'timestamp'
BEGIN
set @SourceInsertColumns = @SourceInsertColumns +
case when @SourceInsertColumns = '' THEN '' ELSE ',' end +
'S.' + @ColumnName
set @DestInsertColumns = @DestInsertColumns +
case when @DestInsertColumns = '' THEN '' ELSE ',' end +
@ColumnName
if @isIdentity = 0
BEGIN
set @UpdateClause = @UpdateClause +
case when @UpdateClause = '' THEN '' ELSE ',' end
+ @ColumnName + ' = ' + 'S.' + @ColumnName + char(10)
END
if @isIdentity = 1 set @identityColName = @ColumnName
END
FETCH NEXT FROM @ColNames INTO @ColumnName, @isIdentity, @IsComputed, @DATA_TYPE
END
CLOSE @ColNames
DEALLOCATE @ColNames
SET @sql = 'SET IDENTITY_INSERT ' + @TableName + ' ON;
MERGE ' + @TableName + ' AS D
USING ' + @SourceDB + @TableName + ' AS S
ON (D.' + @identityColName + ' = S.' + @identityColName + ')
WHEN NOT MATCHED BY TARGET
THEN INSERT(' + @DestInsertColumns + ')
VALUES(' + @SourceInsertColumns + ')
WHEN MATCHED
THEN UPDATE SET
' + @UpdateClause + '
WHEN NOT MATCHED BY SOURCE
THEN DELETE
OUTPUT $action, Inserted.*, Deleted.*;
SET IDENTITY_INSERT ' + @TableName + ' OFF'
Print @SQL
END
答案 1 :(得分:7)
不是你想要的一切,而是部分:
WHEN NOT MATCHED THEN
INSERT
的 ([all fields])
VALUES (field1, field2, ...)
(值列表必须完整,并且与表定义中字段的顺序相匹配。)
答案 2 :(得分:1)
简单的合并替代方法,无需命名任何字段或在表设计更改时必须更新语句。从源到目标,这是单向的,但它可以是双向的。仅对更改的记录起作用,因此即使连接速度较慢的链接服务器也非常快。
--Two statement run as transaction batch
DELETE
C
FROM
productschina C
JOIN
(select * from productschina c except select * from productsus) z
on c.productid=z.productid
INSERT into productschina select * from productsus except select * from productschina
以下是设置要测试的表格的代码:
--Create a target table
--drop table ProductsUS
CREATE TABLE ProductsUS
(
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Rate MONEY
)
GO
--Insert records into target table
INSERT INTO ProductsUS
VALUES
(1, 'Tea', 10.00),
(2, 'Coffee', 20.00),
(3, 'Muffin', 30.00),
(4, 'Biscuit', 40.00)
GO
--Create source table
--drop table productschina
CREATE TABLE ProductsChina
(
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Rate MONEY
)
GO
--Insert records into source table
INSERT INTO ProductsChina
VALUES
(1, 'Tea', 10.00),
(2, 'Coffee', 25.00),
(3, 'Muffin', 35.00),
(5, 'Pizza', 60.00)
GO
SELECT * FROM ProductsUS
SELECT * FROM ProductsChina
GO
答案 3 :(得分:0)
我认为这个答案值得更多的爱。它简单,优雅,有效。但是,根据所讨论的表格,它可能有点慢,因为except子句正在评估每一列。
我怀疑你可以通过加入主键和最后修改日期(如果存在)来节省一点时间。
DELETE
C
FROM
productschina C
JOIN
(select primary_key, last_mod_date from productschina c except select primary_key, last_mod_date from productsus) z
on c.productid=z.productid
INSERT into productschina select * from productsus except select * from productschina