[英]Insert identity column value into table from another table?
create table #test (a int identity(1,1), b varchar(20), c varchar(20))
insert into #test (b,c) values ('bvju','hjab')
insert into #test (b,c) values ('bst','sdfkg')
......
insert into #test (b,c) values ('hdsj','kfsd')
如何将从上面的插入语句填充的标识值( #test.a
)插入#sample
表(另一个表)
create table #sample (d int identity(1,1), e int, f varchar(20))
insert into #sample(e,f) values (identity value from #test table, 'jkhjk')
insert into #sample(e,f) values (identity value from #test table, 'hfhfd')
......
insert into #sample(e,f) values (identity value from #test table, 'khyy')
任何人都可以解释一下如何为更大的记录集(数千条记录)实现这一点吗?
我们可以使用while
循环和scope_identity
吗? 如果是这样,请说明我们该怎么办?
如果我从选择查询中插入#test会是什么情况?
插入#test(b,c)select ... from ...(数千条记录)
我如何捕获身份值并将该值用于另一个(#sample)插入#sample(e,f)select(来自#test的身份值),......来自......(千条记录) -
您可以使用output
子句。 从文档(强调我的):
OUTPUT子句从INSERT,UPDATE,DELETE或MERGE语句影响的每一行返回信息或基于表达式的表达式。 这些结果可以返回到处理应用程序,以用于确认消息,存档和其他此类应用程序要求。 结果也可以插入表或表变量中。 此外,您可以在嵌套的INSERT,UPDATE,DELETE或MERGE语句中捕获OUTPUT子句的结果,并将这些结果插入目标表或视图中。
像这样:
create table #tempids (a int) -- a temp table for holding our identity values
insert into #test
(b,c)
output inserted.a into #tempids -- put the inserted identity value into #tempids
values
('bvju','hjab')
然后你问......
如果插入来自选择,该怎么办?
它的工作方式相同......
insert into #test
(b,c)
output inserted.a into #tempids -- put the inserted identity value into #tempids
select -- except you use a select here
Column1 ,Column2 from SomeSource
无论是从值,派生表,执行语句,dml表源还是默认值插入,它的工作方式都相同。 如果您插入1000条记录,您将在#tempids
获得1000个ID。
我刚刚用输出子句写了一个“基于集合”的样本。
这里是。
IF OBJECT_ID('tempdb..#DestinationPersonParentTable') IS NOT NULL
begin
drop table #DestinationPersonParentTable
end
IF OBJECT_ID('tempdb..#DestinationEmailAddressPersonChildTable') IS NOT NULL
begin
drop table #DestinationEmailAddressPersonChildTable
end
CREATE TABLE #DestinationPersonParentTable
(
PersonParentSurrogateIdentityKey int not null identity (1001, 1),
SSNNaturalKey int,
HireDate datetime
)
declare @PersonOutputResultsAuditTable table
(
SSNNaturalKey int,
PersonParentSurrogateIdentityKeyAudit int
)
CREATE TABLE #DestinationEmailAddressPersonChildTable
(
DestinationChildSurrogateIdentityKey int not null identity (3001, 1),
PersonParentSurrogateIdentityKeyFK int,
EmailAddressValueNaturalKey varchar(64),
EmailAddressType int
)
-- Declare XML variable
DECLARE @data XML;
-- Element-centered XML
SET @data = N'
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Person>
<SSN>222222222</SSN>
<HireDate>2002-02-02</HireDate>
</Person>
<Person>
<SSN>333333333</SSN>
<HireDate>2003-03-03</HireDate>
</Person>
<EmailAddress>
<SSNLink>222222222</SSNLink>
<EmailAddressValue>g@g.com</EmailAddressValue>
<EmailAddressType>1</EmailAddressType>
</EmailAddress>
<EmailAddress>
<SSNLink>222222222</SSNLink>
<EmailAddressValue>h@h.com</EmailAddressValue>
<EmailAddressType>2</EmailAddressType>
</EmailAddress>
<EmailAddress>
<SSNLink>333333333</SSNLink>
<EmailAddressValue>a@a.com</EmailAddressValue>
<EmailAddressType>1</EmailAddressType>
</EmailAddress>
<EmailAddress>
<SSNLink>333333333</SSNLink>
<EmailAddressValue>b@b.com</EmailAddressValue>
<EmailAddressType>2</EmailAddressType>
</EmailAddress>
</root>
';
INSERT INTO #DestinationPersonParentTable ( SSNNaturalKey , HireDate )
output inserted.SSNNaturalKey , inserted.PersonParentSurrogateIdentityKey into @PersonOutputResultsAuditTable ( SSNNaturalKey , PersonParentSurrogateIdentityKeyAudit)
SELECT T.parentEntity.value('(SSN)[1]', 'INT') AS SSN,
T.parentEntity.value('(HireDate)[1]', 'datetime') AS HireDate
FROM @data.nodes('root/Person') AS T(parentEntity)
/* add a where not exists check on the natural key */
where not exists (
select null from #DestinationPersonParentTable innerRealTable where innerRealTable.SSNNaturalKey = T.parentEntity.value('(SSN)[1]', 'INT') )
;
/* Optional. You could do a UPDATE here based on matching the #DestinationPersonParentTableSSNNaturalKey = T.parentEntity.value('(SSN)[1]', 'INT')
You could Combine INSERT and UPDATE using the MERGE function on 2008 or later.
*/
select 'PersonOutputResultsAuditTable_Results' as Label, * from @PersonOutputResultsAuditTable
INSERT INTO #DestinationEmailAddressPersonChildTable ( PersonParentSurrogateIdentityKeyFK , EmailAddressValueNaturalKey , EmailAddressType )
SELECT par.PersonParentSurrogateIdentityKeyAudit ,
T.childEntity.value('(EmailAddressValue)[1]', 'varchar(64)') AS EmailAddressValue,
T.childEntity.value('(EmailAddressType)[1]', 'INT') AS EmailAddressType
FROM @data.nodes('root/EmailAddress') AS T(childEntity)
/* The next join is the "trick". Join on the natural key (SSN)....**BUT** insert the PersonParentSurrogateIdentityKey into the table */
join @PersonOutputResultsAuditTable par on par.SSNNaturalKey = T.childEntity.value('(SSNLink)[1]', 'INT')
where not exists (
select null from #DestinationEmailAddressPersonChildTable innerRealTable where innerRealTable.PersonParentSurrogateIdentityKeyFK = par.PersonParentSurrogateIdentityKeyAudit AND innerRealTable.EmailAddressValueNaturalKey = T.childEntity.value('(EmailAddressValue)[1]', 'varchar(64)'))
;
print '/#DestinationPersonParentTable/'
select * from #DestinationPersonParentTable
print '/#DestinationEmailAddressPersonChildTable/'
select * from #DestinationEmailAddressPersonChildTable
select SSNNaturalKey , HireDate , '---' as Sep1 , EmailAddressValueNaturalKey , EmailAddressType , '---' as Sep2, par.PersonParentSurrogateIdentityKey as ParentPK , child.PersonParentSurrogateIdentityKeyFK as childFK from #DestinationPersonParentTable par join #DestinationEmailAddressPersonChildTable child
on par.PersonParentSurrogateIdentityKey = child.PersonParentSurrogateIdentityKeyFK
IF OBJECT_ID('tempdb..#DestinationPersonParentTable') IS NOT NULL
begin
drop table #DestinationPersonParentTable
end
IF OBJECT_ID('tempdb..#DestinationEmailAddressPersonChildTable') IS NOT NULL
begin
drop table #DestinationEmailAddressPersonChildTable
end
insert into #test (b,c) values ('bvju','hjab')
insert into #sample(e,f) values (@SCOPE_IDENTITY(), 'jkhjk')
@SCOPE_IDENTITY()返回使用的最后一个标识值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.