2013-07-15 23 views
2

嗨我有以下存儲過程,使用用戶定義的表類型工作正常。從用戶定義的表更新多個表

ALTER procedure [dbo].[wl] 
@test [dbo].[testType] readonly 
as 
Begin 
merge into t_values as Target 
using @test as Source 
on Target.Id =Source.Id 
when matched then 
update set target.Num=Source.Num 
when not matched then 
insert (Id,Num) 
values (Source.Id, Source.Num); 
End 

我還有兩個表test2和test3,我需要從源代碼更新一些值。 我不知道我該怎麼做。 基本上我的目標是當一個條件匹配更新或插入值到所有三個表中,因爲外鍵關係。請讓我知道我該怎麼做。謝謝

+0

是Sql Server 2012嗎?爲什麼C#標籤?即使你用c#代碼來控制它,你的問題根本不涉及它。 – gunr2171

+0

雖然基於集合的邏輯通常是可取的,但有時間和地點可以使用遊標,而這可能是一個。遊標有時可以更易於閱讀和維護。如果您只需對幾行執行操作,則可以使光標僅選擇與多個目標表連接的源表的特定行。或者,如果您必須這樣做,您可以撤回陳舊的IF聲明。 – criticalfix

+0

@criticalfix,我必須強調:當涉及到大型數據集時,遊標非常緩慢。仔細選擇你的情況。 – gunr2171

回答

0

您需要使用「輸出」子句來捕獲插入.......插入後的行的「鍵」。

這是一個例子...不是一個確切的匹配....但顯示的想法。

http://granadacoder.wordpress.com/2008/12/10/sqlserver20052008-output-clause-in-insertupdatedelete-statements/

編輯:

這裏是我做了.......我在那裏插入一個「人」(父實體)和部分電子郵件地址(子實體)的例子。 我撕碎XML,你的數據表應該是類似的。

關鍵是我「審計」(與輸出子句)身份正在生成與我插入一個人.....但我保持這個信息與自然唯一標識符(SSN在這個例子中)。當我插入電子郵件地址時,我插入了我所插入的人員的IDENTITY。

我的#TempTables將是您的最終解決方案中的「真正的表格」。我的@VariableTable是收集和存儲由「輸出」子句收集的信息的「審計」表。

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 
+0

http://blogs.msdn.com/b/sqltips/archive/2005/06/13/output-clause.aspx – granadaCoder

+0

我知道這是你自己的博客,但它可能有助於在這裏總結代碼,讓你404。 – gunr2171

+0

http://msdn.microsoft.com/en-us/library/ms177564.aspx – granadaCoder