2013-07-15 96 views
3
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') 

我将如何插入得到了来自上面的INSERT语句填充到#sample表(另一个表)的标识值(#test.a将标识列值从另一个表中插入表中?

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 loop和scope_identity?如果是这样,请解释我们该怎么做?

如果我从选择查询中插入#test,情况会是怎样?

INSERT INTO #TEST(B,C) 选择... ...从(几千条记录)

我将如何捕捉标识值并使用该值到另一个(#sample) 插入到#sample(E,F) 选择(身份从#TEST值),...从...(记录千元) - )

+0

我已更新我的回答,以解决您删除的评论中的问题。 – canon

回答

6

您可以使用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。

0
insert into #test (b,c) values ('bvju','hjab') 
insert into #sample(e,f) values (@SCOPE_IDENTITY(), 'jkhjk') 

@SCOPE_IDENTITY(返回的最后一个标识值使用

+1

使用@SCOPE_IDENTITY()它会返回最后一个标识值,但是如何从#test表中捕获所有标识记录? – user2584834

+0

您需要使用游标一次执行一个游标,以便插入表A,然后插入相同ID的表B.佳能的方法虽然更好,因为它是基于集合的操作,并且效率更高。 – AaronLS

0

我刚刚用output子句写了一个“基于集合”的示例。

这是它。

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>[email protected]</EmailAddressValue> 
     <EmailAddressType>1</EmailAddressType> 
    </EmailAddress> 

    <EmailAddress> 
     <SSNLink>222222222</SSNLink> 
     <EmailAddressValue>[email protected]</EmailAddressValue> 
     <EmailAddressType>2</EmailAddressType> 
    </EmailAddress> 

    <EmailAddress> 
     <SSNLink>333333333</SSNLink> 
     <EmailAddressValue>[email protected]</EmailAddressValue> 
     <EmailAddressType>1</EmailAddressType> 
    </EmailAddress> 

    <EmailAddress> 
     <SSNLink>333333333</SSNLink> 
     <EmailAddressValue>[email protected]</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 
相关问题