MISdad
Technical User
- May 5, 2004
- 34
I need some help. I wrote a trigger that will change a value in a table from Y to N and back again based on a record being inserted or updated in another table. My problem is that it doesn't seem to work consistently. It will change the value sometimes but not all the time. My frustration is in the fact that if it works on some records, it should work on all of them. I guess I need another set of eyes to look at this thing. Here is the detail, including the trigger code. Any help is greatly appreciated.
As you can see, there are 3 tables referenced. This is because there is not a direct link from the es_lockrecords table to the activwork table. The es_lockcolumns table is used to create the proper linking so that only the currently selected record is updated. Again, the problem is that this trigger only seems to run on some records and not all of them.
create trigger cd.t_eslockrecords on echo.es_lockrecords
for insert, update
as
declare @status char (2)
declare @lockrecordid char (18)
declare @lockcolumnvalue char (18)
declare @lockcolumnrid char (18)
select @lockrecordid = inserted.uniqueid_c from inserted
select @lockcolumnvalue = echo.es_lockcolumns.value_vc from
echo.es_lockcolumns
select @lockcolumnrid = echo.es_lockcolumns.es_lockrecordsid_c from
echo.es_lockcolumns
select @status = inserted.status_c from inserted
--This sets the printed_c field to 'Y'when the record is locked
if @status in ('L', 'A')
begin
update ar.activwork
set printed_c = 'Y'
where @lockrecordid = @lockcolumnrid and
ar.activwork.uniqueid_c = @lockcolumnvalue
end
else
--This sets the printed_c field to 'N' when the record is unlocked
if @status = 'IS'
begin
update ar.activwork
set printed_c = 'N'
where @lockrecordid = @lockcolumnrid and
ar.activwork.uniqueid_c = @lockcolumnvalue
end
As you can see, there are 3 tables referenced. This is because there is not a direct link from the es_lockrecords table to the activwork table. The es_lockcolumns table is used to create the proper linking so that only the currently selected record is updated. Again, the problem is that this trigger only seems to run on some records and not all of them.
create trigger cd.t_eslockrecords on echo.es_lockrecords
for insert, update
as
declare @status char (2)
declare @lockrecordid char (18)
declare @lockcolumnvalue char (18)
declare @lockcolumnrid char (18)
select @lockrecordid = inserted.uniqueid_c from inserted
select @lockcolumnvalue = echo.es_lockcolumns.value_vc from
echo.es_lockcolumns
select @lockcolumnrid = echo.es_lockcolumns.es_lockrecordsid_c from
echo.es_lockcolumns
select @status = inserted.status_c from inserted
--This sets the printed_c field to 'Y'when the record is locked
if @status in ('L', 'A')
begin
update ar.activwork
set printed_c = 'Y'
where @lockrecordid = @lockcolumnrid and
ar.activwork.uniqueid_c = @lockcolumnvalue
end
else
--This sets the printed_c field to 'N' when the record is unlocked
if @status = 'IS'
begin
update ar.activwork
set printed_c = 'N'
where @lockrecordid = @lockcolumnrid and
ar.activwork.uniqueid_c = @lockcolumnvalue
end