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 1052675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:07:51+00:00 2026-05-16T17:07:51+00:00

I have the following stored procedure: proc_main:begin declare done tinyint unsigned default 0; declare

  • 0

I have the following stored procedure:

proc_main:begin

declare done tinyint unsigned default 0;
declare dpth smallint unsigned default 0;


create temporary table hier(
 AGTREFERRER int unsigned, 
 AGTNO int unsigned, 
 depth smallint unsigned default 0
)engine = memory;

insert into hier values (p_agent_id, p_agent_id, dpth);

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while done <> 1 do

  if exists( select 1 from agents a inner join hier on a.AGTREFERRER = hier.AGTNO and hier.depth = dpth) then

    insert into hier 
      select a.AGTREFERRER, a.AGTNO, dpth + 1 from agents a
      inner join tmp on a.AGTREFERRER = tmp.AGTNO and tmp.depth = dpth;

    set dpth = dpth + 1;      

    truncate table tmp;
    insert into tmp select * from hier where depth = dpth;

  else
    set done = 1;
  end if;

end while;


select 
 a.AGTNO,
 a.AGTLNAME as agent_name,
 if(a.AGTNO = b.AGTNO, null, b.AGTNO) as AGTREFERRER,
 if(a.AGTNO = b.AGTNO, null, b.AGTLNAME) as parent_agent_name,
 hier.depth,
 a.AGTCOMMLVL
from 
 hier
inner join agents a on hier.AGTNO = a.AGTNO
inner join agents b on hier.AGTREFERRER = b.AGTNO
order by
 -- dont want to sort by depth but by commission instead - i think ??
 -- hier.depth, hier.agent_id; 
 a.AGTCOMMLVL desc;

drop temporary table if exists hier;
drop temporary table if exists tmp;

end proc_main

While the function does its job well – it only currently allows sorting via AGTCOMMLVL descending order. The stored procedure’s purpose is to match a memberID with their parentID and associated COMMLVL. Once paired appropriately,I use the memberID in a second query to return information about that particular member.

I would like to be able to sort by any number of filters but have the following problems:

  1. I can’t seem to find a way to pass a variable into the stored procedure altering its sorting by field.

  2. Even if I could – the sort may actually only contain data from the second query (such as first name, last name, etc)

  3. Running a sort in the second query does nothing even though syntax is correct – it always falls back to the stored procedure’s sort.

any ideas?

EDIT

My php uses mysqli with code:

$sql = sprintf("call agent_hier2(%d)", $agtid);
$resulta = $mysqli->query($sql, MYSQLI_STORE_RESULT) or exit(mysqli_error($mysqli));
  • 1 1 Answer
  • 1 View
  • 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-05-16T17:07:52+00:00Added an answer on May 16, 2026 at 5:07 pm

    If you want to sort by input parameter of the stored procedure, you need to use Prepared staments
    For example,

    DELIMITER //
    CREATE  PROCEDURE `test1`(IN field_name VARCHAR(40) )
    BEGIN
      SET @qr = CONCAT ("SELECT * FROM table_name ORDER BY ",  field_name);
    
      PREPARE stmt FROM @qr;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;
    END //
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.