SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 trigger is deleting the entries in the audit table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gagani
Yak Posting Veteran

94 Posts

Posted - 08/30/2013 :  07:30:05  Show Profile  Reply with Quote
The following is the trigger which create a row in the audit table when a single deletion is occurred.

ALTER TRIGGER [dbo].[TRG_Delete_tbl_attendance]
ON [dbo].[tbl_attendance]
AFTER DELETE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for trigger here
BEGIN TRANSACTION
IF ( (SELECT COUNT(*) FROM deleted) > 0 )
BEGIN
INSERT INTO [dbo].[tbl_EY_Audit]
([SourceTable]
,[RecordPrimaryKey]
,[UserName]
,[HostName]
,[Action]
,[DateTime])
SELECT 'tbl_attendance'
,AttPatternId
,SUSER_SNAME()
,HOST_NAME()
,'DELETE'
,GETDATE()
FROM deleted d
END

IF @@ERROR!=0
BEGIN
ROLLBACK TRANSACTION
RAISERROR('Error deleting data in [tbl_attendance] from Table Triggier',11,1)
END

COMMIT TRANSACTION

END


I am trying to create a trigger which should prevent the bulk deletion. The following is the trigger which I have written, it is preventing the bulk deletion. But the problem is, it is removing the single deletion entries in the audit table. I want audit table to hold back the single deletion entries without allowing the bult deletion


ALTER TRIGGER [dbo].[TRG_Delete_Bulk_tbl_attendance]
ON [dbo].[tbl_attendance]
AFTER DELETE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for trigger here
BEGIN TRANSACTION
IF (SELECT COUNT(*) FROM deleted) > 1
BEGIN
--SET @isBulkTried = 1
RAISERROR('YOU CAN ONLY DELETE ONE RECORD AT A TIME.',10,1)
ROLLBACK TRANSACTION
END

IF @@ERROR!=0
BEGIN
ROLLBACK TRANSACTION
RAISERROR('Error bulk deleting data in [tbl_attendance] from Table Triggier',11,1)
END

END

James K
Flowing Fount of Yak Knowledge

3588 Posts

Posted - 08/30/2013 :  08:17:35  Show Profile  Reply with Quote
You need an else clause to insert when there is one row. See in red below:
ALTER TRIGGER [dbo].[TRG_Delete_Bulk_tbl_attendance]
ON [dbo].[tbl_attendance]
AFTER DELETE
AS 
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for trigger here
BEGIN TRANSACTION
IF (SELECT COUNT(*) FROM deleted) > 1
BEGIN
--SET @isBulkTried = 1
RAISERROR('YOU CAN ONLY DELETE ONE RECORD AT A TIME.',10,1)
ROLLBACK TRANSACTION
END
ELSE 
	INSERT INTO....

IF @@ERROR!=0
BEGIN
ROLLBACK TRANSACTION
RAISERROR('Error bulk deleting data in [tbl_attendance] from Table Triggier',11,1)
END

END
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000