日期:2014-05-19  浏览次数:20576 次

触发器发邮件
大家好,我在做触发器发邮件的时候,当连续插入两条记录的时候,就只能收到一封邮件:
CREATE   TRIGGER   INSERT_GTBH
ON   GTBH
FOR   INSERT
AS
DECLARE   @MESSAGEBOX   VARCHAR(250)
DECLARE   @GTBH_CODE   VARCHAR(17)
DECLARE   @GBH_SUM   NUMERIC(15,2)
SELECT   @GTBH_CODE=A.gtbh_code,@GTBH_FROM=A.c_code   FROM   inserted   A
set   @messagebox=@gtbh_code+ '   '+@gtbh_from
EXEC   master..XP_SENDMAIL   'pd@finity-int.com ',@messagebox
GO



------解决方案--------------------
不能直接从insert中取,这样只能取一条,写游标逐行取
CREATE TRIGGER INSERT_GTBH
ON GTBH
FOR INSERT
AS
DECLARE @MESSAGEBOX VARCHAR(250)
DECLARE @GTBH_CODE VARCHAR(17)
DECLARE @GBH_SUM NUMERIC(15,2)
declare cur_tmp cursor for
SELECT A.gtbh_code,A.c_code FROM inserted A
open cur_tmp
fetch next from cur_tmp into @GTBH_CODE,@GTBH_FROM
while @@fetch_status=0
begin
set @messagebox=@gtbh_code+ ' '+@gtbh_from
EXEC master..XP_SENDMAIL 'pd@finity-int.com ',@messagebox
fetch next from cur_tmp into @GTBH_CODE,@GTBH_FROM
end
close cur_tmp
deallocate cur_tmp