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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:14:32+00:00 2026-05-16T07:14:32+00:00

I was wondering if it is possible in a mysql stored-function or stored-procedure compose

  • 0

I was wondering if it is possible in a mysql stored-function or stored-procedure compose a mysql query as a string variable you can execute later? I have a stored-function get_district_part ((district_id INT,county_id INT,city_id INT,zip_id INT)) RETURNS INT that references the table:

CREATE TABLE IF NOT EXISTS `modx`.covereage_district_part(
    id      INT     NOT NULL    AUTO_INCREMENT  PRIMARY KEY,
    districtID  INT     NOT NULL,
    countyID    INT,
    cityID      INT,
    zipID       INT,
    FOREIGN KEY (districtID) REFERENCES `modx`.coverage_district (id),
    FOREIGN KEY (countyID) REFERENCES `modx`.coverage_county (id),
    FOREIGN KEY (cityID) REFERENCES `modx`.coverage_city (id),
    FOREIGN KEY (zipID) REFERENCES `modx`.coverage_zip (id)
);

get_distrct_part is meant to return the id of rows mathing district_id and some combination of county_id, city_id, and zip_id. The thing is I want to return the id of the row matching the exact combination of ids not the ids of rows that contain any of those ideas. To do that, I am wanting to segment my query statement so it is built specific to the ids provided. I am trying to not have to match nulls if I can.

I realize this can be easily done with PHP, but I would like the do this as mysql stored process if I can for no other reason then all the other functions for this are stored processes.

  • 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-05-16T07:14:32+00:00Added an answer on May 16, 2026 at 7:14 am

    Yes, you can PREPARE and EXECUTE a string as dynamic SQL in a stored procedure.

    delimiter //
    CREATE PROCEDURE get_district_part (district_id INT, county_id INT, city_id INT, zip_id INT)
    BEGIN
      SET @query = 'SELECT id FROM covereage_district_part WHERE 1=1';
    
      IF (district_id IS NOT NULL) THEN
        SET @query = CONCAT(@query, ' AND districtID=', district_id);
      END IF;
    
      IF (county_id IS NOT NULL) THEN
        SET @query = CONCAT(@query, ' AND countyID=', county_id);
      END IF;
    
      IF (city_id IS NOT NULL) THEN
        SET @query = CONCAT(@query, ' AND cityID=', city_id);
      END IF;
    
      IF (zip_id IS NOT NULL) THEN
        SET @query = CONCAT(@query, ' AND zipID=', zip_id);
      END IF;
    
      PREPARE stmt1 FROM @query;
    
      EXECUTE stmt1;
    END //
    delimiter ;
    
    call get_district_part(1,1,1,1);
    call get_district_part(1,1,null,null);
    

    note: You cannot, however, execute dynamic SQL in a stored function. Your question mentions declaring your routine with a RETURNS clause which would be a stored function.

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

Sidebar

Related Questions

No related questions found

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.