我有一个触发器,该触发器调用一个数据库过程,该过程将所有更改记录到特定表中的每一列,但是当触发器触发时,我会得到一个错误
信息8114,第16级,状态5,过程dp_PolicyBenefit_audit,第0行
将数据类型nvarchar转换为int时出错。
对于INSERT
中的每一列,我都有一个UPDATE
和PolicyBenefit
触发器,总共100个-以下代码仅显示一个
drop procedure if exists dp_PolicyBenefit_audit
GO
create procedure dp_PolicyBenefit_audit
@p_policy_id varchar(20),
@p_id int,
@p_benefit_cd char(4),
@p_seq smallint,
@p_operation varchar(10),
@p_description varchar(100),
@p_old_value varchar(20),
@p_new_value varchar(20)
AS
if (@p_operation = 'INSERT')
begin
insert into PolicyBenefitAudit
(
PolicyNo,
EntityNo,
BenefitCode,
BenefitSequence,
Description,
OldValue,
NewValue,
CreatedBy,
CreatedDate
)
values
(
@p_policy_id,
@p_id,
@p_benefit_cd,
@p_seq,
@p_description,
null,
@p_new_value,
user_name(),
getdate()
)
end
if (@p_operation = 'UPDATE') and isnull(@p_old_value,'') != isnull(@p_new_value,''))
begin
insert into PolicyBenefitAudit
(
PolicyNo,
EntityNo,
BenefitCode,
BenefitSequence,
Description,
OldValue,
NewValue,
CreatedBy,
CreatedDate
)
values
(
@p_policy_id,
@p_id,
@p_benefit_cd,
@p_seq,
@p_description,
@p_old_value,
@p_new_value,
user_name(),
getdate()
)
end
GO
drop trigger if exists dt_PolicyBenefit_OccupationCode_upd
GO
create trigger dt_PolicyBenefit_OccupationCode_upd on PolicyBenefit
after update as
begin
declare
@old_OccupationCode varchar(100)
select @old_OccupationCode = cast(OccupationCode as varchar(100)) from deleted
exec dp_PolicyBenefit_audit @p_policy_id = PolicyNo,
@p_id = EntityNo,
@p_benefit_cd = BenefitCode,
@p_seq = BenefitSequence,
@p_operation = 'UPDATE',
@p_description = 'Occupational Per Mille loading duration (in years)',
@p_old_value = @old_OccupationCode,
@p_new_value = OccupationCode
end
GO
期望在PolicyBenefitAudit
表中插入行的过程
答案 0 :(得分:0)
错误在于将参数传递到您的sp中:
exec dp_PolicyBenefit_audit @p_policy_id = PolicyNo,
@p_id = EntityNo,
@p_benefit_cd = BenefitCode,
@p_seq = BenefitSequence,
@p_operation = 'UPDATE',
@p_description = 'Occupational Per Mille loading duration (in years)',
@p_old_value = @old_OccupationCode,
@p_new_value = OccupationCode
您的@p_id
被声明为int
,并且您将EntityNo
(字符串)传递给它。
这会导致“将数据类型nvarchar转换为int时出错”,EntityNo
是nvarchar。
答案 1 :(得分:0)
完全放弃了数据库过程,并在50个触发器中对其进行了编码,如下所示:
create trigger dt_PolicyBenefit_OccupationalPerMilleLoadingDuration on PolicyBenefit
after insert, update as
begin
insert into PolicyBenefitAudit
(
PolicyNo,
EntityNo,
BenefitCode,
BenefitSequence,
Description,
OldValue,
NewValue,
CreatedBy,
CreatedDate
)
select i.PolicyNo,
i.EntityNo,
i.BenefitCode,
i.BenefitSequence,
'Occupational Per Mille loading duration (in years)',
d.OccupationalPerMilleLoadingDuration,
i.OccupationalPerMilleLoadingDuration,
user_name(),
getdate()
from inserted i left join deleted d
on i.PolicyBenefitId = d.PolicyBenefitId
where i.OccupationalPerMilleLoadingDuration != d.OccupationalPerMilleLoadingDuration;
end
GO