Sql 看起来像…嗨,@gbtimmon提供的表格有效吗?谢谢你的反馈。我很好奇为什么要在CASE语句和WHERE子句中比较列?WHERE子句中的比较是否足够?这样我们就只使用T2中不同于T1的值填充T3。如果我们不做这些病例,那么T3在匹配T1或不匹配T1时,将
Sql 看起来像…嗨,@gbtimmon提供的表格有效吗?谢谢你的反馈。我很好奇为什么要在CASE语句和WHERE子句中比较列?WHERE子句中的比较是否足够?这样我们就只使用T2中不同于T1的值填充T3。如果我们不做这些病例,那么T3在匹配T1或不匹配T1时,将,sql,sql-server,sql-server-2008,tsql,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008,Tsql,Sql Server 2008 R2,看起来像…嗨,@gbtimmon提供的表格有效吗?谢谢你的反馈。我很好奇为什么要在CASE语句和WHERE子句中比较列?WHERE子句中的比较是否足够?这样我们就只使用T2中不同于T1的值填充T3。如果我们不做这些病例,那么T3在匹配T1或不匹配T1时,将具有与T2相同的值。任何一种解决方案都可能有效,这将取决于您打算如何使用T3。哦,如果T2列中至少有一列与T1列中的至少一列不匹配,我想插入T2到T3的所有列……我还需要CaSE语句吗。我想我看到了你在这个案例背后的逻辑。但它和我在这篇评论中发
看起来像…嗨,@gbtimmon提供的表格有效吗?谢谢你的反馈。我很好奇为什么要在CASE语句和WHERE子句中比较列?WHERE子句中的比较是否足够?这样我们就只使用T2中不同于T1的值填充T3。如果我们不做这些病例,那么T3在匹配T1或不匹配T1时,将具有与T2相同的值。任何一种解决方案都可能有效,这将取决于您打算如何使用T3。哦,如果T2列中至少有一列与T1列中的至少一列不匹配,我想插入T2到T3的所有列……我还需要CaSE语句吗。我想我看到了你在这个案例背后的逻辑。但它和我在这篇评论中发布的内容是否一致?然后用一个精选的splat.holy cow!替换这个案例!。。。嗯,你能告诉我(保留这篇帖子:)如何插入所有列吗?我想要的不是5-NULL-NULL-NULL-NULL-K,而是5-10-20-I-K…唯一的要求是第1列是键,第n列(最后一列)是表2中的标志。您需要同时安装my helper函数和jeff moden的split函数。继续,将表更改为有10列,仍然有效。-很高兴,我在之前对gbtimmon的帖子的评论中添加了固定要求,但没有更新op。我会更新它。sry。
-- 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|
-- 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