该解决方案适用于五列。100列怎么样。您需要动态 T-SQL。
下面的代码很长,分为 4 个部分。
第 1 部分 - 创建数据库、测试表和测试数据。
第 2 节 - 我为分隔列列表和 Jeff Moden 的拆分函数定义的用户定义函数。
第 3 节 - 动态 T-SQL,假设第一列是键,最后一列是标志。使用 EXCEPT 命令查找行差异。
第 4 节 - 动态 T-SQL,将 T1 中的第 2 列与 T2 中的第 2 列与 case 语句进行比较。继续所有列。仅将有差异的行插入 T3。
-- THIS CODE WILL WORK FOR A DYNAMIC LIST OF COLUMNS, NOT JUST 4! --
-- 1 - Create test tables w/data
-- the master db
use master;
go
-- create test database
create database test;
go
-- use test
use test;
go
-- create table 1
if (OBJECT_ID('t1') <> 0) drop table t1;
go
create table t1
( key1 int, col1 int, col2 int, col3 varchar(16), col4 varchar(16) );
go
-- create table 2
if (OBJECT_ID('t2') <> 0) drop table t2;
go
create table t2
( key1 int, col1 int, col2 int, col3 varchar(16), col4 varchar(16), flag1 int default 0);
go
-- create table 3
if (OBJECT_ID('t3') <> 0) drop table t3;
go
create table t3
( key1 int, col1 int, col2 int, col3 varchar(16), col4 varchar(16) );
go
-- Add 5 rows to t1
insert into t1 values (1, 2, 4, 'A', 'B');
insert into t1 values (2, 4, 8, 'C', 'D');
insert into t1 values (3, 6, 12, 'E', 'F');
insert into t1 values (4, 8, 16, 'G', 'H');
insert into t1 values (5, 10, 20, 'I', 'J');
select * from t1;
-- Add 5 rows to t2
insert into t2 (key1, col1, col2, col3, col4) values (1, 2, 4, 'A', 'B');
insert into t2 (key1, col1, col2, col3, col4) values (2, 4, 8, 'C', 'D');
insert into t2 (key1, col1, col2, col3, col4) values (3, 6, 12, 'E', 'F');
insert into t2 (key1, col1, col2, col3, col4) values (4, 8, 16, 'G', 'H');
insert into t2 (key1, col1, col2, col3, col4) values (5, 10, 20, 'I', 'K');
select * from t2;
--
-- 2A - Declare helper function for column name list
--
-- use test
use test;
go
-- remove function if it exists
if (OBJECT_ID('dbo.get_column_list') <> 0)
drop function get_column_list;
go
-- create new function
create function get_column_list (@schema_name sysname, @table_name sysname, @del_value varchar(10) = ',') returns varchar(max)
as
begin
-- nothing to do
if (@table_name is null) return null;
-- misc variables
declare @list varchar(max) = '';
-- select the changed items
select
@list += c.name + @del_value
from
sys.schemas s join sys.objects o on s.schema_id = o.schema_id
join sys.columns c on o.object_id = c.object_id
where
o.type = 'u' and
s.name = @schema_name and
o.name = @table_name
order by c.column_id;
-- remove last delimiter
select @list = substring(@list, 1, len(@list) - len(@del_value));
-- return a list
return @list
end;
go
--
-- 2B - spliter function from jeff moden
--
-- http://www.sqlservercentral.com/articles/Tally+Table/72993/
-- You download and install as TVF in [TEST] database
--
-- 3 - Find row differences using except
--
-- declare/initialize variables
declare @stmt1 varchar(max) = '';
-- column lists
declare @list1 varchar(max) = dbo.get_column_list ('dbo', 't1', ', ');
declare @list2 varchar(max) = dbo.get_column_list ('dbo', 't2', ', ');
-- key (first), flag (last)
declare @key1 sysname = left(@list1, charindex(',', @list1, 1) - 1);
declare @key2 sysname = reverse(left(reverse(@list2), charindex(',', reverse(@list2), 1) - 2));
-- make dynamic sql
select @stmt1 = '(select ' + @list1 + ' from t2 except select ' + @list1 + ' from t1) as d1';
select @stmt1 = 'update t2 set ' + @key2 + ' = 1 where ' + @key1 + ' in (select d1.' + @key1 + ' from ' + @stmt1 + ');';
-- debug line
--print @stmt1;
-- execute the sql
exec (@stmt1);
go
--
-- 4 - Find the columns differences using case
--
-- declare/initialize variables
declare @stmt varchar(max) = '';
-- column list
declare @list varchar(max) = dbo.get_column_list ('dbo', 't2', ',');
-- key (first), flag (last)
declare @key1 sysname = left(@list, charindex(',', @list, 1) - 1);
declare @key2 sysname = reverse(left(reverse(@list), charindex(',', reverse(@list), 1) - 1));
-- select the changed items (skip key & flag)
select
@stmt += 'case when s.' + Item + ' = t.' + Item + ' then null else t.' + Item + ' end as val_' + Item + ', '
from
DelimitedSplit8K (@list, ',')
where
ItemNumber not in
(
select min(ItemNumber) as skip_vals from DelimitedSplit8K (@list, ',')
union
select max(ItemNumber) as skip_vals from DelimitedSplit8K (@list, ',')
);
-- complete the statement
select @stmt = 'insert into t3 select t.' + @key1 + ',' + substring(@stmt, 1, len(@stmt) - 1) + ' from t1 as s join t2 as t on s.' + @key1 + ' = t.' + @key1 + ' where t.' + @key2 + ' = 1 ';
-- debug line
--print @stmt;
-- execute the sql
exec (@stmt);
go