question

postonoh avatar image
postonoh asked

delete copy

I have a very simple stored procedure ALTER PROCEDURE [dbo].[pMBSolutionsNarrativeDelete] @pk_Narrative_ID int AS BEGIN DELETE [dbo].[Narrative] WHERE [Narrative_ID] = @pk_Narrative_ID END I created a trigger named delcopy ALTER TRIGGER [dbo].[delnTrigger] ON [dbo].[Narrative] FOR DELETE AS INSERT INTO NarrativeDelete (Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID) SELECT Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID FROM Narrative when I execute pMBSolutionsNarrativeDelete @pk_Narrative(record 1) it delete. doesn't copy(what and I doing wrong?)
trigger
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image
Matt Whitfield answered
You are referencing the unbounded main table, and you need to be referencing the `deleted` virtual table. I.e. Instead of INSERT INTO NarrativeDelete (Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID) SELECT Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID FROM Narrative You want INSERT INTO NarrativeDelete (Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID) SELECT Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID FROM deleted
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

postonoh avatar image postonoh commented ·
you are fast thanks fixed.
0 Likes 0 ·
Cyborg avatar image
Cyborg answered
If my assumption is correct then, you are trying to insert the deleted records of Narrative table to NarrativeDelete table (Just like Auditing). In that event you should use Deleted Table inside your trigger. its simple replace your Insert query with INSERT INTO NarrativeDelete (Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID) SELECT Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID FROM Deleted
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image
WilliamD answered
You need to change the select statement in the trigger to select that data from the pseudo-table deleted: ALTER TRIGGER [dbo].[delnTrigger] ON [dbo].[Narrative] FOR DELETE AS INSERT INTO NarrativeDelete (Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID) SELECT Narrative_ID, MonthYear, Location, SubContractor, SiteManager, Date, MonitorSummary, MIM, MIF, MAM, MAF, Project_ID, BidPackage_ID FROM deleted /* <-- here is the change */ The select you have up to now is trying to get the information that you have already deleted. The pseudo-table stores the information that runs from the trigger. There are the pseudo-tables `inserted` and `deleted` which can be accessed through `INSERT`, `UPDATE` and `DELETE` triggers.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.