-- =============================================
-- Author: sean
-- Create date: 2010-06-23
-- Description: 添加公告的时候,在BulletinAndMember表里,给每个用户添加一条公告信息
-- =============================================
CREATE TRIGGER BulletinMember
ON Bulletin
AFTER INSERT
AS
BEGIN
declare @BulletinId varchar(50) --保存刚刚插入的公告的信息的编号
declare @memberId varchar(50)
if exists(select * from inserted)
set @BulletinId =(select BulletinId from inserted) --去除刚刚插入的公告的编号
declare Member_Cur cursor for --定义游标,游标名称取名为Member_Cur
select memberId from Member --查出需要的集合,放到游标中(类似于放到SqlDataReader)
open Member_Cur --打开游标
fetch next from Member_Cur into @memberId --读取第一条数据
while @@fetch_status = 0
begin
insert into BulletinAndMember (BulletinID,memberId) values (@BulletinId,@memberId) ---插入到公告和员工的记录表里面去,一个员工一条记录
fetch next from Member_Cur into @memberId ---跳到下一行!
end
END
GO