create trigger to change a field

0

I have a table that I use to send an advertisement to clients, in this table I have a field called email, I need a trigger to make the change after a customer record is inserted, to verify that this field in particular that is email, if eta null put it in emptiness,

    
asked by Marco Cesar Rios Diaz 03.11.2018 в 01:11
source

1 answer

0

If I did not get it wrong, the idea is that when you insert a record with email in NULL, it becomes an empty string, right? If so, to create a trigger like that in SQL Server you should do this:

CREATE TRIGGER [dbo].[NombreTrigger]
   ON [dbo].[miTabla]
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE miTabla
    SET email = ''
    WHERE id IN (SELECT id FROM inserted WHERE email IS NULL)

END

In the temporary table "inserted" are the data of the inserted record, and there we look if the email is null.

I hope you understand.

Good luck!

    
answered by 03.11.2018 / 01:36
source