It's my first time working with cursors on MySQL, and I am having some problems, it doesn't accept the declaration of my cursor, what's wrong with my code? it shows me this error messagae:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @MyCursor CURSOR' at line 1
and my code is:
DECLARE @MyCursor CURSOR;
DECLARE @MyField varchar2(255);
BEGIN
SET @MyCursor = CURSOR FOR
SELECT codeMaint from affectationmc where iduser=29;
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @MyField
WHILE @@FETCH_STATUS = 0
BEGIN
delete from mcorr where codemaint=@MyField ;
FETCH NEXT FROM @MyCursor
INTO @MyField
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
END;
here is the template I use for cursors. It just taking the items from the select and maps them into the variables declared up top and executes your statement. Once it processes the statement in the section --Actions to Loop Here, it changes the variables by selecting the next row from your select statement. This repeats until it process all of the rows in the select.
DECLARE
@variable_A AS varchar(15)
,@variable_B AS INT
,@variable_C AS INT
,@variable_D AS VARCHAR(255)
DECLARE Process_Name CURSOR LOCAL FAST_FORWARD FOR
SELECT
T.Col_A,T.Col_B,T.Col_C,T.Col_D
FROM
tab.my_table AS T;
OPEN Process_Name
FETCH NEXT FROM Process_Name INTO @variable_A,@variable_B,@variable_C,@variable_D
WHILE @@FETCH_STATUS = 0
BEGIN
--Actions to Loop Here
FETCH NEXT FROM Process_Name INTO @variable_A,@variable_B,@variable_C,@variable_D
END
CLOSE Process_Name
DEALLOCATE Process_Name