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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:26:03+00:00 2026-06-03T00:26:03+00:00

While pondering a potential solution to another question on SO, I started considering using

  • 0

While pondering a potential solution to another question on SO, I started considering using a prepared statement within a procedure (no dynamic SQL in functions makes me sad). The naive approach of executing a PREPARE in the procedure seems inefficient, as the statement only needs to be prepared the first time the procedure is called during a session. Is it possible in MySQL to prepare a statement only if it doesn’t already exist? Is there a way of checking for the existence of a prepared statement?

Here’s a sample procedure for people to play with. It creates a statement that SELECTs all columns of the given type from the given table.

DROP PROCEDURE IF EXISTS select_columns_of_type_statement;
delimiter //
CREATE PROCEDURE select_columns_of_type_statement(IN db VARCHAR(255), IN tbl VARCHAR(255), IN type VARCHAR(255), OUT result VARCHAR(4095))
  READS SQL DATA
  NOT DETERMINISTIC -- as tables can be ALTERed
BEGIN
    SET @scots_db=db, @scots_tbl=tbl, @scots_type=type;
    PREPARE generate_scots FROM "SELECT CONCAT(
          'SELECT ',
          GROUP_CONCAT(COLUMN_NAME SEPARATOR ', '),
          ' FROM `', ?, '`.`', ?, '` '
          )
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND data_type=?
      INTO @scots_stmt";
    EXECUTE generate_scots USING @scots_db, @scots_tbl, @scots_db, @scots_tbl, @scots_type;
    SET result = @scots_stmt;
END //
delimiter ;

Example usage:

CALL select_columns_of_type_statement('mysql', 'user', 'int', @stmt);
SELECT @stmt;
-- or even:
SET @stmt=CONCAT(@stmt, 'LIMIT 3');
PREPARE user_int_cols FROM @stmt;
EXECUTE user_int_cols;

That user variables must be used so much also makes me sad.

  • 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-03T00:26:04+00:00Added an answer on June 3, 2026 at 12:26 am

    Checking existence may not be possible, but the try-and-handle-failure pattern will work. DECLARE an error handler that defines & executes the prepared statement, and execute the prepared statement outside of the handler. If the statement doesn’t exist, the error handler will run. It’s not pretty, but it works.

    DROP PROCEDURE IF EXISTS select_columns_of_type_statement;
    delimiter //
    CREATE PROCEDURE select_columns_of_type_statement(IN db VARCHAR(255), IN tbl VARCHAR(255), IN type VARCHAR(255), OUT result VARCHAR(4095))
      READS SQL DATA
      NOT DETERMINISTIC -- as tables can be ALTERed
    BEGIN
        DECLARE CONTINUE HANDLER
          FOR 1243
          BEGIN
            PREPARE generate_scots FROM "SELECT CONCAT(
                  'SELECT ',
                  GROUP_CONCAT(COLUMN_NAME SEPARATOR ', '),
                  ' FROM `', ?, '`.`', ?, '` '
                  )
              FROM INFORMATION_SCHEMA.COLUMNS
              WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND data_type=?
              INTO @scots_stmt";
            EXECUTE generate_scots USING @scots_db, @scots_tbl, @scots_db, @scots_tbl, @scots_type;
          END;
        SET @scots_db=db, @scots_tbl=tbl, @scots_type=type;
        EXECUTE generate_scots USING @scots_db, @scots_tbl, @scots_db, @scots_tbl, @scots_type;
        SET result = @scots_stmt;
    END //
    delimiter ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was pondering this question last night while debugging some things in IE7, when
I have been pondering about my homework question for a while. I welcome (and
I am pondering about partial specialization . While I understand the idea, I haven't
Heres something I've been pondering for a while ... Do you struggle with the
Something I have been wondering about properties for a while. When you are using
I am wondering about potential issues with – alternatives to - caching using an
We've built up thousands of product urls while using Magento as our cart application
I haven't accessed data using SqlCommand etc. for a while as I tend to
I have been pondering on this for a while. Consider a web application of
So I've been using System.Net.Mail.MailMessage objects for sending e-mail via SmtpClient for a while

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.