The CREATE DATABASE statement doesn't accept a parameter, so you'll need to use dynamic SQL.
<pre>CREATE PROCEDURE usp_CreateDatabase
@DatabaseName sysname
AS
DECLARE @sqlcmd NVARCHAR(4000)
SET @sqlcmd = 'CREATE DATABASE [' + @DatabaseName + ']'
EXEC (@sqlcmd)
GO</pre>
You'll also need to run the procedure as a login that has CREATE DATABASE rights. Either by logging into the instance with a login which has the rights or by using the EXECUTE AS statement within the procedure like is shown below.
<pre>CREATE PROCEDURE usp_CreateDatabase
@DatabaseName sysname
AS
DECLARE @sqlcmd NVARCHAR(4000)
SET @sqlcmd = 'CREATE DATABASE [' + @DatabaseName + ']'
EXECUTE AS LOGIN = 'SomeLogin'
EXEC (@sqlcmd)
REVOKE
GO</pre>
Last Wiki Answer Submitted: March 6, 2011 9:43 am by Denny Cherry64,505 pts.
All Answer Wiki Contributors: Denny Cherry64,505 pts.
If you live outside the United States, by submitting your email address you consent to having your personal data transferred to and processed in the United States.