Problems with SQL and cursor

Asked

Viewed 43 times

2

I do not understand why but my SQL code is in error on the FETCH line, more specifically: 'FETCH NEXT FROM c_cursor INTO @idC, @LIT, @LST, @LST, @LIT;' and I do not understand the reason for this error. I am trying to make a cursor to scroll through the (existing) crop table and make a version related to the fields in the file, but for some reason I get this error: 'The Procedure 'Alerta (DBA)' could not be modified in the database. Syntax error near 'FETCH' on line 14'. If anyone could help.

ALTER PROCEDURE "DBA"."Alerta"(IN Temperatura DECIMAL, IN Humidade DECIMAL)
BEGIN

DECLARE @idC INTEGER;
DECLARE @LIT DECIMAL;
Declare @LST DECIMAL;
DECLARE @LSH DECIMAL;
DECLARE @LIH DECIMAL;


DECLARE c_cursor CURSOR FOR 
SELECT IDCultura, LimiteInferiorTemperatura, LimiteSuperiorTemperatura, LimiteSuperiorHumidade, LimiteInferiorHumidade FROM Cultura;
OPEN c_cursor 
FETCH NEXT FROM c_cursor INTO @idC, @LIT, @LST, @LST, @LIT;

WHILE @@FETCH_STATUS = 0
BEGIN;
    IF (@LST < new_temp.ValorMedicaoTemperatura OR new_temp.ValorMedicaoTemperatura > @LIT)
        INSERT INTO Alertas(IDAlerta, DataMedicao, ValorMedicao, NomeVariavel, IDCultura)
        VALUES 
        (now(), new_temp.ValorMedicaoTemperatura, 'Temp', @idC); 

    IF (@LSH < new_temp.ValorMedicaoHumidade OR new_temp.ValorMedicaoHumidade > @LIH)
        INSERT INTO Alertas(IDAlerta, DataMedicao, ValorMedicao, NomeVariavel, IDCultura)
        VALUES 
        (now(), new_temp.ValorMedicaoHumidade, 'Hum', @idC); 

    FETCH NEXT FROM c_cursor INTO @idC, @LIT, @LST, @LST, @LIT;

END;
CLOSE c_cursor;
DEALLOCATE c_cursor;

END

1 answer

0

Missing a semicolon in

OPEN c_cursor;

Take an example here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.