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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:45:55+00:00 2026-06-01T09:45:55+00:00

I am setting a variable, and then I want to use the USE command

  • 0

I am setting a variable, and then I want to use the USE command to change the database. Unfortunately USE doesn’t evaluate the variable.

SET @con="testDB";
Query OK, 0 rows affected (0.00 sec)
select @con;
+--------+
| @con   |
+--------+
| testDB |
+--------+
1 row in set (0.00 sec)
use @con;
ERROR 1049 (42000): Unknown database '@con'

So the value of the variable is not evaluated when I try to connect. 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-01T09:45:57+00:00Added an answer on June 1, 2026 at 9:45 am

    I have found my own solution, so I am going to answer my own question, in case anybody is interested.

    @outis you are right, it’s not possible to use a variable with the command USE.

    However, due to the fact that I want to create a table in a database specified at runtime, my solution would be to use dynamic SQL:

    set @schema="testDB";
    set @front="CREATE TABLE IF NOT EXISTS ";
    set @endpart=".`TEST1` (DIM_ID INT(16)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_unicode_ci;";
    set @stat=concat(@front,@schema,@endpart);
    prepare command from @stat;
    execute command;
    

    So basically this solution builds up the statement, prepares it and executes it.
    @schema parameter can even be past down to the script. This way I dynamically build the create statement.

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

Sidebar

Related Questions

I am copying a file to a directory, and then setting an environment variable
I must be overlooking something simple. I'm setting a variable from a query result
How can I have a dynamic variable setting the amount of rows to return
I've got a choose statement that should be setting my variable but for some
I am working in flex and I am just setting a variable to false.
Simply setting the SVN_EDITOR variable to mate does not get the job done. It
I have variable @count of datatype int.I am setting values to this @count. I
I am trying to include a variable in a .ini file setting by surrounding
I am having a problem setting the Authorize attribute Role value from a variable.
I have a text box on a form called txtNID. I want to use

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.