I have a table Test
with columns
InveCode varchar(50)
itemid numeric(2)
rowid numeric identity,
purdate date
I have a trigger on this table.
Create trigger [dbo].[TrgInveCode]
on [dbo].[test]
after insert
as
declare @itemid numeric(10)
declare @total numeric(10)
declare @rowid numeric(18)
declare @Date date
select @itemid = i.itemid from inserted i
select @rowid = i.rowid from inserted i
select @Date = i.purdate from inserted i
select @total = count(itemid)
from test
where itemid = @itemid and year(purdate) = year(@date)
update test
set invecode= cast(@itemid as varchar(50)) + '-' + cast(year(@Date) as varchar(50)) + '-' + cast(@total as varchar(50))
where rowid = @rowid
When I use this command
insert into test(itemid, purdate, buyingyear)
output inserted.invecode
values(@itemid, @purdate, @buyingyear)
in vb.net, I get this error:
The targer table 'test' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.
UPDATE
statements affects 25 rows, you'll get the trigger fired once, but thenInserted
andDeleted
will each contain 25 rows. WHich of those 25 rows will your code select here??SELECT i.RowId FROM Inserted i
- it's non-deterministic, you'll get an arbitrary row, and the other 24 will be ignored. You need to rewrite your trigger to take this into account! - marc_sOUTPUT
clause in that way. Either useOUTPUT inserted.invecode INTO .........
or don't useOUTPUT
at all - marc_s