2013-07-15 134 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 
相關問題