Solved

CREATE DATABASE

Posted on 2001-07-24
3
246 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

813 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

15 Experts available now in Live!

Get 1:1 Help Now