2014-06-18 74 views
0

我想刪除用戶(更新)我還有8個處理表,匹配表和futured表與用戶表相關。 我想如果我刪除(InActive)用戶然後必須從其他8表中刪除所有activitry我也寫了sp但我知道這是不正確的方式來做到這一點....我想使用連接(或其他方式)我怎麼能這樣做...請幫助我..謝謝加入多個表在sql server中刪除/更新

CREATE PROC [dbo].[bb_DeleteUser] 
    @userId int 
    AS 
    SET NOCOUNT ON 
    SET XACT_ABORT ON  

    BEGIN TRAN  

    UPDATE bb_user SET IsActive =0 WHERE user_id = @userId 

    IF EXISTS (SELECT * FROM DealA WHERE [user_id]= @userId) 
    BEGIN 
     update DealA 
     set [acq_veripro]= 'Del',[acq_status] = 'Closed',[acq_isclose] = 0,[acq_modifieddate]= Getdate() 
     where [user_id]= @userId 
    END 

    IF EXISTS (SELECT * FROM DealB WHERE [user_id]= @userId) 
    BEGIN  
     update DealB 
     set [fundfundraise_veripro]= 'Del',[fundfundraise_status] = 'Closed',[fundfundraise_isclose] = 0,[fundfundraise_modifieddate] = Getdate() 
     where [user_id]= @userId 
    END 

    IF EXISTS (SELECT * FROM DealC WHERE [user_id]= @userId) 
    BEGIN   
     update DealC 
     set [provider_veripro]= 'Del',[provider_status] = 'Closed',[provider_isclose] = 0,[provider_modifieddate] = Getdate() 
     where [user_id]= @userId 
    END    

    IF EXISTS (SELECT * FROM DealD WHERE [user_id]= @userId) 
    BEGIN 
     update DealD 
     set [seek_veripro]= 'Del',[seek_status] = 'Closed' ,[seek_isclose] = 0,[seek_modifieddate] = Getdate() 
     where [user_id]= @userId 
    END   

    IF EXISTS (SELECT * FROM DealE WHERE [user_id]= @userId) 
    BEGIN 
     update DealE 
     set [lpinvestor_veripro]= 'Del',[lpinvestor_status] = 'Closed',[lpinvestor_isclose] = 0,[lpinvestor_modifieddate]= Getdate() 
     where [user_id]= @userId 
    END 

    IF EXISTS (SELECT * FROM DealF WHERE [user_id]= @userId) 
    BEGIN 
     update DealF 
     set [sellseek_veripro]= 'Del',[sellseek_status] = 'Closed',[sellseek_isclose] = 0,[sellseek_modifieddate] = Getdate() 
     where [user_id]= @userId   
    END 

    IF EXISTS (SELECT * FROM Match WHERE Seeker_id [email protected] or [email protected] ) 
    BEGIN 
     update Match 
     set [IsActive]=0 ,[Match_ModifiedDate]=GETDATE() 
     where Seeker_id [email protected] or [email protected]  
    END 

    IF EXISTS (SELECT * FROM Featured_Deal WHERE Deal_User_Id= @userId ) 
    BEGIN 
     UPDATE Featured_Deal 
     set [Active_Status]= 0,[Modified_Date]= Getdate() 
     WHERE Deal_User_Id= @userId 
    END 

COMMIT 
+0

一旦停用用戶,我不明白爲什麼有必要更新任何其他表。但是再次,我不明白爲什麼每筆交易都需要單獨的表格。 –

+0

請不要這樣 - 爲什麼我們有很多交易表...只是想知道如何加入所有表刪除/更新? –

+1

你的問題很混亂。 – Rahul

回答

0

看看ON DELETE CASCADE。如果表格被正確引用,它將從其他表格中刪除相應的行。

假設您已聲明@user已經在原始代碼中更新有問題的用戶? 您可以加入的表像這樣的更新:

SET NOCOUNT ON 
GO 

UPDATE bb_user 
SET a.IsActive = 0, 
a.[acq_veripro]= 'Del', 
a.[acq_status] = 'Closed', 
a.[acq_isclose] = 0, 
a.[acq_modifieddate]= Getdate(), 
b.[fundfundraise_veripro]= 'Del', 
b.[fundfundraise_status] = 'Closed', 
b.[fundfundraise_isclose] = 0, 
b.[fundfundraise_modifieddate] = Getdate(), 
c.[provider_veripro]= 'Del', 
c.[provider_status] = 'Closed', 
c.[provider_isclose] = 0, 
c.[provider_modifieddate] = Getdate(), 
d.[seek_veripro]= 'Del', 
d.[seek_status] = 'Closed' , 
d.[seek_isclose] = 0, 
d.[seek_modifieddate] = Getdate() 


FROM DealA a 
    INNER JOIN DealB b 
    On a.user_id = b.user_id 
    INNER JOIN DealC 
    On a.user_id = c.user_id 
    INNER JOIN DealD 
    On a.user_id = d.user_id 
    where a.[user_id]= @userId 
+0

減號1.他正在嘗試更新,而不是刪除其他表中的記錄。 –

+0

上面的代碼將更新連接表中不刪除的字段。 – sz1

+1

謝謝sz1這是很好,我正在尋找這種解決方案....這將幫助我...再次感謝 –