-1

I want to create database using subquery's return value name

CREATE DATABASE (SELECT (REPLACE( REPLACE( REPLACE( standard_name, ' ', '' ), '-', '' ), '_', '' )) FROM standard_master WHERE _id = 1)

My Subquery return value

SELECT (REPLACE( REPLACE( REPLACE( standard_name, ' ', '' ), '-', '' ), '_', '' )) FROM standard_master WHERE _id = 1

standard01

Create database query give me an syntax error

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(SELECT (REPLACE( REPLACE( REPLACE( standard_name, ' ', '' ), '-', '' ), '_', ''' at line 1

Vishal Bareja
  • 128
  • 2
  • 11

1 Answers1

1

I assume that you are trying to create a database by fetching database name from a query. You can use a variable as :

  • declare a variable and store the output of your query
  • use variable in dynamic sql to create a database.

example:

SELECT @id := (REPLACE( REPLACE( REPLACE( standard_name, ' ', '' ), '-', '' ), '_', '' )) FROM standard_master WHERE _id = 1;
# database name is in the @id variable
SET @SQL = CONCAT('CREATE DATABASE ', @id);
PREPARE stmt FROM @SQL;
# execute the statement that you have created
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

credit: I have used https://dba.stackexchange.com/questions/14397/creating-a-table-from-variable-names as reference for dynamic sql.

Hope it answers your query.

Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34