SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


trigger to write to two tables


trigger to write to two tables

Author
Message
briancampbellmcad
briancampbellmcad
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 428
Thanks in advance for any help with this!

I have a trigger that is successfully writing data to two different tables: tblTransactions & tblBulkPurchases... on the first run of an insert into tblTransactions I get a beautiful creation of the same in tblBulkPurchases... however on the second run of another insert into tblTransactions I get TWO identical records created in the tblBulkPurchases table... NOT GOOD! Is there a way of nesting or some other method to keep this from happening?

USE [TrackIT]
GO
CREATE TRIGGER [dbo].[trPopulateBulkPurchases]
ON [dbo].[tblTransactions]
AFTER INSERT
AS
BEGIN
INSERT INTO tblBulkPurchases
(tblBulkPurchases.PO_Number,
tblBulkPurchases.Buyer_Initial,
tblBulkPurchases.Quantity,
tblBulkPurchases.Transaction_Date,
tblBulkPurchases.Transaction_Type)
SELECT
tblTransactions.PO_Number,
tblTransactions.Buyer_Initial,
tblTransactions.Quantity,
tblTransactions.Transaction_Date,
tblTransactions.Transaction_Type
FROM tblTransactions
WHERE Transaction_Type = 'Bulk Purchase'
END
GO
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14175 Visits: 15963
Please will you post DDL for the two tables, including any primary key or unique constraints. Please also give an example of an INSERT statement.

Are you sure that after every insert into one table, you want to copy everything into the other table, and not just the rows you have inserted?

John
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28229 Visits: 39955
the trigger is using the base table, isntead of just the new rows that exist in the special virtual tables INSERTED AND DELETED

this will insert just the new rows:

CREATE TRIGGER [dbo].[trPopulateBulkPurchases]
ON [dbo].[tblTransactions]
AFTER INSERT
AS
BEGIN
INSERT INTO tblBulkPurchases
(tblBulkPurchases.PO_Number,
tblBulkPurchases.Buyer_Initial,
tblBulkPurchases.Quantity,
tblBulkPurchases.Transaction_Date,
tblBulkPurchases.Transaction_Type)
SELECT
INSERTED.PO_Number,
INSERTED.Buyer_Initial,
INSERTED.Quantity,
INSERTED.Transaction_Date,
INSERTED.Transaction_Type
FROM INSERTED
WHERE INSERTED.Transaction_Type = 'Bulk Purchase'
END
GO


edited: fixed table aliases:

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

briancampbellmcad
briancampbellmcad
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 428
I'm getting the following error for each field of the tblTransactions when I try to execute the T-SQL to create the trigger:

Msg 4104, Level 16, State 1, Procedure trPopulateBulkPurchases, Line ...
The multi-part identifier "tblTransactions.Transaction_Type" could not be bound.
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14175 Visits: 15963
Remove the column qualifiers from the SELECT list, or replace them with "Inserted".

John
briancampbellmcad
briancampbellmcad
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 428
Now it is working flawlessly by replacing the qualifiers with INSERTED. ... thanks all!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search