I am not a SQL guru, nor do I know much about DB administration
I am trying to write a stored procedure that will create DBs on our SQL2000 machine.
I have two syntax errors which I believe won't let me accomplish what I want... I want to confirm that what I am trying to do is do able or not.
It seems like it doesn't like me using variables for the "CREATE DATABASE" command (if I remove the '@' it doesn't complain). It also doesn't like the
statement where I try to USE 'newdatabase' it complains if I use the variable or it complains the DB doesn't exist. Is what I am trying to do possible?
CREATE PROCEDURE sp_CreateDB
@dbname char(10),
@pass char(10)
AS
CREATE DATABASE @dbname
ON
( NAME = '@dbname',
FILENAME = 'e:\SQL\MSSQL\data\@dbname.mdf',
SIZE = 10,
MAXSIZE = 100MB,
FILEGROWTH = 10MB )
LOG ON
( NAME = '@dbname',
FILENAME = 'd:\SQL\MSSQL\data\@dbname.ldf',
SIZE = 5MB,
MAXSIZE = 20MB,
FILEGROWTH = 5MB )
GO
EXEC sp_addlogin '@dbname', '@pass'
GO
USE @dbname
GO
sp_changedbowner '@dbname''
EXEC sp_defaultdb '@dbname'', '@pass'
GO
I have two syntax errors which I believe won't let me accomplish what I want... I want to confirm that what I am trying to do is do able or not.
It seems like it doesn't like me using variables for the "CREATE DATABASE" command (if I remove the '@' it doesn't complain). It also doesn't like the
statement where I try to USE 'newdatabase' it complains if I use the variable or it complains the DB doesn't exist. Is what I am trying to do possible?
CREATE PROCEDURE sp_CreateDB
@dbname char(10),
@pass char(10)
AS
CREATE DATABASE @dbname
ON
( NAME = '@dbname',
FILENAME = 'e:\SQL\MSSQL\data\@dbname.mdf',
SIZE = 10,
MAXSIZE = 100MB,
FILEGROWTH = 10MB )
LOG ON
( NAME = '@dbname',
FILENAME = 'd:\SQL\MSSQL\data\@dbname.ldf',
SIZE = 5MB,
MAXSIZE = 20MB,
FILEGROWTH = 5MB )
GO
EXEC sp_addlogin '@dbname', '@pass'
GO
USE @dbname
GO
sp_changedbowner '@dbname''
EXEC sp_defaultdb '@dbname'', '@pass'
GO