[英]Update table with matching primary keys
I have two tables TABLE1
and TABLE2
.我有两个表
TABLE1
和TABLE2
。
I need to update TABLE1
with the matching primary keys from TABLE2
.我需要使用来自
TABLE2
的匹配主键来更新TABLE1
。
Here is my code to get the primary key from TABLE1:这是我从 TABLE1 获取主键的代码:
SELECT C.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE C ON C.CONSTRAINT_NAME = T.CONSTRAINT_NAME
WHERE C.TABLE_NAME = 'TABLE1'
AND T.CONSTRAINT_TYPE = 'PRIMARY KEY'
This is the code to get primary key for TABLE2:这是获取 TABLE2 主键的代码:
SELECT C.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE C ON C.CONSTRAINT_NAME = T.CONSTRAINT_NAME
WHERE C.TABLE_NAME = 'TABLE2'
AND T.CONSTRAINT_TYPE = 'PRIMARY KEY'
And this code is used to update TABLE1:此代码用于更新 TABLE1:
MERGE INTO <TABLE1>
USING <TABLE2> ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME)
WHEN MATCHED THEN
UPDATE
SET <TABLE1.COLUMN_NAME> = <TABLE1.COLUMN_NAME>
I need to merge all snippets of code to update the TABLE1, please help me how to get the primary keys and update with a single code.我需要合并所有代码片段来更新 TABLE1,请帮助我如何获取主键并使用单个代码进行更新。
As @GuidoG recommended, you will probably need to use dynamic SQL to perform this feat.正如@GuidoG 建议的那样,您可能需要使用动态 SQL 来执行此壮举。 Furthermore, from your question it is hard to figure out how to determine that Table1 and Table2 are related to one another.
此外,从您的问题很难弄清楚如何确定 Table1 和 Table2 彼此相关。
Let us suppose that you have a table with table pairs available, and Table1 and Table 2 are one of those pairs.让我们假设您有一个带有可用表对的表,表 1 和表 2 是其中一对。 We'll need to figure out what the primary keys of both tables are, and then build the dynamic SQL using this information.
我们需要弄清楚两个表的主键是什么,然后使用此信息构建动态 SQL。
DECLARE @tableCombinations TABLE ([MainTable] NVARCHAR(255), [SubTable] NVARCHAR(255))
INSERT INTO @tableCombinations ([MainTable], [SubTable]) VALUES ('Table1','Table2')
DECLARE @q NVARCHAR(MAX) = ''
;WITH PrimaryKeys AS (
SELECT [Schema_Name] = sch.[name]
, [Table_Name] = tbl.[name]
, [Column_Name] = cols.[name]
FROM [sys].[indexes] i
JOIN [sys].[index_columns] ic ON i.[index_id] = ic.[index_id] AND i.[object_id] = ic.[object_id]
JOIN [sys].[columns] cols ON cols.[object_id] = ic.[object_id] AND cols.[column_id] = ic.[column_id]
JOIN [sys].[tables] tbl ON ic.[object_id] = tbl.[object_id]
JOIN [sys].[schemas] sch ON tbl.[schema_Id] = sch.[schema_id]
WHERE i.[is_primary_key] = 1
)
SELECT @q += 'MERGE INTO ' + QUOTENAME(pkMain.[Schema_Name]) + '.' + QUOTENAME(pkMain.[Table_Name]) +' mt
USING ' + QUOTENAME(pkSub.[Schema_Name]) + '.' + QUOTENAME(pkSub.[Table_Name]) + 'st ON (
mt.' + QUOTENAME(pkMain.[Column_Name]) + ' = st.' + pkSub.[Column_Name] + ')
WHEN MATCHED THEN UPDATE SET mt.' + QUOTENAME(pkMain.[Column_Name]) + ' = st.' + pkSub.[Column_Name] + ';' + CHAR(13) + CHAR(10)
FROM @tableCombinations tc
JOIN [PrimaryKeys] pkMain ON tc.[MainTable] = pkMain.[Table_Name]
JOIN [PrimaryKeys] pkSub ON tc.[SubTable] = pkSub.[Table_Name]
SET @q = REPLACE(@q, ' ', '')
SELECT @q
Of course, if this yields the correct results, you can directly execute this query using [sys].[sp_executesql].当然,如果这产生了正确的结果,您可以使用 [sys].[sp_executesql] 直接执行此查询。 For more reliable results, be sure to include the schema names of the table combinations.
要获得更可靠的结果,请务必包含表组合的架构名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.