Sql server 2005 开发trigger的一个问题
为一张表Gen_Currency表建立了一个update的trigger,如下:
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'tr_gen_currency_update ' AND type = 'TR ')
DROP TRIGGER tr_gen_currency_update
GO
CREATE TRIGGER tr_gen_currency_update
ON gen_currency
FOR UPDATE
AS
DELETE FROM bct_deploy..gen_currency
WHERE gen_lang_id + '/ ' + gen_ccy_code IN
(SELECT gen_lang_id + '/ ' + gen_ccy_code FROM deleted)
INSERT INTO bct_deploy..gen_currency (gen_insttu_code, gen_lang_id, gen_ccy_code,gen_ccy_dec_no, gen_ccy_name,
gen_ccy_chi_name, gen_ccy_int_cal_day_no, gen_ccy_sys_use_f, mnt_aut_dnt, mnt_aut_id, mnt_autstts_f,
mnt_maker_dnt, mnt_maker_id, mnt_stts_f, mnt_version_no, mnt_interface_code, mnt_editmask_txt)
SELECT 'BCT ', gen_lang_id,gen_ccy_code,gen_ccy_dec_no, gen_ccy_name,
gen_ccy_chi_name, gen_ccy_int_cal_day_no, gen_ccy_sys_use_f, mnt_aut_dnt, mnt_aut_id, mnt_autstts_f,
mnt_maker_dnt, mnt_maker_id, mnt_stts_f, mnt_version_no, mnt_interface_code, mnt_editmask_txt
FROM inserted
但是每次操作时,后台总是报错:
Record already in the databaseUpdate GEN_CURRENCY set mnt_stts_f = mnt_stts_f where GEN_CCY_CODE = ? and GEN_INSTTU_CODE = ? and GEN_LANG_ID = ? [AUD, BCOM, 1]
但是如果我直接在数据库更新Gen_Currency表,则可以正常更新,望知道的高人给解答下,谢了。
------解决方案--------------------
Try:
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'tr_gen_currency_update ' AND type = 'TR ')
DROP TRIGGER tr_gen_currency_update
GO
CREATE TRIGGER tr_gen_currency_update
ON gen_currency
FOR UPDATE
AS
set nocount on --加这句
DELETE FROM bct_deploy..gen_currency
WHERE gen_lang_id + '/ ' + gen_ccy_code IN
(SELECT gen_lang_id + '/ ' + gen_ccy_code FROM deleted)
INSERT INTO bct_deploy..gen_currency (gen_insttu_code, gen_lang_id, gen_ccy_code,gen_ccy_dec_no, gen_ccy_name,
gen_ccy_chi_name, gen_ccy_int_cal_day_no, gen_ccy_sys_use_f, mnt_aut_dnt, mnt_aut_id, mnt_autstts_f,
mnt_maker_dnt, mnt_maker_id, mnt_stts_f, mnt_version_no, mnt_interface_code, mnt_editmask_txt)
SELECT 'BCT ', gen_lang_id,gen_ccy_code,gen_ccy_dec_no, gen_ccy_name,
gen_ccy_chi_name, gen_ccy_int_cal_day_no, gen_ccy_sys_use_f, mnt_aut_dnt, mnt_aut_id, mnt_autstts_f,
mnt_maker_dnt, mnt_maker_id, mnt_stts_f, mnt_version_no, mnt_interface_code, mnt_editmask_txt
FROM inserted
------解决方案--------------------
建议
DELETE FROM bct_deploy..gen_currency
WHERE gen_lang_id + '/ ' + gen_ccy_code IN <