473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using local VAR as FILENAME in CREATE DB Statement

1
I am ripping my hair out here trying to do something on a SQL Server 2000 box using TSQL that is a breeze in SQL 2005 but I can't make it work.

I am trying to do this ... VERSION 1

SET @DatabasePath = N'D:\DATA\Maintenance\'
SET @LOGBackupPath = N'E:\LOGS\Maintenance\'
SET @Retention = 1

/* modify NOTHING below this line !!!!! */

SET @TableName = @DatabasePath + N'Maintenance.mdf'
SET @LogFileName = @DatabasePath + N'Maintenance_log.ldf'

CREATE DATABASE [Maintenance] ON (NAME = N'Maintenance', FILENAME = @TableName, SIZE = 2, FILEGROWTH = 10%)
LOG ON (NAME = N'Maintenance_log', FILENAME = @LogFileName , SIZE = 1, FILEGROWTH = 10%)
COLLATE Latin1_General_CI_AS
GO

instead of this ... VERSION 2


CREATE DATABASE [Maintenance] ON (NAME = N'Maintenance', FILENAME = 'D:\DATA\Maintenance\Maintenance.mdf' , SIZE = 2, FILEGROWTH = 10%)
LOG ON (NAME = N'Maintenance_log', FILENAME = 'E:\LOGS\Maintenance\Maintenance_log.ldf' , SIZE = 1, FILEGROWTH = 10%)
COLLATE Latin1_General_CI_AS
GO

Version 2 works, version 1 will not compile '

it gives error

Line 25: Incorrect syntax near '@TableName'.


In Sql server 2005 I'd just use $(drive) $(Directory) etc ... after setting the values of course with setvar:

I need to be able to distribute this to customers for building backup jobs that most of them seem totally unable to do well .... since each customer is configured slightly differently I want them to change the paths and drives etc at the top of the script.

How can I use local variables such as @TableName as the FILENAME part of the CREATE DATABASE Statement ????

(The database is needed as an audit for the backup process created later as most of my customers are pharmacueticals)
May 22 '07 #1
2 2389
almaz
168 Expert 100+
I am ripping my hair out here trying to do something on a SQL Server 2000 box using TSQL that is a breeze in SQL 2005 but I can't make it work...
Try this:
Expand|Select|Wrap|Line Numbers
  1. SET @DatabasePath = N'D:\DATA\Maintenance\'
  2. SET @LOGBackupPath = N'E:\LOGS\Maintenance\'
  3. SET @Retention = 1
  4.  
  5. /* modify NOTHING below this line !!!!! */
  6.  
  7. SET @TableName = @DatabasePath + N'Maintenance.mdf'
  8. SET @LogFileName = @DatabasePath + N'Maintenance_log.ldf'
  9.  
  10. exec('
  11. CREATE DATABASE [Maintenance] ON (NAME = N''Maintenance'', FILENAME = ''' + @TableName + ''', SIZE = 2, FILEGROWTH = 10%)
  12. LOG ON (NAME = N''Maintenance_log'', FILENAME = ''' + @LogFileName + ''', SIZE = 1, FILEGROWTH = 10%)
  13. COLLATE Latin1_General_CI_AS')
  14. GO
May 22 '07 #2
if i use ''' + $<varname> + ''' for filename as in the example above, i get the following error:

Msg 5105, Level 16, State 2, Line 35
A file activation error occurred. The physical file name '' +@mdf_path+'' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 1802, Level 16, State 1, Line 35
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

How can i resolve this? some other place i found i should use N'<fname>' but in this case how do i add N? my variables are declared as nvarchar

Thanks,
Nisha
Jul 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Adam | last post by:
I have an application which interacts with a webserver over https using client certificates. Due to a bug in openssl 0.9.6, I upgraded to 0.9.7 and rebuilt python. Now, when I access the page...
3
by: Terri | last post by:
I'm using xp_cmdshell to output a text file from a trigger like this CREATE TRIGGER ON tblApplications FOR INSERT AS DECLARE @FirstName varchar(75) DECLARE @LastName varchar(75) Declare...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
3
by: Loane Sharp | last post by:
Hi there I use the FileStream object to download a zip file over the internet to my local disk. The file downloads successfully, but when I attempt to unzip it, I'm told that the file is in use...
1
by: Aaron West | last post by:
Try this script to see what queries are taking over a second. To get some real output, you need a long-running query. Here's one (estimated to take over an hour): PRINT GETDATE() select...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
by: =?Utf-8?B?Sm9obkJhdGVz?= | last post by:
Problem: I need to backup and clear the security event log. I have this working via a vbsscript which I will post below. However while I can use this script manually it is not user friendly and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.