我试图通过使用游标来实现行级删除触发器但是在尝试删除表中的任何行时花了这么长时间。
我无法理解它到底在哪里。
/****** Object: Trigger [delStudent] Script Date: 06/24/2010 12:33:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [delStudent]
ON [dbo].[Student]
FOR DELETE
AS
DECLARE @Roll as varChar(50);
DECLARE @Name as varChar(50);
DECLARE @Age as int;
DECLARE @UserName as varChar(50);
SELECT @UserName=SYSTEM_USER;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANSACTION;
declare CurD cursor for select roll, Sname, age from deleted
open CurD
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO [dbo].[Audit]
(roll,sname,age,userId)
VALUES
(@Roll,@Name,@Age,@UserName)
END
COMMIT TRANSACTION;
Close CurD
DEALLOCATE CurD
答案 0 :(得分:3)
我认为你应该在insert-select语句中转换光标。我不确定这会解决你的问题,但无论如何这都是一个很好的最佳实践。
INSERT [dbo].[Audit] (roll,sname,age,userId)
SELECT 'FIELDS FROM DELETED', SYSTEM_USER
FROM deleted
尽量避免使用游标,这样可以提高性能。