lantervj
asked on
SQL studio delete trigger
I'm trying to do a simple trigger where any delete to the table will generate an error.
USE [TTdev]
GO
/****** Object: Trigger [dbo].[sales_center_CRM_co mpanies_De lete] Script Date: 11/01/2011 15:15:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ========================== ========== =========
-- Author:		<Author,,Na me>
-- Create date: <Create Date,,>
-- Description:	<Descripti on,,>
-- ========================== ========== =========
ALTER TRIGGER [dbo].[sales_center_CRM_co mpanies_De lete]
ON [dbo].[Sales_Center_CRM_Co mpanies]
AFTER DELETE
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
RAISERROR ('This is a special error message for Vaughn. Let him know ASAP.',100,1);
END
go
I'm not getting an error when deleting a row using studio.
USE [TTdev]
GO
/****** Object: Trigger [dbo].[sales_center_CRM_co
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ==========================
-- Author:		<Author,,Na
-- Create date: <Create Date,,>
-- Description:	<Descripti
-- ==========================
ALTER TRIGGER [dbo].[sales_center_CRM_co
ON [dbo].[Sales_Center_CRM_Co
AFTER DELETE
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
RAISERROR ('This is a special error message for Vaughn. Let him know ASAP.',100,1);
END
go
I'm not getting an error when deleting a row using studio.
ASKER
No, I don't care if it is deleted, I just want to know when it happens.
ASKER
Mamagement Studio doesn't seem to be saving my changes.
Hi lantervj,
You should use lower severity, not 100, but 16, as Lowfatspread suggested.
If this does not help. check, if trigger is not disabled:
You should use lower severity, not 100, but 16, as Lowfatspread suggested.
If this does not help. check, if trigger is not disabled:
SELECT objectproperty(object_id('sales_center_CRM_companies_Delete'), 'ExecIsTriggerDisabled')
Please try the code below:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ========================== ========== =========
-- Author:		<Author,,Na me>
-- Create date: <Create Date,,>
-- Description:	<Descripti on,,>
-- ========================== ========== =========
create TRIGGER [dbo].[sales_center_CRM_co mpanies_De lete]
ON [dbo].[Sales_Center_CRM_Co mpanies]
AFTER DELETE
AS
if (select count(*) from deleted) > 0
RAISERROR ('This is a special error message for Vaughn. Let him know ASAP.',16,1);
go
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ==========================
-- Author:		<Author,,Na
-- Create date: <Create Date,,>
-- Description:	<Descripti
-- ==========================
create TRIGGER [dbo].[sales_center_CRM_co
ON [dbo].[Sales_Center_CRM_Co
AFTER DELETE
AS
if (select count(*) from deleted) > 0
RAISERROR ('This is a special error message for Vaughn. Let him know ASAP.',16,1);
go
If you need to send information message only, you should use low severity level <= 10. Severity level > 10 considered as the error and can rollback the transaction in some cases.
Out of curiosity, why not to use database mail (http://msdn.microsoft.com/en-us/library/ms190307.aspx) instead of raiserror?
Out of curiosity, why not to use database mail (http://msdn.microsoft.com/en-us/library/ms190307.aspx) instead of raiserror?
If your purpose is to be notified then maybe this will be both more transparent and more reliable: instead of RAISERROR,
declare @msg varchar(1000)
select @msg = 'User ' + suser_sname() + ' on machine ' + host_name() + ' has just deleted company ' + min(CompanyName) + ' in the table sales_center_CRM_companies ' from deleted
exec master..xp_sendmail 'vaughn@mycompany.com',@ms g,@no_outp ut=true
select @msg = 'User ' + suser_sname() + ' on machine ' + host_name() + ' has just deleted company ' + min(CompanyName) + ' in the table sales_center_CRM_companies
exec master..xp_sendmail 'vaughn@mycompany.com',@ms
Just for clarification - unless author is using SQL Server 2000, database mail (sp_senddbmail) is preferred method in compare with xp_sendmail.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
e.g.
BEGIN
RAISERROR ('This is a special error message for Vaughn. Let him know ASAP.',100,1);
ROLLBACK TRANSACTION;
RETURN
gave you tried using severity 16?