Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger not working

Status
Not open for further replies.

FrankPV

Technical User
Oct 11, 2000
20
US
I am trying to implement referential integrity using a trigger because I have two related tables in two different databases. The table which holds the trigger is the invoices table in my Hardgoods database. This table is related to invoice items in the same database with the invcnum field and referential integrity is established between these two tables supporting cascading updates and deletes. I would like the trigger to perform the same function between the invoices table and the invoice_items table in the flowersSQL database using the trigger. The trigger as written below is not working:

ALTER TRIGGER trg_Update_Invoice_Items_Flowers
ON dbo.Invoices
FOR UPDATE
AS

DECLARE @rows_affected int, @invcnumchg_before nvarchar, @invcnumchg_after nvarchar
SELECT @rows_affected = @@ROWCOUNT
IF @rows_affected = 0
RETURN /*No rows changed, exit trigger */
IF UPDATE (INVCNUM)
BEGIN
IF @rows_affected = 1
BEGIN
SELECT @invcnumchg_before = invcnum FROM deleted
SELECT @invcnumchg_after = invcnum FROM inserted
UPDATE flowersSQL.dbo.Invoice_Items
SET invcnum = @invcnumchg_after
WHERE invcnum = @invcnumchg_before
END
ELSE
BEGIN
RAISERROR ('Cannot update more than 1 row.', 16, 1)
ROLLBACK TRAN
RETURN
END
END

The trigger appears to be firing but the invcnum field in the invoice_items table in flowersSQL is not being updated. Could it be because there is already referential integrity between the invoices table and invoice_items table in the Hardgoods database? If so, do I need to remove referential integrity between these two tables and implement with another trigger?

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top