Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

why does this trigger find rows when I can't find them?

$
0
0

I've got a trigger that seems to find rows in the INSERTED table when none exist. I can't figure out what's going on. I've got a foreign key defined on PARCEL that does cascading updates on the APPEALS table. When I update the PARCEL table, it updates the key in the APPEALS table and that fires the trigger and displays what seems to me to be a spurious error message from the trigger.

Here's the definition of the trigger:

CREATE TRIGGER [dbo].[Appeal_Ruling_Affirmed]
ON [dbo].[Appeals]
FOR INSERT, UPDATE
AS
DECLARE @counter int, @seq int, @yr as int, @id as varchar(5)

IF EXISTS (select NULL from INSERTED where Ruling = 'AFFIRMED' AND TaxYear > 2008)
BEGIN

set @counter = (select count(*) from INSERTED where Ruling = 'AFFIRMED')
set @seq = (select top 1 SeqNo from INSERTED where Ruling = 'AFFIRMED')
set @id = (select top 1 LevelID from INSERTED where Ruling = 'AFFIRMED')
set @yr = (select top 1 TaxYear from INSERTED where Ruling = 'AFFIRMED')
PRINT 'Ruling = Affirmed, @counter = ' + convert(varchar(20), @counter) + ', LevelID = ' + @id +
', SeqNo = ' + convert(varchar(3), @seq) + ', TaxYear = ' + convert(char(4), @yr)

When I run the update statement,

UPDATE PARCEL set ParcelNo = '078 007.03' where ParcelNo = '078 007.03 000' and TaxYear = 2011

the trigger fires and displays:

Ruling = Affirmed, @counter = 1, LevelID = CB, SeqNo = 60, TaxYear = 2011

When I check the data in the table, here's what I get:

select TaxYear, LevelID, SeqNo, Ruling from Appeals where ParcelNo = '078 007.03 000' and TaxYear = 2011

TaxYear LevelID SeqNo Ruling
------- ------- ----- ----------
2011    AJ      100   REDUCED
2011    CB      60    INCREASED

So what's causing the trigger to fire? There aren't any rows in the table that's being modified that meet the criteria.

Thanks for any ideas on troubleshooting this.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>