Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My trigger doesnt work due to a problem related to the clause then

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading