CREATE TRIGGER [t_mail] ON [dbo].[users]
FOR INSERT
AS
EXEC master..xp_sendmail 'MaryM',
'Don''t forget to print a report for the distributors.'
.......
...全文
1827打赏收藏
有关触发器的问题请教:如何取得当前插入的ID呢?
我写了一个触发器,当有 INSERT 操作时,取当前插入的ID,然后给用户发邮件。但是如何取得当前插入的ID呢? CREATE TRIGGER [t_mail] ON [dbo].[users] FOR INSERT AS EXEC master..xp_sendmail 'MaryM', 'Don''t forget to print a report for the distributors.' .......
CREATE TRIGGER [t_mail] ON [dbo].[users]
FOR INSERT
AS
declare @id int
declare #tb cursor for select id from inserted
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
EXEC master..xp_sendmail 'MaryM',
'Don''t forget to print a report for the distributors.'
.......
fetch next from #tb into @id
end
close #tb
deallocate #tb
go
CREATE TRIGGER [t_mail] ON [dbo].[users]
FOR INSERT
AS
declare @id int
--如果id是标识字段,就用:
set @id=@@identity
--如果id是插入记录时指定的,就用
set @id=id from inserted
EXEC master..xp_sendmail 'MaryM',
'Don''t forget to print a report for the distributors.'
.......