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

  • Home
  • SEARCH
  • 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 9180489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:04:06+00:00 2026-06-17T18:04:06+00:00

I have a MySql function, which takes in 3 arguments. The last argument is

  • 0

I have a MySql function, which takes in 3 arguments. The last argument is a query itself, which I can execute with PREPARE(?), at least if it does not have multiple results. I want to loop through all results returned from the PREPARED statement.

How can I do that? I was thinking of a CURSOR, but all I can find is that it is impossible to use CURSORs on a PREPARED variable statement.

What I want to achieve is the following:
1. I have a view of results in my application. The results are paged.
2. I want to search for a particular row, find the page it belongs on, and put it into view.
3. The view of results can be filtered and ordered in several ways, so therefore the third argument of the MySql function is the query the view of results is filled with.

Hopefully I made myself clear, otherwise let me know.

So far I have the following:
DELIMITER $$ # Otherwise you cannot use semicolons to end a line

/*
The bar graph (clsBarGraph) shows cows in pages.
If you wish to search for a page with a certain Cow ID you need to do alot of things.
Therefore this function is created, to do the heavy lifting all in the database engine.accessible
@param LongCowID The long cow ID of the cow you wish to get the page for
@param ItemsPerPage To determine on what page a cow comes, it is necessary to known how many cows will fit into a page
@param SelectQuery The query that was used to view the data in the BarGraph. This determines ordering, which cows to have in the resultset to limit on, etc.
        This should be without the limit
@return The page number to set the view to, or -1 if the cow does not exist.
*/
CREATE FUNCTION `GetPageForCowID`(LongCowID INT, ItemsPerPage INT, SelectQuery VARCHAR(255))
RETURNS INT
BEGIN
DECLARE `page` INT;

/* Prepares queries to execute */
PREPARE stmt_CheckIfCowExists FROM 'SELECT COUNT(`Long_Cow_ID`) as `rows` FROM `cow_data` INTO @NumberOfRows';
EXECUTE stmt_CheckIfCowExists;
IF @NumberOfRows = 1 THEN 
    /* The cow does nto exist */
    SET `page` = -1;
ELSE
    /* The cow does exist */
    /* Get all the cows used in the view of the data, without a limit and put it into a variable */
    SET @SelectQuery = CONCAT(@SelectQuery, ' INTO @CowsForDataView');
    PREPARE stmt_SelectDataForView FROM @SelectQuery;
    EXECUTE stmt_SelectDataForView;
    SELECT COUNT(*) FROM stmt_SelectDataForView;

    DEALLOCATE PREPARE stmt_SelectDataForView;
END if;

/* Clean Up */
deallocate PREPARE stmt_CheckIfCowExists;
return `page`;
END

thanks in advance.

  • 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-17T18:04:07+00:00Added an answer on June 17, 2026 at 6:04 pm

    Put the results into a temporary table:

    SET @sql := CONCAT('CREATE TEMPORARY TABLE tmp_GetPageForCowID ', SelectQuery);
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    Then one can iterate a cursor over the content of the temporary table.

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

Sidebar

Related Questions

Do MySQL have any function by which I can split data of a column
I want to have a SelectAll function which takes in a few arguments (class,
I am writing a PHP/MYSQL search function which takes a users search query (as
I have a PHP function which inserts multiple records into MySQL: function commit_purchase($asset_type_ID, $org_ID,
I have a problem regarding the datediff MYSQL function, I can use it and
I have created a simple function which takes user id and show total no.
i have a function (below) which is used in my mysql abstraction class and
I have a function which takes two arrays containing the tokens/words of two texts
So I have this function that takes all the users from an MySql table
I have develop this function which uses recursion to call itself. I need to

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.