0

我有 3 个具有相同 5 列的表(+ 1 个 T2 中的标志列,我们稍后将使用它......让我们关注这个示例的 5 列)。T1、T2 和 T3。

所有三个表中的第一列都是关键列。

假设 T1 和 T2 中有 5 条记录。T1 和 T2 中的 4 条记录与所有列匹配。第 5 条记录有 4 个匹配列(1 个键 + 3 个非键)。这意味着,T1 和 T2 对于第 5 条记录有 1 个不匹配的非键列。

我不想对前 4 列执行任何操作,我想将第 5 列从 T2 插入到 T3,并将 T2 的第 6 列更新为 TRUE。

如何做到这一点?合并查询不起作用,因为据我所知,它仅适用于 2 个表......如果我错了,请纠正我。

请注意:这些表实际上有超过 100 列(相同的列)sooo...呵呵

比X

更新:我仍然希望将前 4 列的值从 T2 传递到 T3 ......而不仅仅是改变的列。

4

2 回答 2

1
-- Not tuned to any particular syntax, may need to be tweaked. 
T1([a],b,c,d,e);
T2([a],b,c,d,e,f); 
T3([a],b,c,d,e);

---------
-- SQL --
---------
BT;

INSERT INTO T3
SELECT
   T2.a,
   CASE WHEN (T1.b <> T2.b) THEN T2.b ELSE null,
   CASE WHEN (T1.c <> T2.c) THEN T2.c ELSE null,
   CASE WHEN (T1.d <> T2.d) THEN T2.d ELSE null,
   CASE WHEN (T1.e <> T2.e) THEN T2.e ELSE null
FROM 
   T1,
   T2
WHERE T1.a = T2.a
  AND (
      T1.b <> T2.b
   OR T1.c <> T2.c
   OR T1.d <> T2.d
   OR T1.e <> T2.e
);

UPDATE T2
SET T2.f = true
T1.a IN (
    SEL T1.a
    FROM T1, T2
    WHERE T1.a = T2.a
    AND (
        T1.b <> T2.b
     OR T1.c <> T2.c
     OR T1.d <> T2.d
     OR T1.e <> T2.e
    )
);

ET; -- OR COMMIT depending on sytax

示例数据

 -- BEFORE --                      -- AFTER --
T1                          T1
|[a]| b | c | d | e |       |[a]| b | c | d | e | 
|---+---+---+---+---|       |---+---+---+---+---|
| 0 | 1 | 2 | 3 | 4 |       | 0 | 1 | 2 | 3 | 4 |
| 1 | 2 | 3 | 4 | 5 |       | 1 | 2 | 3 | 4 | 5 |
| 1 | 2 | 3 | 4 | 5 |       | 1 | 2 | 3 | 4 | 5 |
| 3 | 4 | 5 | 6 | 7 |       | 3 | 4 | 5 | 6 | 7 |
| 4 | 5 | 6 | 7 | 8 |       | 4 | 5 | 6 | 7 | 8 |

T2                          T2
|[a]| b | c | d | e | f |   |[a]| b | c | d | e | f |
|---+---+---+---+---+---|   |---+---+---+---+---+---|
| 0 | 1 | 2 | 3 | 4 | f |   | 0 | 1 | 2 | 3 | 4 | f |
| 1 | 2 | 3 | 4 |-3 | f |   | 1 | 2 | 3 | 4 |-3 | t |
| 2 | 3 | 4 | 5 | 6 | f |   | 2 | 3 | 4 | 5 | 6 | f |
| 3 | 4 | 5 |-5 | 7 | f |   | 3 | 4 | 5 |-5 | 7 | t |
| 4 | 5 | 6 | 7 | 8 | f |   | 4 | 5 | 6 | 7 | 8 | f |

T3                          T3   
|[a]| b | c | d | e |       |[a]| b | c | d | e |
|---+---+---+---+---|       |---+---+---+---+---|
                            | 1 |nul|nul|nul|-3 |
                            | 3 |nul|nul|-5 |nul|
于 2013-08-13T17:10:07.260 回答
1

该解决方案适用于五列。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

在此处输入图像描述

于 2013-08-13T20:29:31.670 回答