MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 30th, 2012, 11:19 PM
besau_89 besau_89 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 besau_89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 40 sec
Reputation Power: 0
[Help] Call store procedure within cursor (SP use input parameters from cursor)

Hi all,

I've got a error below when i tried to call store procedure within cursor. Please show me what happen. Tks so much!
DROP PROCEDURE IF EXISTS prestashop.uspRetrieveData;
CREATE PROCEDURE prestashop.`uspRetrieveData`()
READS SQL DATA
BEGIN

DECLARE listpinIDs VARCHAR(255);
DECLARE orderid INT;
DECLARE done INT DEFAULT FALSE;
DECLARE c CURSOR FOR SELECT listpinID, preorderid FROM ps_payoo_preorder WHERE listpinid IS NOT NULL AND preorderid IS NOT NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP TEMPORARY TABLE IF EXISTS tmpOrderMoneyTotal;
CREATE TEMPORARY TABLE `tmpOrderMoneyTotal`(`preorderid` int default NULL, `MoneyTotal` float default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
OPEN c;
READLOOP: LOOP
FETCH c INTO listpinIDs, orderid;
IF done THEN
LEAVE READLOOP;
ELSE
SET @sqlStr = CONCAT('SELECT SUM(Price) INTO @MoneyTotal FROM ps_payoo_cards Card WHERE payoo_card_id IN (', listpinIDs, ')');

PREPARE stmt FROM @sqlStr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

INSERT INTO tmpOrderMoneyTotal(preorderid, MoneyTotal)
SELECT orderid, @MoneyTotal;
END IF;
END LOOP;
CLOSE c;

SELECT * FROM tmpOrderMoneyTotal;

END;

Error: 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 1

Reply With Quote
  #2  
Old October 1st, 2012, 06:16 AM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
are you creating this in the mysql client? if so it is necessary to change the delimiter from ; to something else in order to use ; within your SP.

so at the top add DELIMITER & or some other character you aren't going to use in your SP at the bottom you'll need to type & for the SP to know you are finished and then type in DELIMITER ; to switch back.

Reply With Quote
  #3  
Old October 1st, 2012, 11:33 PM
besau_89 besau_89 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 besau_89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 40 sec
Reputation Power: 0
Hi,
I added delimiter at the top and bottom of SP. it executed successful.But when I call "CALL uspRetrieveData()". I still get this error Are something wrong when I call @strStr winthin cursor declaration?


delimiter //
DROP PROCEDURE IF EXISTS prestashop.uspRetrieveData;
CREATE PROCEDURE prestashop.`uspRetrieveData`()
READS SQL DATA
BEGIN

DECLARE v_listpinIDs VARCHAR(255);
DECLARE v_orderid INT;
DECLARE v_done INT DEFAULT FALSE;
DECLARE v_c CURSOR FOR SELECT listpinID, preorderid FROM ps_payoo_preorder WHERE listpinid IS NOT NULL AND preorderid IS NOT NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
DROP TEMPORARY TABLE IF EXISTS tmpOrderMoneyTotal;
CREATE TEMPORARY TABLE `tmpOrderMoneyTotal`(`preorderid` int default NULL, `MoneyTotal` float default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
OPEN v_c;
READLOOP: LOOP
FETCH v_c INTO v_listpinIDs, v_orderid;
IF v_done THEN
LEAVE READLOOP;
ELSE
SET @sqlStr = CONCAT('SELECT SUM(Price) INTO @MoneyTotal FROM ps_payoo_cards Card WHERE payoo_card_id IN (', v_listpinIDs, ')');
#SELECT @sqlStr;
#SELECT v_listpinIDs;
PREPARE stmt FROM @sqlStr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
INSERT INTO tmpOrderMoneyTotal(preorderid, MoneyTotal)
SELECT v_orderid, @MoneyTotal;
END IF;
END LOOP;
CLOSE v_c;

SELECT * FROM tmpOrderMoneyTotal;
END //
delimiter;

#CALL uspRetrieveData();

Tks so much

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > [Help] Call store procedure within cursor (SP use input parameters from cursor)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap