0
Good evening guys, I need you to check if the address and phone columns are null, if you are returning an error message, otherwise effective operation. my code is like this:
CREATE TRIGGER VERIFICA
ON cliente
INSTEAD OF INSERT AS
BEGIN
DECLARE
@NOME VARCHAR(50),
@TELEFONE VARCHAR(12),
@ENDERECO VARCHAR (100)
SELECT @NOME = NOME FROM inserted
SELECT @TELEFONE = TELEFONE FROM inserted
SELECT @ENDERECO = ENDERECO FROM inserted
IF @telefone is not null and @endereco is not null
begin
RAISERROR ('Campos nulos', 11,1);
end
ELSE
INSERT INTO cliente (nome, telefone, endereco) VALUES (@NOME, @TELEFONE, @ENDERECO);
END
This could not be done directly in the application, thus avoiding using trigger procedure (Trigger) for something so simple?
– José Diz
It is necessary to be aware that a trigger procedure (Trigger) SQL Server has to be built to handle multiple lines at the same time. The above code considers that there will be a single line, which can lead to an execution error. Suggested reading: https://portosql.wordpress.com/2018/08/18/armadilhas-na-programacao-de-trigger/
– José Diz
It is actually easier to treat nulls in the application itself than in SQL, unless you have a very strong reason to do so!
– João Martins
Yes, it would be much more interesting to do this in the client service but it’s just a question of university.
– Opticom