1. 程式人生 > >SQL_觸發器學習

SQL_觸發器學習

char alua 消息 after create name ble date() null

--觸發器學習
-------------------------------------------------------------------------------after 觸發器-------------------------------------------------------------------------------
--1 insert 觸發器
IF EXISTS(SELECT * FROM sys.objects WHERE name=‘tgr_tablename_insert‘)
DROP TRIGGER tgr_tablename_insert
GO
CREATE TRIGGER tgr_tablename_insert
ON dbo.TB_HR_SatisfactionEvaluation_Quota
--插入數據觸發
FOR INSERT
AS
--觸發執行的操作
INSERT INTO dbo.TB_HR_SatisfactionEvaluation_Quota( EvaluationYear ,EvaluationType ,LocationName ,DescVal ,DeptName ,EvaluationDesc ,enable ,createby ,CreateTime ,updateby ,updateTime)
VALUES(‘2017‘,‘test01‘,‘test01‘,123,‘test01‘,‘test01‘,1,1,GETDATE(),1,GETDATE())
GO

--2 delete 觸發器
IF EXISTS(SELECT * FROM sys.objects WHERE name=‘tgr_tablename_delete‘)
DROP TRIGGER tgr_tablename_delete
GO
CREATE TRIGGER tgr_tablename_delete
ON dbo.TB_HR_SatisfactionEvaluation_Quota
FOR DELETE
AS
--觸發執行執行的操作
--DELETE FROM dbo.TB_HR_SatisfactionEvaluation_Quota WHERE CreateTime=‘2017-07-31‘ AND LocationName=‘test01‘
PRINT ‘備份數據中...‘

IF(OBJECT_ID(‘BackupTest‘)IS NOT NULL)
BEGIN

INSERT INTO BackupTest
SELECT
EvaluationYear ,
EvaluationType ,
LocationName ,
DescVal ,
DeptName ,
EvaluationDesc ,
enable ,
createby ,
CreateTime ,
updateby ,
updateTime
FROM Deleted;
END
ELSE
BEGIN
SELECT * INTO BackupTest FROM Deleted
END

PRINT ‘數據備份成功!‘
GO


--3 update觸發器
IF EXISTS(SELECT * FROM sys.objects WHERE name=‘tgr_tablename_update‘)
DROP TRIGGER tgr_tablename_update
GO
CREATE TRIGGER tgr_tablename_update
ON dbo.TB_HR_SatisfactionEvaluation_Quota
FOR UPDATE
AS
--觸發器要執行的操作 inserted表 deleted表

--inserted表中的數據
INSERT INTO dbo.BackupTest SELECT
Inserted.EvaluationYear ,
Inserted.EvaluationType ,
Inserted.LocationName ,
Inserted.DescVal ,
Inserted.DeptName ,
Inserted.EvaluationDesc ,
Inserted.enable ,
Inserted.createby ,
Inserted.CreateTime ,
Inserted.updateby ,
Inserted.updateTime FROM Inserted
--deleted表中的數據
INSERT INTO dbo.BackupTest SELECT
Deleted.EvaluationYear ,
Deleted.EvaluationType ,
Deleted.LocationName ,
Deleted.DescVal ,
Deleted.DeptName ,
Deleted.EvaluationDesc ,
Deleted.enable ,
Deleted.createby ,
Deleted.CreateTime ,
Deleted.updateby ,
Deleted.updateTime FROM Deleted
GO
-------------------------------------------------------------------------------after 觸發器-------------------------------------------------------------------------------


-------------------------------------------------------------------------------instead of 觸發器-------------------------------------------------------------------------------
/*
instead of 觸發器 不執行相應的update delete insert 操作,執行的只是觸發器本身的腳本
*/
IF EXISTS(SELECT * FROM sys.objects WHERE name=‘tgr_tablename_insteadOf‘)
DROP TRIGGER tgr_tablename_insteadOf
GO
CREATE TRIGGER tgr_tablename_insteadOf
ON dbo.TB_HR_SatisfactionEvaluation_Quota INSTEAD OF DELETE /*update insert*/
AS
DECLARE @LocationName NVARCHAR(50)=‘‘
SELECT @LocationName=Deleted.LocationName FROM Deleted
PRINT [email protected]
--暫時不刪除數據的
DELETE FROM dbo.TB_HR_SatisfactionEvaluation_Quota WHERE LocationName=‘test01‘
GO

-------------------------------------------------------------------------------instead of 觸發器-------------------------------------------------------------------------------

--顯示自定義消息 raiseError
IF EXISTS(SELECT * FROM sys.objects WHERE name=‘tgr_messager‘)
DROP TRIGGER tgr_messager
GO
CREATE TRIGGER tgr_messager
ON dbo.TB_HR_SatisfactionEvaluation_Quota
AFTER INSERT,DELETE
AS
RAISERROR(‘tgr_messager觸發器被觸發執行‘,16,10)
GO


---啟用 禁用觸發器
DISABLE TRIGGER tgr_messager ON dbo.TB_HR_SatisfactionEvaluation_Quota;
ENABLE TRIGGER tgr_messager ON dbo.TB_HR_SatisfactionEvaluation_Quota;

--查看已經存在的觸發器
SELECT * FROM sys.triggers
SELECT * FROM sys.objects WHERE type=‘TR‘

SQL_觸發器學習