Solved

CREATE DATABASE

Posted on 2001-07-24
3
235 Views
Last Modified: 2008-03-17
Hi folks,

i am wanting to write a script to create a new database on any server.

i have got this:

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'quotaman')
     DROP DATABASE [quotaman]
GO

declare @FN varchar(128)
declare @LN varchar(128)

SELECT @FN = LEFT(FILENAME, LEN(FILENAME)-10)+'quotaman_data.MDF', @LN = LEFT(FILENAME, LEN(FILENAME)-10)+'quotaman_data.LDF' FROM MASTER.DBO.SYSALTFILES WHERE NAME = 'tempdev'
GO

CREATE DATABASE [quotaman] ON (NAME = 'quotaman_Data', FILENAME = @FN, SIZE = 6, FILEGROWTH = 10%) LOG ON (NAME = N'quotaman_Log', FILENAME = @LN, SIZE = 1, FILEGROWTH = 10%)
GO

but i am always rewarded with

Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near '@FN'.

so how else can i dynamically set the os_file_name?

thanks and regards,

Mike.
0
Comment
Question by:meverest
3 Comments
 
LVL 6

Expert Comment

by:acampoma
ID: 6312475
try using this proc
IF OBJECT_ID('dbo.CreateDatabase') IS NOT NULL
BEGIN
    DROP PROCEDURE dbo.CreateDatabase
    IF OBJECT_ID('dbo.CreateDatabase') IS NOT NULL
        PRINT '<<< FAILED DROPPING PROCEDURE dbo.CreateDatabase >>>'
    ELSE
        PRINT '<<< DROPPED PROCEDURE dbo.CreateDatabase >>>'
END
go
CREATE PROCEDURE CreateDatabase (
@DBName varchar (25),
@DbPath varchar (50),
@LogPath varchar (50)

)
AS
Declare @DBFile varchar (40)
Declare @LogFile  varchar (40)
Declare @exec_str varchar(1000)

   set @DBFile = @DbPath + @DBName + '.mdf'
   set @LogFile =@LogPath+ @DBName + '_log.ldf'

   --print @DBName
   --print @DBFile
   --print @LogFile
   set @exec_str = 'CREATE DATABASE ' + @DBName + '
   ON
   ( NAME = ' + @DBName + '_dat,
     FILENAME = "' + @DBFile + '",
     SIZE = 7,  
     MAXSIZE = unlimited,
     FILEGROWTH = 10% )
   LOG ON
   ( NAME = ' + @DBName + '_log,
     FILENAME = "' + @LogFile + '",
     SIZE = 4MB,  
     MAXSIZE = unlimited,
     FILEGROWTH = 2% )'
   EXEC (@exec_str)
go
IF OBJECT_ID('dbo.CreateDatabase') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE dbo.CreateDatabase >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE dbo.CreateDatabase >>>'
go
0
 
LVL 2

Accepted Solution

by:
Caseys95 earned 50 total points
ID: 6313310
acampomo had it right.

The use of the variables in create statement are not resolved at execution time.

I modified your code to get the result that I think you are looking for.

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'quotaman')
DROP DATABASE [quotaman]
GO

declare @FN varchar(128)
declare @LN varchar(128)
declare @SQL nvarchar(512)

SELECT @FN = IsNull(LEFT(FILENAME, LEN(FILENAME)-10), '')+'quotaman_data.MDF',
       @LN = IsNull(LEFT(FILENAME, LEN(FILENAME)-10), '')+'quotaman_data.LDF'
  FROM MASTER.DBO.SYSALTFILES WHERE NAME = 'tempdev'

Set @SQL = "CREATE DATABASE [quotaman] ON (NAME = 'quotaman_Data', FILENAME = '" + @FN  +
          "', SIZE = 6, FILEGROWTH = 10%) LOG ON (NAME = N'quotaman_Log', FILENAME = '" + @LN +
          "', SIZE = 1, FILEGROWTH = 10%) "

Execute sp_executesql @SQL
GO

0
 
LVL 37

Author Comment

by:meverest
ID: 6315688
acampoma - thanks for your suggestion. i couldn't get it to work, but i didn't try too hard because i wanted to avoid having to remove the stored proc afterward..

i award the points to Caseys95 because this is a most elowuent solution.

thanks all.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now