0

I want to create and use a database/schema by using a variable, so I can change it when I want to run a script on a different Schema.

I have something like this:

SET @VarSchema = 'test';
SET @Front = 'CREATE DATABASE IF NOT EXISTS ';
SET @EndPart = ';';
SET @Stmt = CONCAT(@Front,@VarSchema,@EndPart);
PREPARE stmt FROM @Stmt;
EXECUTE stmt;

SET @Front = 'USE ';
SET @EndPart = ';';
SET @Stmt = CONCAT(@Front,@VarSchema,@EndPart);
PREPARE COMMAND FROM @Stmt;
EXECUTE COMMAND;

But this gives me this error: Error Code: 1295. This command is not supported in the prepared statement protocol yet

EDIT: It complains about this statement:

SET @Front = 'USE ';
SET @EndPart = ';';
SET @Stmt = CONCAT(@Front,@VarSchema,@EndPart);
PREPARE COMMAND FROM @Stmt;
EXECUTE COMMAND;

Is there any way I can do this?

2
  • 1
    Which statement is it complaining about? Commented Sep 1, 2014 at 10:39
  • The second one, ill edit it to show that. Commented Sep 1, 2014 at 10:41

1 Answer 1

1

Since you can't execute the USE statement with a prepared statement, you'll have to prepare all your queries so you can substitute the database name into them.

Sign up to request clarification or add additional context in comments.

1 Comment

It does seem like a strange limitation, but if this is the way I have to do it, then i shall do it like that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.