SQL Cursor syntax error - mysql

I have been breaking my head over this problem. Could not find any answer yet on SO, so I would be greatful if anyone could help me out!
The following query results in a suntax error. Any clue what's wrong?
BEGIN
DECLARE cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrape = FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
DECLARE done INT DEFAULT 0;
read_loop: LOOP
FETCH cur INTO record;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE lifetimes SET end_time=extract(epoch from now()) WHERE object_id=record.object_id AND end_time IS NULL;
END LOOP;
CLOSE cur;
END
The syntax error:
SQL Error [1064] [42000]: 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 cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2
SQL Error [1064] [42000]: 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 cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2
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 cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2
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 cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrap' at line 2

Thank you all for your help! the epoch stuff is indeed postgresql, have changed that. #solarflare was right with the create_procedure remark, that did the trick eventually!
CREATE PROCEDURE set_end_time (out done int)
BEGIN
DECLARE cur CURSOR FOR
SELECT * FROM objects
WHERE present_in_last_scrape = FALSE;
OPEN cur;
LOOP
UPDATE lifetimes SET end_time=UNIX_TIMESTAMP(NOW()) WHERE hemnet_id=cur.hemnet_id AND end_time IS NULL;
END LOOP;
CLOSE cur;
END

Related

MYSQL is not supporting a while loop containing more than one statement

I am trying to install a procedure in Mysql 5.6 launching the command from mysql workbench (version 6).
I get a sintax error with no apparent reason.
If I just remove one of the two assigments inside the while loop, then it would works.
How come i cannot put two statements inside the while?
DELIMITER //
CREATE PROCEDURE PROC (IN TABLE_ varchar(400))
BEGIN
DECLARE Statement varchar(400) DEFAULT "";
DECLARE i INTEGER DEFAULT 1;
DECLARE N INTEGER DEFAULT 2;
while i <= N do
set Statement = 'a';
set i = i+1;
end while;
END
//
DELIMITER ;
Error Code: 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 '= 'a'; set i = i+1; end while; END' at line
10

How to call procedure inside cursor in MariaDB

I'm new to MariaDB, I only have some experience in MS SQL Server.
I'm trying to call a procedure for every row returned from a query.
But I can't find the error when trying to create a procedure with a cursor.
I'm using Heidy to connect to MariaDB 10.0
CREATE PROCEDURE SetAll()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE id INT ;
DECLARE cur CURSOR FOR SELECT aID FROM Customers ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN cur;
testLoop: LOOP
FETCH cur INTO id;
IF done THEN
LEAVE testLoop;
END IF;
CALL SetById(id);
END LOOP testLoop;
CLOSE cur;
END
Error: "/* Error de SQL (1064): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 4 */"
My precedure SetById(id) work just fine.

MySQL Syntax error creating a procedure

This is the error i am getting and can't find where i am going wrong
any help would be highly appreciated
thanks,
SQL query:
CREATE PROCEDURE yearly_income_tax_calculation_federal( ) BEGIN DECLARE salary FLOAT;
MySQL said:
#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 '' at line 5
procedure:
create procedure yearly_income_tax_calculation_federal()
begin
declare salary float;
declare tax float;
SELECT salary_annually INTO salary FROM ndcga776_payroll_db.payroll WHERE payroll_id=2;
IF (salary>0 AND salary<=44701) THEN SET tax = salary*0.15;
ELSE IF (salary>44701 and salary<=89401) THEN SET tax=44701*0.15+(salary-44701)*0.22;
ELSE IF (salary>89401 and salary<=138586) THEN SET tax=44701*0.15+(89401-44701)*0.22+(salary-89401)*0.26;
END IF
END IF
ELSE SET tax=44701*0.15+(89401-44701)*0.22+(138596-89401)*0.26+(salary-138586)*0.29;
END IF
end

Why does this MySQL if statement on SELECT COUNT(*) not work?

I have a .bat file which executes the SQL file as follows.
The aim is that if records are found in a table, nothing will be done, whereas if the table is empty, some records will be inserted.
BEGIN
DECLARE rowCount INT;
SELECT count(*) FROM `martin1` INTO rowCount;
IF rowCount <= 5 THEN
END IF;
END;
But when I execute it, there is an error. I tried to delete the DECLARE, but even for (IF SELECT COUNT(*)...>0) there is still an error.
The error is,
ERROR 1064 (42000) at line 1: 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 rowCount INT' at line 2
How can I resolve this?
try this way
BEGIN
DECLARE rowCount INT;
SELECT count(*) INTO rowCount FROM `martin1`
IF rowCount <= 5 THEN
END IF;
END;
And have a look at this

MySql Stored Procedure Loop Cursor - Syntax error

Where is the syntax error?
DECLARE irid INT DEFAULT 0;
DECLARE tmp_joinid INT DEFAULT 0;
DECLARE loopjoins_eof INT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET loopjoins_eof = TRUE;
START TRANSACTION;
SET irid = (SELECT id FROM `tables` WHERE `adapter_id`=_aid AND `view_id`=_vid AND `name`=_tname);
IF irid IS NOT NULL THEN
DECLARE cur0 CURSOR FOR SELECT `joins`.`id` FROM `joins` WHERE `table_left_id`=irid OR `table_right_id`=irid;
OPEN cur0;
loopjoins: LOOP
FETCH cur0 INTO tmp_joinid;
IF loopjoins_eof THEN
LEAVE loopjoins;
END IF;
-- Lösche Join-Columns
DELETE FROM `join_columns` WHERE `join_id`=tmp_joinid;
END LOOP loopjoins;
CLOSE cur0;
END IF;
COMMIT;
SELECT irid;
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 cur0 CURSOR FOR SELECT joins.id FROM joins WHERE table_left_id=i' at line 12
Thank you
a much better option is to avoid the cursor, you can replace the cursor with something like
DELETE FROM `join_columns`
WHERE `join_id` in
(SELECT `id`
FROM `joins`
WHERE `table_left_id`=irid OR `table_right_id`=irid);
From the manual:
Cursor declarations must appear before handler declarations and after variable and condition declarations.