SET @COLUMNNAMES_HIST= ( SELECT STRING_AGG( COLUMN_NAME, ' ,') COLUMN_NAMES FROM INFORMATION_SCHEMA.COLUMNS WHERE UPPER(TABLE_SCHEMA) = '@SCHEMANAME' AND UPPER(TABLE_NAME) = '@TABLE_HIST';
SET @MERGESQL = 'MERGE ' + @TABLE_HIST + ' USING #FINAL_TEMP_CPY SRC
ON (TGT.@PRIMARYKEY = SRC.@PRIMARYKEY AND TGT.UPDATE_TIMESTAMP = SRC.UPDATE_TIMESTAMP)
WHEN NOT MATCHED BY TARGET
THEN INSERT
(
@COLUMNNAMES_HIST -- Column list
)
(
SELECT *, GETTIMESTAMP(), GETTIMESTAMP() FROM FINAL_TEMP_CPY;
)'
我首先将 TABLE_HIST 中的所有列提取到一个变量中,并且我在 INSERT 语句中使用了该变量。这样好吗?然后在值中,我从源表中获取所有值并同时获取 TABLE_HIST 中其他两列的时间戳。你认为这会奏效吗?