stored procedure error message - mysql

I have implemented the stored procedure as below, however i am getting the following error message when i try applying it:
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 'WHERE symbol_id = id GROUP BY symbol_id;
I did some debugging and found that it was caused by the #max variable which i am trying to write the result into, however i do not see anything wrong with the syntax, can anyone please advise?
DROP PROCEDURE IF EXISTS `GENERATE_REPORT`;
DELIMITER $$
CREATE DEFINER=CURRENT_USER PROCEDURE `GENERATE_REPORT`()
BEGIN
DECLARE id INT;
DECLARE max INT;
DECLARE at_end BIT DEFAULT 0;
DECLARE cur CURSOR
FOR SELECT symbol_id from trade;
DECLARE CONTINUE HANDLER
FOR SQLSTATE '02000' SET at_end=1;
OPEN cur;
FETCH cur INTO id;
WHILE (NOT at_end) DO
SELECT SUM(quantity) FROM trade INTO **#max** WHERE symbol_id = id GROUP BY symbol_id;
FETCH cur into id;
END WHILE;
CLOSE cur;
END
$$
DELIMITER ;

You have incorrect syntax in your SELECT ... INTO:
Change
SELECT SUM(quantity)
FROM trade
INTO #max -- Incorrect placement
WHERE symbol_id = id
GROUP BY symbol_id;
To
SELECT SUM(quantity)
INTO #max -- Correct placement
FROM trade
WHERE symbol_id = id
GROUP BY symbol_id;
The INTO should come right after the SELECT and before the FROM

Related

Nested Cursor Declare Issue Mysql

I'm trying to make a Nested Cursor in Mysql by following this instruction.
Then i got this issue:
#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 'DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;' at line 22
I've 2 table 'account' and 'n_activity' (n = account_id in table 'account')
Ex: i've table 'account' and '20_activity'.
So i want to loop the 'account_id' and get the 'activity_id' from that loop.
Here is my code:
DROP PROCEDURE if exists update_schema_activity_startdate_and_duedate;
DELIMITER $$
CREATE PROCEDURE update_schema_activity_startdate_and_duedate()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE accountid INT;
--
-- GET ALL ACCOUNT ID
--
DECLARE accountids CURSOR FOR SELECT account_id FROM account;
--
-- LOOP
--
OPEN accountids;
read_loop: LOOP
FETCH accountids INTO accountid;
BLOCK2: BEGIN
SET #_activity = CONCAT(accountid,'_activity');
DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;
END LOOP;
CLOSE accountids;
END$$
DELIMITER ;
CALL update_schema_activity_startdate_and_duedate();
Please help, thanks.

how to declare & set cursor in MYSQL STORED PROCEDURE?

I am trying to use CURSOR inside mysql stored procedure... I am getting challenge while declaring the cursor... I error is **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 ';
SELECT count(*) INTO countitem from TBL_SHOPPING_CART_ITEM
SELECT Produ' at line 8
**
Please help me to solve this issue... Thanks in advance.. my code is like below,
delimiter //
CREATE PROCEDURE placeOrder(IN cartId INT)
BEGIN
DECLARE countitem INT;
DECLARE productId INT;
DECLARE cartId INT;
DECLARE itemDicountAmt INT;
DECLARE itemCursor CURSOR;
SET countitem = SELECT count(*) from TBL_SHOPPING_CART_ITEM
SET itemCursor = SELECT ProductId, Quantity FROM TBL_SHOPPING_CART_ITEM
OPEN itemCursor
WHILE countitem > 0
BEGIN
FETCH itemCursor into productId, cartId;
itemDicountAmt = calculateNetItemStandardDiscountAmount(productId, cartId);
insert into debugtable select concat('item discount amount', itemDicountAmt);
SET countitem = countitem - 1;
END
CLOSE itemCursor
DEALLOCATE itemCursor
END//
delimiter ;
Don't worry, this isn't a bug.
The DECLARE clause is used to tell your machine that a local variable exists. The operations are made after the declarations, by aknowledging the existing, declared local variables.
So in your case, you tried to add a new declaration of variable after the machine started calculating, which isn't possible in MySQL. You'll have to find another way to use your last variable.
To assign a new content to a variable, first use DECLARE to create your variable first, then use SET as an assignment to the declared variable. I'll leave this link right here so you can get to know how to use it.

I dont't understand why im getting syntax error on my code below. Can anyone help me?

DELIMITER ##
create trigger tra_Price after update on assets_cdn_charge for each row
begin
declare res int;
declare ids int;
declare idq int;
declare idt int;
set res = (select price from assets_cdn_charge where price = new.price);
set ids = (select id from assets_cdn_charge where price = new.price);
DECLARE cur CURSOR FOR SELECT id FROM assets_cdn_composite WHERE cdn_charge_id = ids;
open cur;
ins_loop:LOOP
fetch cur into idq;
declare curs cursor for select id from assets_cdn_traffic where domain_name_id = idq;
open curs;
ins1_loop:LOOP
fetch curs into idt;
update assets_cdn_traffic set cost = traffic * res where domain_namd_id = idt;
end LOOP;
close curs;
end LOOP;
close cur;
END; ##
when I run this code,I had get this error:
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 id FROM
assets_cdn_composite WHERE cdn_charge_id =' at line 9
Probably because:
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
...so that line should likely be moved before the two set statements above that point.
I believe that you're going to have other trouble if that fixes that error, because I don't know what you expect that query to retrieve as idq is declared before that point, but is not set to any value?
=====
UPDATE:
Below is an example from a previous comment about the possibility to eliminate cursors altogether. Try this:
BEGIN
UPDATE assets_cdn_traffic
JOIN assets_cdn_composite ON assets_cdn_traffic.domain_name_id = assets_cdn_composite.cdn_charge_id
JOIN assets_cdn_charge ON assets_cdn_charge.id = assets_cdn_composite.cdn_charge_id
SET cost = traffic * NEW.price
WHERE assets_cdn_charge.id = NEW.id
END
However, I would try the update query separately before using in the trigger to make sure that it works as expected. Replace the NEW.price and the NEW.id with test values to verify the handling.

Error with MySQL, DECLARE

I am using MySQL v5.0.92 and I'm trying to import some data, but I get an error on declare my variables.
BEGIN
DECLARE flag INT;
DECLARE id INT;
while flag=0 begin
SET id=(SELECT top 1 user_id
FROM ac_user_info WHERE user_id>#id order by user_id)
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'first_name',first_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'last_name',last_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
SET flag=(select case #id when(SELECT MAX(user_id)
FROM cuddleew_bakup.ac_user_info) then 1
else 0
END CASE)
END WHILE
END
In MySQL console i get this:
BEGIN DECLARE flag INT;
#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 flag INT' at line 2.
Can you help me with this and tell me what's wrong. Thanks in advance.
You have to change your delimiter first, so the ; doesn't tell MySQL that this command is over.
DELIMITER $$
[your code here]
END $$
DELIMITER ;

MySQL cursor declaration with multiple joins

I am attempting to declare a cursor with multiple joins in a stored procedure. The query runs perfectly outside of the stored procedure, but the stored procedure gives me an error at the cursor declaration, claiming there is a syntax error.
DROP PROCEDURE IF EXISTS getCheaters;
DELIMITER $$
CREATE PROCEDURE getCheaters()
BEGIN
DECLARE id INT (11);
DECLARE first_name VARCHAR (255);
DECLARE last_name VARCHAR(255);
DECLARE file_name VARCHAR(255);
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE userCursor FOR
SELECT last_name, first_name, users.id
FROM users JOIN documents ON (users.id = documents.user_id)
JOIN licenses ON (licenses.user_id = users.id)
WHERE multi_user_license_id IS NULL
GROUP BY last_name, first_name
HAVING count(documents.title) > 60;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN userCursor;
SELECT FOUND_ROWS() INTO num_rows;
read_loop: LOOP
/*Do stuff*/
IF no_more_rows THEN
CLOSE userCursor;
LEAVE read_loop;
END IF;
SET loop_cntr = loop_cntr + 1;
END LOOP;
END $$
DELIMITER;
And I get 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 'DELIMITER' at line 1
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 'FOR
(SELECT last_name, first_name, users.id
FROM users JOIN docu' at line 13
Does any one see where my error is?
Change:
...
DECLARE userCursor FOR
...
by:
...
DECLARE userCursor CURSOR FOR
...
SQL Fiddle demo