复制数据并在目标中增加 PK table
Copy Data and increment PK in destination table
我有一个临时文件 table,其中的数据需要拆分为另外 3 个 table。这些 table 中的每一个都有一个主键,该主键不与彼此共享,也不与临时 table 共享。这是一个小样本:
Table 1
RSN AGENT STATUS STATUS DECRIPTION
0 280151 51 Terminated
1 86 57 C/O Comp Agent Dele
2 94 57 C/O Comp Agent Dele
3 108 51 Terminated
Table 2
RSN AGENT CITY
1 10 Englewood
2 123 Jackson
3 35 Eatontown
4 86 Trenton
Table 3
RSN AGT SIGN NO_EMP START_DATE
0 241008 Y 1 2002-10-31 00:00:00.000
1 86 Y 0 2002-10-24 09:51:10.247
2 94 Y 0 2002-10-24 09:51:10.247
3 108 Y 0 2002-10-24 09:51:10.247
我需要检查每个 table 以查看临时 table 中的数据是否存在,如果不存在,我想插入那些带有 RSN# 的行,以最大数字开头table。因此,如果我在第一个 table 中有 5000 条记录,并且我要添加 5000 个新行,它们的编号将从 5001 到 10000。
然后我需要检查是否有任何列已更改以匹配行并更新它们。
在此先感谢您的协助。
斯科特
您必须为 T1、2 和 3 重复以下代码并更新匹配列和不匹配列。
插入新值:
Insert Into Table1(col1, col2, ...)
Select t.col1, t.col2
From temp as t
Left Join table1 as t1 On t.matchcol1 = t1.matchcol1 and t.matchcol2 = t1.matchcol2
Where t.col1 is null
用 T 和 T1 之间的匹配列列表替换 matchcol1
更新:
Update t1 set col1 = t.col1, t.col2 = t1.col2, ...
From table1 as t1
Inner Join temp as t On t.matchcol1 = t1.matchcol1 and t.matchcol2 = t1.matchcol2 and ...
Where col1 <> t.col1 or t.col2 <> t1.col2 or ...
这可能也有效:
我不确定您是否真的需要更新某些内容或只是插入以及如何 link temp 和 table1 以了解它是否已更改。
Insert Into Table1(RSN, AGENT, STATUS, STATUS, DECRIPTION)
Select (Select max(RSN) From table1) + Row_number() over(order by agent)
, AGENT, STATUS, STATUS, DECRIPTION
From (
Select AGENT, STATUS, STATUS, DECRIPTION From TempTable
Except
Select AGENT, STATUS, STATUS, DECRIPTION From Table1
) as t1
或者您可以升级到 SQL Server 2008 并使用 Merge。会容易很多
我最终在暂存中添加了 4 个新专栏 table;一个临时 rsn#,它是一个以 1 开头的身份列,以及一个 rsn# 代表我的 3 个目的地 table 中的每一个。我创建了一个变量,从每个 table 中获取最大值,然后将其添加到我的临时 rsn# 中。
我有一个临时文件 table,其中的数据需要拆分为另外 3 个 table。这些 table 中的每一个都有一个主键,该主键不与彼此共享,也不与临时 table 共享。这是一个小样本:
Table 1
RSN AGENT STATUS STATUS DECRIPTION
0 280151 51 Terminated
1 86 57 C/O Comp Agent Dele
2 94 57 C/O Comp Agent Dele
3 108 51 Terminated
Table 2
RSN AGENT CITY
1 10 Englewood
2 123 Jackson
3 35 Eatontown
4 86 Trenton
Table 3
RSN AGT SIGN NO_EMP START_DATE
0 241008 Y 1 2002-10-31 00:00:00.000
1 86 Y 0 2002-10-24 09:51:10.247
2 94 Y 0 2002-10-24 09:51:10.247
3 108 Y 0 2002-10-24 09:51:10.247
我需要检查每个 table 以查看临时 table 中的数据是否存在,如果不存在,我想插入那些带有 RSN# 的行,以最大数字开头table。因此,如果我在第一个 table 中有 5000 条记录,并且我要添加 5000 个新行,它们的编号将从 5001 到 10000。
然后我需要检查是否有任何列已更改以匹配行并更新它们。
在此先感谢您的协助。
斯科特
您必须为 T1、2 和 3 重复以下代码并更新匹配列和不匹配列。
插入新值:
Insert Into Table1(col1, col2, ...)
Select t.col1, t.col2
From temp as t
Left Join table1 as t1 On t.matchcol1 = t1.matchcol1 and t.matchcol2 = t1.matchcol2
Where t.col1 is null
用 T 和 T1 之间的匹配列列表替换 matchcol1
更新:
Update t1 set col1 = t.col1, t.col2 = t1.col2, ...
From table1 as t1
Inner Join temp as t On t.matchcol1 = t1.matchcol1 and t.matchcol2 = t1.matchcol2 and ...
Where col1 <> t.col1 or t.col2 <> t1.col2 or ...
这可能也有效:
我不确定您是否真的需要更新某些内容或只是插入以及如何 link temp 和 table1 以了解它是否已更改。
Insert Into Table1(RSN, AGENT, STATUS, STATUS, DECRIPTION)
Select (Select max(RSN) From table1) + Row_number() over(order by agent)
, AGENT, STATUS, STATUS, DECRIPTION
From (
Select AGENT, STATUS, STATUS, DECRIPTION From TempTable
Except
Select AGENT, STATUS, STATUS, DECRIPTION From Table1
) as t1
或者您可以升级到 SQL Server 2008 并使用 Merge。会容易很多
我最终在暂存中添加了 4 个新专栏 table;一个临时 rsn#,它是一个以 1 开头的身份列,以及一个 rsn# 代表我的 3 个目的地 table 中的每一个。我创建了一个变量,从每个 table 中获取最大值,然后将其添加到我的临时 rsn# 中。