Saturday, April 26, 2014

Error in sql server with a trigger (I want to display a customize message, when the user left a blank field or null) How can I do?

Hi, If you are curious/insist to know the trigger method then please take a look which is least preferred method for your context.


Please note that the trigger you have written is 'AFTER TRIGGER' not Instead of trigger. The FOR INSERT is nothing but AFTER INSERT.



CREATE TRIGGER [dbo].[BLANKFIELD_AfterTrigger]
ON [dbo].[Status]
FOR INSERT, UPDATE

AS

BEGIN
IF @@ROWCOUNT=0 RETURN;
SET NOCOUNT ON;

IF EXISTS(SELECT * FROM inserted WHERE Status IS NULL OR ltrim(rtrim(Status)) ='')
BEGIN
THROW 50000, 'Please Fill the Status field as it is required', 0; --sql Srv 2012

--RAISERROR('Please Fill the Status field as it is required',16,1) --Prior to sql srv 2012
END;

END


No comments:

Post a Comment