How to create a trigger so that it does not erase a record if it contains certain characteristics in sql server

0

That is, create a trigger that prevents me from deleting a record from the 'person' table, but only if the name contains the following characters: 'JUAN' and print the message that can not be deleted

CREATE TRIGGER borrar
ON PERSONA
INSTEAD OF DELETE
AS
SET NOCOUNT ON
DECLARE @NOMBRE VARCHAR(30)
SELECT @NOMBRE = D.NOMBRE 
FROM PERSONA P INNER JOIN DELETED D 
ON P.ID_PER=D.ID_PER
IF (@NOMBRE LIKE '%JUAN%')
BEGIN
PRINT 'NO ES POSIBLE ELIMINAR'
END
ELSE
DELETE FROM PERSONA WHERE NOMBRE=@NOMBRE
GO
    
asked by ara celi 16.05.2017 в 20:54
source

1 answer

0

It's more or less like this:

CREATE TRIGGER Ejemplo ON persona
    FOR DELETE
AS 
BEGIN

    IF EXISTS (SELECT TOP 1 1 FROM DELETED WHERE CHARINDEX('JUAN', NOMBRE) > 0)
    BEGIN
        RAISERROR ( 'No se puede eliminar una persona con el nombre Juan',  16, 10)
    END
END
GO
    
answered by 16.05.2017 / 21:57
source