SQL Server Trigger basic

refer :  http://www.codeproject.com/Articles/25600/Triggers-SQL-Server

 

SELECT * FROM sys.triggers; //get all 

 

SELECT OBJECT_DEFINITION (OBJECT_ID(N'onJobPosts_mm_SlotsInsert')) AS [Trigger Definition]; //获取code

 

IF OBJECT_ID ('onJobPostsInsert','TR') IS NOT NULL 

  DROP TRIGGER onJobPostsInsert;

GO 

 


CREATE TRIGGER onJobPosts_mm_SlotsInsert ON [dbo].[JobPosts_mm_Slots]
FOR INSERT
AS
declare @JobPosts_id int;
select @JobPosts_id=i.JobPosts_id from inserted i;


insert into dbo.PublishRecords (isPublish,JobPosts_id) values(1,@JobPosts_id);
GO



CREATE TRIGGER onJobPosts_mm_SlotsDelete ON [dbo].[JobPosts_mm_Slots]
FOR DELETE
AS
BEGIN
  SET NOCOUNT ON
  BEGIN
    if(@@ROWCOUNT = 1)
    BEGIN
      declare @JobPosts_id int;
      select @JobPosts_id=i.JobPosts_id from deleted i;
      insert into dbo.PublishRecords (isPublish,JobPosts_id) values(0,@JobPosts_id);
    END
  END
END

 

posted @ 2015-10-26 14:30  兴杰  阅读(196)  评论(0编辑  收藏  举报