So i have a project where i have to build a database and establish connections between the tables and to create some views, procedures and triggers.
So i created one and i can´t seem to why it doesn´t work.
Here is the piece of code of the trigger:
CREATE TRIGGER TRG_ValidarEstoque
ON Trabalho.Equipamento
FOR INSERT
AS
BEGIN
DECLARE @estoque_minimo INT;
-- Defina o valor do estoque mínimo aceitável
SET @estoque_minimo = 10;
-- Verifique se o estoque está abaixo do limite mínimo, se estiver aparece uma mensagem
IF NEW.Stock < @estoque_minimo THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Estoque abaixo do limite mínimo';
END IF;
END;
And here’s the table i m using if it helps:
CREATE TABLE Trabalho.Equipamento (
Equipamento_ID INT PRIMARY KEY,
Designacao VARCHAR(99),
DataAquisicao DATE,
Stock INT,
Fornecedor_ID INT,
Colaborador_ID INT,
FOREIGN KEY (Colaborador_ID) REFERENCES
Trabalho.Colaborador(Colaborador_ID),
FOREIGN KEY(Fornecedor_ID) REFERENCES
Trabalho.Fornecedor(Fornecedor_ID)
);
I tried removing some pieces of the code and when i thought it would be working a new error would appear. I get the following error if it helps.
Msg 156, Level 15, State 1, Procedure TRG_ValidarEstoque, Line 13 [Batch Start Line 221]
Incorrect syntax near the keyword ‘THEN’.
Thank you
>Solution :
Assuming this is SQL Server from the error message, there is no then or end if, the syntax is just IF (condition) (statement) ELSE (statement)