Flash
Flash

Reputation: 3021

Creating tables - Write a script

Hii ,

I am a bit new to SQL and would like some help to solve the following problem.

I have a database X which contains a table TABLE with column named domain.I retrieve these values and create one database for each value with that value as the database name . For example , if the domain has the values value1, value2, value3 there should be three databases created with names value1 , value2 and value3...and when i add a value to the TABLE in database X , a new database should be created with the value X.TABLE.domainvalue ... I need to write an SQLScript that accomplishes this.

Any help would be appreciated.

Links that would help me understand better are welcome.

Upvotes: 0

Views: 85

Answers (1)

LaGrandMere
LaGrandMere

Reputation: 10359

Try this :

CREATE PROCEDURE procCreateDataBase()
BEGIN
  DECLARE domainName CHAR(50);
  DECLARE cur1 CURSOR FOR SELECT domain FROM databaseX.domainNames;

  OPEN cur1;

  LOOP
    FETCH cur1 INTO domainName;
      CREATE DATABASE IF NOT EXISTS domainName;
  END LOOP;

  CLOSE cur1;
END;

This is a stored procedure you can call anytime and that will create your databases depending on the domains found, if they don't already exist.

Upvotes: 1

Related Questions