Using a variable to create a database

  • I am running Microsoft SQL Server 2000.  Im trying to create a stored procedure that when run, creates a database with same name as the passed input variable(i.e user).  The purpose of the stored procedure is to create an individual database for each user and give that user permissions to his db only.  We are training and want each user to have his own db.

    My problem is this:  the create database command appears to not like a variable for the name/filename for the primary data file.

    Is this even possible?

  • You will have to use the variable name to create a dynamic SQL string and use sp_executesql. 

    declare @DBname varchar(20),

     @sqlstring nvarchar (4000)

    set @dbname = 'testme2'

    set @sqlstring = N'create database ' +@dbname

    execute sp_executesql @sqlstring

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

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

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