how to read data from CURSOR - mysql stored procedure - mysql

I have implemented the CURSOR inside mysql stored procedure... I am getting challenge while reading the cursor... Basically I want to read column value from cursor & store the same into a variable.. but I am getting the variable value as NULL... What would be the solution... Please help me.. Thanks in advance...
My code is like below,
delimiter //
CREATE PROCEDURE placeOrder(IN cartId INT)
BEGIN
DECLARE countitem INT DEFAULT 0;
DECLARE productId INT;
DECLARE quantity INT;
DECLARE itemDicountAmt DOUBLE DEFAULT 0;
DECLARE v_finished INT DEFAULT 0;
DEClARE cart_cursor CURSOR FOR
SELECT ProductId, Quantity FROM TBL_SHOPPING_CART_ITEM;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
OPEN cart_cursor;
get_cart: LOOP
FETCH cart_cursor into productId, quantity;
IF v_finished = 1 THEN
LEAVE get_cart;
END IF;
-- build email list
insert into debugtable select concat('PRODUCT :', productId);
insert into debugtable select concat('QUANTITY :', quantity);
END LOOP get_cart;
CLOSE cart_cursor;
END//
delimiter ;
OUTPUT:
tmptable
NULL
NULL
As I am having one record in my shopping cart table...

See C.1 Restrictions on Stored Programs :: Name Conflicts within Stored Routines.
Option 1:
...
/*DEClARE cart_cursor CURSOR FOR
SELECT ProductId, Quantity FROM TBL_SHOPPING_CART_ITEM;*/
DEClARE cart_cursor CURSOR FOR
SELECT
TBL_SHOPPING_CART_ITEM.ProductId,
TBL_SHOPPING_CART_ITEM.Quantity
FROM TBL_SHOPPING_CART_ITEM;
...
Option 2:
...
/*DECLARE productId INT;
DECLARE quantity INT;*/
DECLARE _productId INT;
DECLARE _quantity INT;
...
/*FETCH cart_cursor into productId, quantity;*/
FETCH cart_cursor into _productId, _quantity;
...
/*insert into debugtable select concat('PRODUCT :', productId);
insert into debugtable select concat('QUANTITY :', quantity);*/
insert into debugtable select concat('PRODUCT :', _productId);
insert into debugtable select concat('QUANTITY :', _quantity);
...

Related

Cursor to copy distinct record from one table to another

I want to copy all recoreds from temp1 table to anoter two tables I am using cursor for this .
DELIMITER //
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
declare tcursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;
OPEN tcursor;
REPEAT
FETCH cursor into v_fn,v_ln, v_email;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fname,v_lname,v_email);
END REPEAT;
CLOSE tcursor;
END//
DELIMITER
this code is showing error
MySQL said: Documentation
#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 'for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLE' at line 8
How to resolve this
You have multiple errors in your syntax and don't exit the loop. Try this?
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
DECLARE done INT DEFAULT FALSE;
declare tcursor cursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN tcursor;
read_loop: LOOP
FETCH tcursor into v_fn,v_ln, v_email;
if done then
LEAVE read_loop;
END IF;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fn,v_ln,v_email);
END LOOP;
CLOSE tcursor;
END
I tried this query and find this is working
insert into atom(id,type) select id,'Person' from user1;
INSERT INTO user( id, fname, lname, mailid ) SELECT id, fname, lname, mailid FROM user1;

Mysql Stored procedure with cursor

Mysql cursor issue?
I have written a stored procedure which will travel's record from one table and insert those into 2-3 different tables
using insert statements.
Problem is that i am checking if record is not exists in table1 then I am inserting record from temptable to table1 ,table2 sequentially
,but the condition is having some problem i don't know it its always going into else part.
Code sample is as follows:
CREATE PROCEDURE `insertData`(In clientNo INT,In usedID INT)
BEGIN
declare mame varchar(100);
declare address varchar(100);
declare city varchar(50);
declare IdentityNO1 varchar(20)
declare cur1 cursor for select * from temptable;
declare continue handler for not found set done=1;
SET #clientNo = clientNO;
SET #userID = userID;
set done = 0;
open cur1;
igmLoop: loop
fetch cur1 into Name,Address,City,IdentityNO1,clientNo;
if done = 1 then leave igmLoop; end if;
//If no record exists in some records table1,table2.
IF ( (SELECT COUNT(*) FROM table1
WHERE IndentityNo=IdentityNo1
AND clientNo=#clientNo) < = 0)
INSERT INTO table1 (Name,IdentityNO) VALUES (name,IdentityNO1);
INSERT INTO table2 (Address,City) VALUES(address,city);
ELSE
INSERT INTO tblexceptional(Name,Address,City,IdentityNo)
VALUES(name,address,city,IdentityNo1);
end loop igmLoop;
close cur1;
END
There is no THEN nor END IF keywords, the procedure cannot compile.
Check this link for proper syntax of IF statement: http://dev.mysql.com/doc/refman/5.7/en/if.html
Use EXIST operator instead of (SELECT count(*)... ) <=0,
read this link to know the reason: http://sqlblog.com/blogs/andrew_kelly/archive/2007/12/15/exists-vs-count-the-battle-never-ends.aspx
IF EXISTS(
SELECT null FROM table1
WHERE IndentityNo=IdentityNo1
AND clientNo=#clientNo
)
THEN
INSERT INTO table1 (Name,IdentityNO) VALUES (name,IdentityNO1);
INSERT INTO table2 (Address,City) VALUES(address,city);
ELSE
INSERT INTO tblexceptional(Name,Address,City,IdentityNo)
VALUES(name,address,city,IdentityNo1);
END IF;
I recommend using some prefixes for procedure arguments and variable names to avoid ambiguity, for example use p_ for parameters and v_ for variables. It's hard to guess, looking at this code, which name is a column name, a variable or a procedure parameter. This can lead to mistakes and errors.
Avoid using SELECT * - this code will fail if someone will change the table structure. Explicitely list required columns in the cursor declaration:
declare cur1 cursor for
select name,Address,City,IdentityNO,clientNo
from temptable;
The corrected procedure might look like this:
CREATE PROCEDURE `insertData`(In p_clientNo INT,In p_usedID INT)
BEGIN
declare v_name varchar(100);
declare v_address varchar(100);
declare v_city varchar(50);
declare v_IdentityNO varchar(20)
declare v_clientNo int
declare cur1 cursor for
select name,Address,City,IdentityNO,clientNo
from temptable;
declare continue handler for not found set done=1;
set done = 0;
open cur1;
igmLoop: loop
fetch cur1 into v_name,v_Address,v_City,v_IdentityNO,v_clientNo;
if done = 1 then leave igmLoop; end if;
//If no record exists in some records table1,table2.
IF EXISTS( SELECT 1 FROM table1
WHERE IndentityNo = v_IdentityNo
AND clientNo = v_clientNo)
INSERT INTO table1 (Name,IdentityNO) VALUES (v_name,v_IdentityNO);
INSERT INTO table2 (Address,City) VALUES(v_address,v_city);
ELSE
INSERT INTO tblexceptional(Name,Address,City,IdentityNo)
VALUES(v_name,v_address,v_city,v_IdentityNo);
END IF;
end loop igmLoop;
close cur1;
END

Stored procedure not working properly in MySql?

I have been debugging a SQL stored procedure which has to take values (in my code ID and Numb) form table A based on the values (ID) present in the Table C, then square the Numb and store it in Table B i.e. all the things ID, Numb and Square.
I am not able to figure out the problem in the below code
DELIMITER $$
CREATE PROCEDURE matlab.squaring
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
SET #square= #Numb * #Numb
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF
END LOOP the_loop;
END$$
When I run the stored procedure the error that pops up is "there seems to be some syntax error in your code, please refer to MYSql guide. "
edit:
one more help please how to execute this stored procedure.
There are various minor errors;
You need a parameter list, even if empty for the procedure;
CREATE PROCEDURE matlab.squaring()
The continue handler needs to be right below the other declarations;
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET #finish = TRUE;
You forgot a semicolon;
SET #square= #Numb * #Numb;
You forgot # on the variable usages;
) values ( #ID , #Numb, #square);
You forgot a semicolon on END IF
END IF;
Just as an overview, here's the complete thing updated;
CREATE PROCEDURE matlab.squaring()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET #finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( #ID , #Numb, #square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END//
You have missed () after PROCEDURE matlab...
And ; after END IF
Also, HANDLER declaration should be before any executable code and
after CURSOR declaration
Semicolon after SET #square= #Numb * #Numb is needed
So, query should be like this:
DELIMITER $$
CREATE PROCEDURE matlab.squaring ()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END$$
Missed parameter brackets and semicolon at endif.
DELIMITER $$
CREATE PROCEDURE squaring()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END$$

MySQL Stored Procedure Nested Cursors Error

i am copying data for a Many to Many Relationship table making a Third Table.
The stored procedure currently looks like this but it has some error
DELIMITER $$
CREATE PROCEDURE `test`.UpdateRelatedAccounts()
BEGIN
DECLARE ssn_sel_id VARCHAR(255) DEFAULT 0;
DECLARE id_sel_id CHAR(36) DEFAULT 0;
DECLARE id_sel_rel CHAR(36) DEFAULT 0;
DECLARE no_more_rows BOOLEAN;
DECLARE num_rows INT DEFAULT 0;
DECLARE no_more_rel_rows BOOLEAN;
DECLARE rel_num_rows INT DEFAULT 0;
DECLARE ssn_all_cur CURSOR FOR
SELECT ssn, id FROM ssn WHERE ssn NOT IN ('','000-00-0000');
DECLARE ssn_cur CURSOR FOR
SELECT id FROM ssn WHERE id != id_sel_id AND ssn = ssn_sel_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
-- DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rel_rows = TRUE;
OPEN ssn_all_cur;
SELECT FOUND_ROWS() INTO num_rows;
the_loop: LOOP
FETCH ssn_all_cur
INTO ssn_sel_id, id_sel_id;
IF no_more_rows THEN
CLOSE ssn_all_cur;
LEAVE the_loop;
END IF;
OPEN ssn_cur;
SELECT FOUND_ROWS() INTO rel_num_rows;
the_rel_loop: LOOP
FETCH ssn_cur
INTO id_sel_rel;
IF no_more_rel_rows THEN
CLOSE ssn_cur;
LEAVE the_rel_loop;
END IF;
INSERT INTO `ssn_related` ( `ssn_primary`, `ssn_related` ) VALUES ( id_sel_id, id_sel_rel ), ( id_sel_rel, id_sel_id );
END LOOP the_rel_loop;
END LOOP the_loop;
END$$
DELIMITER ;
How do i nest to use the values and insert into third table.
Cursors are slow and most of the time unnecessary. Nested cursors are slow² and unnecessary². Cursors shall only be used as the last resort, when there's really no other way.
What you want to do can be broken down to this:
INSERT INTO `ssn_related` ( `ssn_primary`, `ssn_related` )
SELECT
ssn_1.id,
ssn_2.id
FROM
ssn ssn_1
INNER JOIN ssn ssn_2 ON ssn_2.ssn = ssn_1.id
WHERE ssn_1.ssn NOT IN ('', '000-00-0000')
AND ssn_2.id != ssn_1.id;
Then you do the same again with swapped columns in the SELECT.
INSERT INTO `ssn_related` ( `ssn_primary`, `ssn_related` )
SELECT
ssn_2.id,
ssn_1.id
FROM
ssn ssn_1
INNER JOIN ssn ssn_2 ON ssn_2.ssn = ssn_1.id
WHERE ssn_1.ssn NOT IN ('', '000-00-0000')
AND ssn_2.id != ssn_1.id;

MySQL Procedure with Cursor Parameter

I have the following code that is supposed to return a list of customers that are younger than the provided age parameter and a total count of found records.
My table consists of: columns ID, FIRST_FIRSTNAME, LASTNAME, DATE_OF_BIRTH
I could have easily made a few mistakes in the syntax, logic and formatting of this procedure but forgive me, I am new to this! The following is my sql procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS sample_procedure$$
CREATE PROCEDURE sample_procedure(IN actualinput INT)
BEGIN
DECLARE no_more_customers int(4);
DECLARE l_customer_count int(4);
DECLARE l_id varchar(1);
DECLARE l_first_name varchar(10);
DECLARE l_last_name varchar(10);
DECLARE l_date_of_birth varchar(20);
DECLARE customer_list varchar(250);
DECLARE dateinput DATE;
SET dateinput=DATE_SUB(now(), interval actualinput year);
DECLARE cid CURSOR FOR
SELECT ID, FIRST_FIRSTNAME, LAST_NAME, DATE_OF_BIRTH
FROM customers WHERE DATE_OF_BIRTH >= dateinput;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_customers=1;
SET no_more_customers=0;
SET l_customer_count=0;
OPEN cid;
cid_cursor: REPEAT
FETCH cid INTO l_id, l_first_name, l_last_name, l_date_of_birth; IF no_more_customers THEN
LEAVE cid_cursor;
END IF;
SET customer_list=concat(customer_list, l_id,',',l_first_name,',',l_last_name,',',l_date_of_birth);
SET l_customer_count=1+l_customer_count;
UNTIL no_more_customers
END REPEAT cid_cursor;
CLOSE cid;
SET no_more_customers=0;
SELECT customer_list AS Customers;
SELECT concat(l_customer_count);
END;
$$
I appear to have an error in the method I used to calculate the age of customers, possibly an issue with the method I used to call the cursor and my final list of customers is only returning null.
Thanks in advance...
I can suppose that variable 'customer_list' should be initialized, e.g. -
...
DECLARE customer_list VARCHAR(250) DEFAULT '';
...
because 'SET customer_list = CONCAT(customer_list, ...)' when customer_list is NULL, will return NULL too.
Take advantage of MySQL Stored Procedure Debugger. Hope this feature will help you.