Saturday, 15 August 2015

I want triggers to delete from 2 tables in sql server -


I have 2 SQL Server tables (A and B).

I want to delete the following lines: A, when removing a line in B by using a trigger

and I want to delete the rows from it: B, when one is in Deleting a line leads to a trigger.

This is my trigger so far:

  Instead of creating / removing triggers deleted from DELETE B to B, WHERE B. Id = deleted.Id deleted from deleted A, deleted WHERE A.Id = deleted.Id  

and my second trigger:

  after Create the trigger after- Delete the A after the deletion after deletion, A. Aid = deleted when deleted. Id  

but it does not work correctly, I get a straightforward repetition (instead of triggers, do not support direct recursion. Trigger execution failure - this error).

How can I fix this?

I wanted to delete a hierarchy of records in a self-referenced table that had foreign key restrictions. Later using the Daily Trigger was not working as the foreign key restriction was already violated. The trigger did not work by using any INSTEAD because it did not support "direct recycling from inside the trigger" error, changing the setting directly and indirectly to the database did not have any effect. I could not find a solution where I only solution that one by records from the great depth with a number of uses CTE depth / level was to select all records in succession and avoid foreign key violations To remove it was to use the cursor. The example is triggered:

create triggers [dbo]. [DBO] at [relatedAccountindedDeliet] Start with [the related ACC] as the start of DELETE; - Log in to related accounts deleted from related accounts (select rye). [Guide], ra1.relatedAccount_guid related account ra1 Internal included Deleted d. [Guid] = ra1. [Guid] Union Select all ra1. [Guid ], Ral RelatedAccount RA1 joined relatedAccounts RA2 on relatedAccount_guid RA2 interior. [GUID] = ra1.relatedAccount_guid where ra1.relatedAccount_guid not null) select RA1 putting relatedAccount_history. * RelatedAccount RA1 to join the Inner RA2 with relatedAccounts RA2. [GUID] = RA1 [GUID]; - Remove the relevant bodies with the relevant authorities (Raia 1 pick. [Guid], ra1.relatedAccount_guid related Account Ral deleted add interior d. [Guid] = ra1. [Guid] Union Select all ra1. [Guid], ra1.relatedAccount_guid joined relatedAccounts RA2 on RA1 inner RA2 from relatedAccount. [GUID] = ra1.relatedAccount_guid where ra1.relatedAccount_guid not null) remove dbo.relatedEntity where [GUID] for (t1 selection. [GUID] from relatedAccounts D joining inner t1.relatedAccount_guid = d on dbo.relatedEntity T1. [Guid]); - Delete related accounts @guid uniqueidentifier; - Select the relatedAccounts_cursor cursor (related to 1 as selection, RA1 [GUID], associatedAccount to r1.relatedAccount_guid, RA1 inclusion on the inner d for associatedAccounts along with relatedAccounts as the selection of all accounts in any of your hierarchies. deleted D [GUID] = RA1 [... GUID] union all select joined on as ra2.depth + 1 depth, RA1 [GUID], ra1.relatedAccount_guid RA1 inner RA2 from relatedAccount. relatedAccounts RA2. [ GUID] = ra1.relatedAccount_guid where ra1.relatedAccount_guid is not empty) Selection of [GUID] with respect to the related digits in depth; - violate foreign key constraints open relatedAccounts_cursor while @@ fetch_status = 0 @guid bring next relatedAccounts_cursor initially delete dbo.relatedAccount delete accounts from the top down to avoid the [GUID] = @guid; The next arithmetic by related counting @Gangband related points related to related end; Related departments; END

No comments:

Post a Comment