我正在尝试将来自源(链接的Oracle服务器)的非常宽的表合并到目标表(SQL Server 2012),而不列出所有列。两个表都是相同的,除了它们中的记录。 这就是我一直在使用的:
TRUNCATE TABLE TargetTable
INSERT INTO TargetTable
SELECT *
FROM SourceTable
当/如果我得到这个工作,我想把它作为一个程序,以便我可以传递进行更新所需的源,目标和匹配键。现在我只想爱它。
USE ThisDatabase
GO
DECLARE
@Columns VARCHAR(4000) = (
SELECT COLUMN_NAME + ','
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TargetTable'
FOR XML PATH('')
)
MERGE TargetTable AS T
USING (SELECT * FROM SourceTable) AS S
ON (T.ID = S.ID AND T.ROWVERSION = S.ROWVERSION)
WHEN MATCHED THEN
UPDATE SET @Columns = S.@Columns
WHEN NOT MATCHED THEN
INSERT (@Columns)
VALUES (S.@Columns)
请原谅我的诺言。我觉得我只有一半在那里,但我不太了解SQL的某些部分,以便将它们放在一起。非常感谢。
答案 0 :(得分:1)
如前所述,如果您不想指定列,则必须编写动态查询。 在你的情况下,这样的事情应该有所帮助:
DECLARE
@Columns VARCHAR(4000) = (
SELECT COLUMN_NAME + ','
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TargetTable'
FOR XML PATH('')
)
DECLARE @MergeQuery NVARCHAR(MAX)
DECLARE @UpdateQuery VARCHAR(MAX)
DECLARE @InsertQuery VARCHAR(MAX)
DECLARE @InsertQueryValues VARCHAR(MAX)
DECLARE @Col VARCHAR(200)
SET @UpdateQuery='Update Set '
SET @InsertQuery='Insert ('
SET @InsertQueryValues=' Values('
WHILE LEN(@Columns) > 0
BEGIN
SET @Col=left(@Columns, charindex(',', @Columns+',')-1);
IF @Col<> 'ID' AND @Col <> 'ROWVERSION'
BEGIN
SET @UpdateQuery= @UpdateQuery+ 'TargetTable.'+ @Col + ' = SourceTable.'+ @Col+ ','
SET @InsertQuery= @InsertQuery+@Col + ','
SET @InsertQueryValues=@InsertQueryValues+'SourceTable.'+ @Col+ ','
END
SET @Columns = stuff(@Columns, 1, charindex(',', @Columns+','), '')
END
SET @UpdateQuery=LEFT(@UpdateQuery, LEN(@UpdateQuery) - 1)
SET @InsertQuery=LEFT(@InsertQuery, LEN(@InsertQuery) - 1)
SET @InsertQueryValues=LEFT(@InsertQueryValues, LEN(@InsertQueryValues) - 1)
SET @InsertQuery=@InsertQuery+ ')'+ @InsertQueryValues +')'
SET @MergeQuery=
N'MERGE TargetTable
USING SourceTable
ON TargetTable.ID = SourceTable.ID AND TargetTable.ROWVERSION = SourceTable.ROWVERSION ' +
'WHEN MATCHED THEN ' + @UpdateQuery +
' WHEN NOT MATCHED THEN '+@InsertQuery +';'
Execute sp_executesql @MergeQuery
如果您想了解有关合并的更多信息,可以阅读这篇优秀的article
答案 1 :(得分:0)
以下是MERGE的文档: https://msdn.microsoft.com/en-us/library/bb510625.aspx
至于你的代码,我评论了几乎所有内容来解释它并告诉你如何去做。
USE ThisDatabase --This says what datbase context to use.
--Pretty much what database your querying.
--Like this: database.schema.objectName
GO
DECLARE
@SetColumns VARCHAR(4000) = (
SELECT CONCAT(QUOTENAME(COLUMN_NAME),' = S.',QUOTENAME(COLUMN_NAME),',',CHAR(10)) --Concat just says concatenate these values. It's adds the strings together.
--QUOTENAME adds brackets around the column names
--CHAR(10) is a line break for formatting purposes(totally optional)
FROM INFORMATION_SCHEMA.COLUMNS
--WHERE TABLE_NAME = 'TargetTable'
FOR XML PATH('')
) --This uses some fancy XML trick to get your Columns concatenated into one row.
--What really is in your table is a column of your column names in different rows.
--BTW If the columns names in both tables are identical, then this will work.
DECLARE @Columns VARCHAR(4000) = (
SELECT QUOTENAME(COLUMN_NAME) + ','
FROM INFORMATION_SCHEMA.COLUMNS
--WHERE TABLE_NAME = 'TargetTable'
FOR XML PATH('')
)
SET @Columns = SUBSTRING(@Columns,0,LEN(@Columns)) -- this gets rid off the comma at the end of your list
SET @SetColumns = SUBSTRING(@SetColumns,0,LEN(@SetColumns)) --same thing here
SELECT @SetColumns --Your going to want to copy and paste this into your WHEN MATCHED statement
SELECT @Columns --Your going to want to copy this into your WHEN NOT MATCHED statement
GO
特别是关于ROWVERSION的笔记。
MERGE INTO TargetTable AS T
USING SourceTable AS S --Don't really need to write SELECT * FROM since you need the whole table anyway
ON (T.ID = S.ID AND T.[ROWVERSION] = S.[ROWVERSION]) --These are your matching parameters
--One note on this, if ROWVERSION is different versions of the same data you don't want to have RowVersion here
--Like lets say you have ID 1 ROWVERSION 2 in your source but only version 1 in your targetTable
--If you leave T.ID =S.ID AND T.ROWVERSION = S.ROWVERSION, then it will insert the new ROWVERSION
--So you'll have two versions of ID 1
WHEN MATCHED THEN --When TargetTable ID and ROWVERSION match in the matching parameters
--Update the values in the TargetTable
UPDATE SET /*Copy and Paste @SetColumnss here*/
--Should look like this(minus the "--"):
--Col1 = S.Col1,
--Col2 = S.Col2,
--Col3 = S.Col3,
--Etc...
WHEN NOT MATCHED THEN --This says okay there are no rows with the existing ID, now insert a new row
INSERT (col1,col2,col3) --Copy and paste @Columns in between the parentheses. Should look like I show it. Note: This is insert into target table so your listing the target table columns
VALUES (col1,col2,col3) --Same thing here. This is the list of source table columns