Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:11:20+00:00 2026-06-14T19:11:20+00:00

I’ve been Googleing around for a while and I am sure that the problem

  • 0

I’ve been Googleing around for a while and I am sure that the problem is that I don’t understand clearly how CURSORs in MySQL work.

A short explanation of the problem: I’m writing such function (simplified):

CREATE DEFINER=`me`@`localhost` FUNCTION `product_move`(prID INT, tr_type VARCHAR(2), clID INT, am INT, dnID INT, usrID INT, price FLOAT(10,2), ti DATETIME, barc TINYTEXT, cmt TINYTEXT, lnID INT)
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE cur_id INT;
    DECLARE net_pr FLOAT(10,2);
    DECLARE cur_r INT;
    DECLARE remaind INT DEFAULT 0;
    DECLARE avg_price FLOAT(10,2) DEFAULT 0;
    DECLARE curs CURSOR FOR SELECT  `products_transactionsID`,
                                                `price`,
                                                `remains`
                                        FROM `products_transactions`
                                        WHERE `productID`=prID AND `remains`>0 AND `type`='V'
                                        ORDER BY `products_transactionsID` ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
 OPEN curs;
           SET remaind=am;
           read_loop:LOOP
                FETCH curs INTO cur_id,net_pr,cur_r;
                 IF done THEN
                  LEAVE read_loop;
                END IF;
                IF (cur_r>=remaind) THEN
                    SET avg_price = avg_price + (net_pr * remaind);
                    UPDATE `products_transactions` SET `remains`=`remains`-remaind WHERE products_transactionsID=cur_id;
                    LEAVE read_loop;
                ELSE

                    SET avg_price = avg_price + (net_pr * cur_r);
                    SET remaind=remaind-cur_r;
                    UPDATE `products_transactions` SET `remains`=0 WHERE products_transactionsID=cur_id;
                END IF;
           END LOOP;

CLOSE curs;
           SET avg_price=avg_price/am;
 INSERT INTO products_transactions
                       (`products_transactionsID`,`clientID`,`date_created`,`delivery_notesID`,`type`,`productID`,`amountIN`,`amountOUT`,`barcodes`,`in_stock`,`out_stock`,`out_repair`,`out_loss`,`booked`,`ordered`,`userID`,`price`,`comments`,`fifo_buy_price`)
                   SELECT NULL, clID, ti, dnID , tr_type, prID, 0, am, barc, products_transactions.in_stock-am, products_transactions.out_stock,
                       products_transactions.out_repair, products_transactions.out_loss, products_transactions.booked, products_transactions.ordered,usrID,price,cmt,avg_price
                   FROM
                       products_transactions WHERE productID=prID ORDER BY products_transactionsID DESC LIMIT 1;

So, we insert a new row in this table, based upon some calculations from the previously selected rows and updating these rows meanwhile.

The problem is with the avg_price variable, which should be calculated based on the net_pr variable which is FETCH’ed from the cursor. But somehow, instead of being FETCH’ed from the SELECT, the net_pr variable takes the value of the price input parameter of my function! How is that possible?

My guesses have been so far:

  • a variable name conflict? Searched through the code but I can’t find any.

  • updating the table within the LOOP could make the CURSOR loose its position? It would make sense, but that wouldn’t result in this, either…

I’d apreciate any ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:11:21+00:00Added an answer on June 14, 2026 at 7:11 pm

    Two things that I can see:

    1) Don’t update the table that you’re using in the cursor. MySQL says the cursor is read only but I wouldn’t trust this. Set your value, exit the cursor, and then update the table.

    2) Using the same name for a variable in the proc definition and a column in a select gives a conflict: http://dev.mysql.com/doc/refman/5.0/en/local-variable-scope.html

    “A local variable should not have the same name as a table column. If an SQL statement, such as a SELECT … INTO statement, contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable. “

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have been unable to fix a problem with Java Unicode and encoding. The
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.