Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2003
    Posts
    4

    Unanswered: After creating database, running script to create tables?

    I am in my apps master database and I have a Proc to create a project database. However, once that database is created, I have a long script that I need to run to create the tables, indexes, and views.

    What is the best way to run this?

    I'd intended to just make this part of my stored proc -- create database, then tables, then views. However, to simply create the database, I had to build a string to concatenate the passed in value of the new database. I'd hate to have to do this for every line of this script.

    Should I simply take this script as is and create it as a stored proc in the new database and then run that proc?

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Well, you don't need to store it as a stored procedure in order to run it. You could just switch to the new database and run it through Query Analyzer. Alternatively, you could put a USE database statement at the beginning of the script, and that would switch the database focus for you. (You'll want to make sure it succeeds though, because otherwise you could end up creating all your objects in the Master database.)

    Is this supposed to be part of an automated process?

    blindman

  3. #3
    Join Date
    Aug 2003
    Posts
    4
    Originally posted by blindman
    Well, you don't need to store it as a stored procedure in order to run it. You could just switch to the new database and run it through Query Analyzer. Alternatively, you could put a USE database statement at the beginning of the script, and that would switch the database focus for you. (You'll want to make sure it succeeds though, because otherwise you could end up creating all your objects in the Master database.)

    Is this supposed to be part of an automated process?

    blindman
    Yes. I'm trying to allow admin users on our web app the ability to create new projects on the fly. Part of this is creating the new database.

  4. #4
    Join Date
    Aug 2003
    Posts
    4
    Originally posted by blindman
    Well, you don't need to store it as a stored procedure in order to run it. You could just switch to the new database and run it through Query Analyzer. Alternatively, you could put a USE database statement at the beginning of the script, and that would switch the database focus for you. (You'll want to make sure it succeeds though, because otherwise you could end up creating all your objects in the Master database.)

    Is this supposed to be part of an automated process?

    blindman
    Bummer: Server: Msg 154, Level 15, State 1, Procedure CreateclaimDexDB_SP, Line 22
    a USE database statement is not allowed in a procedure or trigger.

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    No, USE is not allowed in a procedure or trigger. I've gotten around that in the past by creating dynamic SQL statements, but I imagine your code is rather long for that.

    Personally, I think this is only one of the drawbacks you will encounter by creating separate databases for each project. By far the easiest way to handle an application like this is to add scalability to your database to allow it to handle multiple projects. This will make it much faster and easier to add, delete, and manage projects. It will also prevent a lot of duplicated information between databases which are sure to get our of synch, and will allow for powerful comparison analysis between projects.

    I think you are heading into an administrative nightmare by creating separate databases, and this problem that you have now is just the tip of the iceberg.

    It is also not a good idea to be storing procedures like this in the master database. If you must, then create a separate database to store the procedures involved in creating project databases.

    Other options include using SQL to load a template database from a backup file. The template database could have all your objects already stored.

    ...or your front end application could switch focus to the new database before executing the procedure for creating all the objects.

    blindman

  6. #6
    Join Date
    Aug 2003
    Posts
    4
    Originally posted by blindman
    No, USE is not allowed in a procedure or trigger. I've gotten around that in the past by creating dynamic SQL statements, but I imagine your code is rather long for that.

    Personally, I think this is only one of the drawbacks you will encounter by creating separate databases for each project. By far the easiest way to handle an application like this is to add scalability to your database to allow it to handle multiple projects. This will make it much faster and easier to add, delete, and manage projects. It will also prevent a lot of duplicated information between databases which are sure to get our of synch, and will allow for powerful comparison analysis between projects.

    I think you are heading into an administrative nightmare by creating separate databases, and this problem that you have now is just the tip of the iceberg.

    It is also not a good idea to be storing procedures like this in the master database. If you must, then create a separate database to store the procedures involved in creating project databases.

    Other options include using SQL to load a template database from a backup file. The template database could have all your objects already stored.

    ...or your front end application could switch focus to the new database before executing the procedure for creating all the objects.

    blindman
    I appreciate your comments, but in this case, separate databases is the right way to go. They truly are for distinct projects, clients, etc. and CAN be scaled across different servers if need be. In addition, some tables will start out the same, but can be altered by each client independently. This is tying into another existing application.

    When I said master DB, I forgot there was a SQL database called master. I meant the main application database, which keeps track of the different projects, users, clients, etc.

    As a general rule, I would agree with you, but in this case, we're doing the right thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •