MISdad
Technical User
- May 5, 2004
- 34
I'm creating a trigger to change a value in one table based on values in other tables. The problem I'm having is that there is not always a corresponding record in the other tables, and the trigger doesn't appear to assign a null value to the variable when that record doesn't exist. Any clues on what I can look for? Here's the code (sorry it's kind of long).
Thanks,
Jim
create trigger cd.t_activwork_update on ar.activwork
for insert,update
as
declare @activworkclientid char (18)
declare @activworkuniqueid char (18)
declare @clientuniqueid char (18)
declare @txgridclientid char (18)
declare @txplanexpdate datetime
declare @activitydate datetime
declare @program char (5)
declare @activity char (5)
declare @recipient char (5)
declare @assessmentdate datetime
declare @txoverride char (1)
declare @activplan char (1)
select @activworkclientid = inserted.clientid_c
from inserted
select @activworkuniqueid = inserted.uniqueid_c
from inserted
select @clientuniqueid = client.uniqueid_c
from ar.client client, inserted
where client.uniqueid_c = inserted.clientid_c
select @txgridclientid = txgrid.clientid_c
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
select @txplanexpdate = left(txgrid.exdate_dt, 11)
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
order by txgrid.exdate_dt
select @activitydate = inserted.activitydate_d
from inserted
select @program = inserted.program_c
from inserted
select @activity = inserted.activity_c
from inserted
select @recipient = inserted.recipient_c
from inserted
select @assessmentdate = left(presentprob.assess1_dt, 11)
from cd.presentprob presentprob, inserted
where presentprob.clientid_c = inserted.clientid_c
order by presentprob.assess1_dt
select @txoverride = txgrid.override_c
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
select @activplan = txgrid.activplan_c
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
--This will allow someone to override the NOTX
if @txoverride = 'N'
begin
-- This checks for MI programs and non crisis, assessment or tx plan
services Also checks to see if the
--service date is before the tx plan expiration date.
if @program in ('010', '030', '110', '120', '140', '150', '160', '210',
'220', '240',
'270', '330', '340', '350', '460', '550') and
@activity not in ('10R0', '13R0', '1023', 'IMR0', 'IQR0', 'IRR0',
'0MR0', '0QR0', '0CR0','0DR0', 'DROP', 'BAL0', 'ATV2',
'ATVT', 'CO1C', 'HA1C', 'HD1C', 'HAL5', 'LI25',
'PR1C', 'PF2C', 'BOOK') and
left(@recipient, 1) in ('1', '3') and
@recipient not in ('10NB', '10NP', '30NB')
-- This will update the activity being scheduled to NOTX because the Tx
Plan is expired
begin
update ar.activwork
set recipient_c = 'NOTX'
where (@activitydate > @txplanexpdate or @txplanexpdate <
left(getdate(), 11)) and
ar.activwork.uniqueid_c = @activworkuniqueid
-- This will update the activity being scheduled to NOTX because the Tx
Plan is not present within 45 days of assessment
update ar.activwork
set recipient_c = 'NOTX'
where @txplanexpdate is null and @assessmentdate < left(getdate() -
45, 11) and
ar.activwork.uniqueid_c = @activworkuniqueid
end
else
--This checks for SA programs and non crisis, assessment or tx plan
services. Also checks to see if the
--service date is before the tx plan expiration date.
if @program in ('080', '140','290', '570', '600', '610', '620', '640',
'660', '690', '730') and
@activity not in ('1000', 'INTA', '0003', '0005', 'DROP', 'BAL0',
'BOOK') and
left(@recipient, 1) in ('1', '3') and
@recipient not in ('10NB', '10NP', '30NB', '30NP', '1EIG', '1EII')
--This will update the activity being scheduled to NOTX because the TX
Plan is expired or has no end date
begin
update ar.activwork
set recipient_c = 'NOTX'
where (@activitydate > @txplanexpdate or @txplanexpdate is null or
@txplanexpdate < left(getdate(), 11))
and ar.activwork.uniqueid_c = @activworkuniqueid
end
--This is the end statement for the override statement above
end
Thanks,
Jim
create trigger cd.t_activwork_update on ar.activwork
for insert,update
as
declare @activworkclientid char (18)
declare @activworkuniqueid char (18)
declare @clientuniqueid char (18)
declare @txgridclientid char (18)
declare @txplanexpdate datetime
declare @activitydate datetime
declare @program char (5)
declare @activity char (5)
declare @recipient char (5)
declare @assessmentdate datetime
declare @txoverride char (1)
declare @activplan char (1)
select @activworkclientid = inserted.clientid_c
from inserted
select @activworkuniqueid = inserted.uniqueid_c
from inserted
select @clientuniqueid = client.uniqueid_c
from ar.client client, inserted
where client.uniqueid_c = inserted.clientid_c
select @txgridclientid = txgrid.clientid_c
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
select @txplanexpdate = left(txgrid.exdate_dt, 11)
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
order by txgrid.exdate_dt
select @activitydate = inserted.activitydate_d
from inserted
select @program = inserted.program_c
from inserted
select @activity = inserted.activity_c
from inserted
select @recipient = inserted.recipient_c
from inserted
select @assessmentdate = left(presentprob.assess1_dt, 11)
from cd.presentprob presentprob, inserted
where presentprob.clientid_c = inserted.clientid_c
order by presentprob.assess1_dt
select @txoverride = txgrid.override_c
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
select @activplan = txgrid.activplan_c
from cd.txplangrid txgrid, inserted
where txgrid.clientid_c = inserted.clientid_c
--This will allow someone to override the NOTX
if @txoverride = 'N'
begin
-- This checks for MI programs and non crisis, assessment or tx plan
services Also checks to see if the
--service date is before the tx plan expiration date.
if @program in ('010', '030', '110', '120', '140', '150', '160', '210',
'220', '240',
'270', '330', '340', '350', '460', '550') and
@activity not in ('10R0', '13R0', '1023', 'IMR0', 'IQR0', 'IRR0',
'0MR0', '0QR0', '0CR0','0DR0', 'DROP', 'BAL0', 'ATV2',
'ATVT', 'CO1C', 'HA1C', 'HD1C', 'HAL5', 'LI25',
'PR1C', 'PF2C', 'BOOK') and
left(@recipient, 1) in ('1', '3') and
@recipient not in ('10NB', '10NP', '30NB')
-- This will update the activity being scheduled to NOTX because the Tx
Plan is expired
begin
update ar.activwork
set recipient_c = 'NOTX'
where (@activitydate > @txplanexpdate or @txplanexpdate <
left(getdate(), 11)) and
ar.activwork.uniqueid_c = @activworkuniqueid
-- This will update the activity being scheduled to NOTX because the Tx
Plan is not present within 45 days of assessment
update ar.activwork
set recipient_c = 'NOTX'
where @txplanexpdate is null and @assessmentdate < left(getdate() -
45, 11) and
ar.activwork.uniqueid_c = @activworkuniqueid
end
else
--This checks for SA programs and non crisis, assessment or tx plan
services. Also checks to see if the
--service date is before the tx plan expiration date.
if @program in ('080', '140','290', '570', '600', '610', '620', '640',
'660', '690', '730') and
@activity not in ('1000', 'INTA', '0003', '0005', 'DROP', 'BAL0',
'BOOK') and
left(@recipient, 1) in ('1', '3') and
@recipient not in ('10NB', '10NP', '30NB', '30NP', '1EIG', '1EII')
--This will update the activity being scheduled to NOTX because the TX
Plan is expired or has no end date
begin
update ar.activwork
set recipient_c = 'NOTX'
where (@activitydate > @txplanexpdate or @txplanexpdate is null or
@txplanexpdate < left(getdate(), 11))
and ar.activwork.uniqueid_c = @activworkuniqueid
end
--This is the end statement for the override statement above
end