Creating a database on the fly using a T-SQL stored procedure

  • I'm having problems creating a T-SQL stored procedure that creates a new SQL Server 2000 database on the fly from my ASP.NET Web application. I'm trying to pass in a parameter (the name of the database) to the procedure and have my CREATE DABASE DATABASENAME statement receive the value of the parameter. Here is my code:

    CREATE PROCEDURE DBO.CREATE DATABASE

    @DBNAME VARCHAR (50)

    AS

    CREATE DATABASE @DBNAME

    GO

    The procedure works If I hard code the name of the database but not when I pass in the parameter to become the name of the database. Anybody's help is greatly appreciated. Thanks!

     

  • Try something like this:

    Create Procedure dbo.Create_Database

    @DBNAME VARCHAR(50)

    AS

    DECLARE @SQL VARCHAR(100)

    SELECT @SQL = 'CREATE DATABASE ' + @DBNAME

    EXEC(@SQL)

    RETURN

  • Thanks for the quick response helping me get my SQL Server stored procedure working!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply