Langsung ke konten utama

Easily Create Trigger Delete Protection SQL Server

First learnTricks For Document Delete Protection On Trigger

[sql]
CREATE PROCEDURE [System].[Utility.GenerateTriggerDeleteProtect]
@TableName NVARCHAR(250)
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@SQL NVARCHAR(MAX)


IF NOT EXISTS
(
SELECT TOP 1
name
FROM sys.triggers
WHERE parent_id = OBJECT_ID(@TableName)
)
BEGIN

SET @SQL = '
CREATE TRIGGER '+REPLACE(@TableName,']','.DeleteProtect]')+'
ON '+@TableName+'
Instead of DELETE
AS
BEGIN
SET NOCOUNT ON;
delete from '+@TableName+' where doc_id in (select doc_id from deleted where docflow_seq=0)
END'

EXEC sp_executesql @SQL
PRINT 'Create delete protect trigger for '+@TableName
END
END
[/sql]

Komentar

Postingan populer dari blog ini

sys.processes Status sp_who2 SQL Server

Taken from the books online reference for sys.processes  and the status column. dormant = SQL Server is resetting the session. running = The session is running one or more batches. When Multiple Active Result Sets (MARS) is enabled, a session can run multiple batches. For more information, see Using Multiple Active Result Sets (MARS). background = The session is running a background task, such as deadlock detection. rollback = The session has a transaction rollback in process. pending = The session is waiting for a worker thread to become available. runnable = The task in the session is in the runnable queue of a scheduler while waiting to get a time quantum. spinloop = The task in the session is waiting for a spinlock to become free. suspended = The session is waiting for an event, such as I/O, to complete.