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


trigger that will update one table when a record is updated in another


trigger that will update one table when a record is updated in another

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
I'm trying to write a trigger that will update one table [tblTransactions] when a record is updated in another [tblBulkPurchases].... am I on the right track here?

USE [TrackIT]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[trUpdateBulkPurchases]
ON [dbo].[tblTransactions]
AFTER UPDATE
AS
BEGIN
UPDATE tblBulkPurchases
(tblBulkPurchases.PO_Number,
tblBulkPurchases.Quantity,
tblBulkPurchases.Transaction_Number,
tblBulkPurchases.Quantity_Remaining,
tblBulkPurchases.Unit_Price,
tblBulkPurchases.Software_Description,
tblBulkPurchases.PO_Date,
tblBulkPurchases.PurchaseCostCenter,
tblBulkPurchases.HeatTicketNumber,
tblBulkPurchases.PurchaseAccount,
tblBulkPurchases.Transaction_Date,
tblBulkPurchases.Transaction_Type,
tblBulkPurchases.SoftwareShortName)
SELECT
INSERTED.PO_Number,
INSERTED.Quantity,
INSERTED.Transaction_Number,
INSERTED.Quantity,
INSERTED.Unit_Price,
INSERTED.Software_Description,
INSERTED.PO_Date,
INSERTED.PurchaseCostCenter,
INSERTED.HeatTicketNumber,
INSERTED.PurchaseAccount,
INSERTED.Transaction_Date,
INSERTED.Transaction_Type,
INSERTED.SoftwareShortName
FROM INSERTED
WHERE Transaction_Type = 'Bulk Purchase'
END
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27994 Visits: 39924
doesn't look quite right to me.

It looks to me like you are missing a relationship between t he two tables.
maybe the PO_Number exists between the two table,s and you need to update WHERE PO_NUMBER = INSERTED.PO_Number AND Transaction_Type = 'Bulk Purchase' ?

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
Correct... do need to associate by PO = PO... thanks.... is the word INSERTED.____ appropriate to use?
Steve Jones
Steve Jones
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: Administrators
Points: 61803 Visits: 19099
INSERTED.___ is fine.

I'd do this, but your way works:


UPDATE tblBulkPurchases
(tblBulkPurchases.PO_Number,
tblBulkPurchases.Quantity,
tblBulkPurchases.Transaction_Number,
tblBulkPurchases.Quantity_Remaining,
tblBulkPurchases.Unit_Price,
tblBulkPurchases.Software_Description,
tblBulkPurchases.PO_Date,
tblBulkPurchases.PurchaseCostCenter,
tblBulkPurchases.HeatTicketNumber,
tblBulkPurchases.PurchaseAccount,
tblBulkPurchases.Transaction_Date,
tblBulkPurchases.Transaction_Type,
tblBulkPurchases.SoftwareShortName)
SELECT
i.PO_Number,
i.Quantity,
i.Transaction_Number,
i.Quantity,
...
FROM INSERTED i
inner join tblBulkPurchases
on tblBulkPurchases.PO = i.po
WHERE Transaction_Type = 'Bulk Purchase'



Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
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
Kinda like your way... but the compiler doesn't like where I've placed astericks:

USE [TrackIT]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[trUpdateBulkPurchases]
ON [dbo].[tblTransactions]
AFTER UPDATE
AS
BEGIN
UPDATE tblBulkPurchases
*(tblBulkPurchases.PO_Number,
tblBulkPurchases.Quantity,
tblBulkPurchases.Transaction_Number,
tblBulkPurchases.Quantity_Remaining,
tblBulkPurchases.Unit_Price,
tblBulkPurchases.Software_Description,
tblBulkPurchases.PO_Date,
tblBulkPurchases.PurchaseCostCenter,
tblBulkPurchases.HeatTicketNumber,
tblBulkPurchases.PurchaseAccount,
tblBulkPurchases.Transaction_Date,
tblBulkPurchases.Transaction_Type,
tblBulkPurchases.SoftwareShortName)
SELECT
i.PO_Number,
i.Quantity,
i.Transaction_Number,
i.Quantity,
i.Unit_Price,
i.Software_Description,
i.PO_Date,
i.PurchaseCostCenter,
i.HeatTicketNumber,
i.PurchaseAccount,
i.Transaction_Date,
i.Transaction_Type,
i.SoftwareShortName
FROM INSERTED i
inner join tblBulkPurchases
on tblBulkPurchases.Transaction_Number = i.Transaction_Number
WHERE Transaction_Type = 'Bulk Purchase' *
GO
Steve Jones
Steve Jones
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: Administrators
Points: 61803 Visits: 19099
Sorry, wasn't reading well. The problem is that you have a malformed UPDATE statement.


CREATE TRIGGER [dbo].[trUpdateBulkPurchases]
ON [dbo].[tblTransactions]
AFTER UPDATE
AS
BEGIN
UPDATE tblBulkPurchases
set PO_Number = i.PO_Number
, Quantity = i.quantity
...
FROM INSERTED i
inner join tblBulkPurchases
on tblBulkPurchases.Transaction_Number = i.Transaction_Number
WHERE Transaction_Type = 'Bulk Purchase'
GO




Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
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
Like this? (Still doesn't like near where the * is):

USE [TrackIT]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[trUpdateBulkPurchases]
ON [dbo].[tblTransactions]
AFTER UPDATE
AS
BEGIN
UPDATE tblBulkPurchases
SET
PO_Number = i.PO_Number,
Quantity = i.Quantity,
Unit_Price = i.Unit_Price,
Software_Description = i.Software_Description,
PO_Date = i.PO_Date,
PurchaseCostCenter = i.PurchaseCostCenter,
HeatTicketNumber = i.HeatTicketNumber,
PurchaseAccount = i.PurchaseAccount,
Transaction_Date = i.Transaction_Date,
SoftwareShortName = i.SoftwareShortName
FROM INSERTED i
inner join tblBulkPurchases
on tblBulkPurchases.Transaction_Number = i.Transaction_Number
WHERE Transaction_Type = 'Bulk Purchase' *
GO
Steve Jones
Steve Jones
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: Administrators
Points: 61803 Visits: 19099
What's the error? If it's an ambiguous column, you need to specify from which table (The base one or INSERTED) you are specifying in the WHERE clause.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
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
WHERE Transaction_Type = 'Bulk Purchase'

Msg 102, Level 15, State 1, Procedure trUpdateBulkPurchases, Line 21
Incorrect syntax near 'Bulk Purchase'.
toddasd
toddasd
SSChasing Mays
SSChasing Mays (624 reputation)SSChasing Mays (624 reputation)SSChasing Mays (624 reputation)SSChasing Mays (624 reputation)SSChasing Mays (624 reputation)SSChasing Mays (624 reputation)SSChasing Mays (624 reputation)SSChasing Mays (624 reputation)

Group: General Forum Members
Points: 624 Visits: 3818
briancampbellmcad (1/14/2013)
Like this? (Still doesn't like near where the * is):
...
WHERE Transaction_Type = 'Bulk Purchase' *
GO


Yes, like that but no asterix. You won't be using an * here.

______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
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