SQL Server Backup Command with a Variable

A few weeks back I blogged how to do CONTAINSTABLE() and FREETEXTTABLE() using SQL variables.  This weekend, I was working on creating a backup script for a client and ran into a similar problem where the SQL Server 2005 documentation says you can use a variable but it didn't actually work because of a crucial missing detail.

For this script:

declare @databaseName nvarchar

select @databaseName = 'DbProTesting'

backup database @databaseName
to disk = 'c:\temp\DbProTesting.bak'

I got the following weird SQL error:

Msg 911, Level 16, State 11, Line 5
Could not locate entry in sysdatabases for database 'D'. No entry found with that name. Make sure that the name is entered correctly.
Msg 3013, Level 16, State 1, Line 5
BACKUP DATABASE is terminating abnormally.

Of course there's no database named "D" in sysdatabases but why does SQL Server 2005 think that that's what the database name should be?!

Solution: If you change the variable data type from "nvarchar" to "nvarchar(1024)" it works fine.

Here's the working script:

declare @databaseName nvarchar(1024)

select    @databaseName = 'DbProTesting'

backup database @databaseName
to disk = 'c:\temp\DbProTesting.bak'

-Ben

posted @ Monday, August 13, 2007 9:37 AM

Print

Comments on this entry:

# re: SQL Server Backup Command with a Variable

Left by Ryan at 8/13/2007 4:34 PM
Gravatar
Perhaps you should just condense all these tips into: "Don't use nvarchar as a type for a variable because when you select something into it it only grabs the first character." Thanks for the heads up there though.

# re: SQL Server Backup Command with a Variable

Left by Ben Day at 8/13/2007 4:54 PM
Gravatar
Ha! I was thinking the same thing. I'm sure that I'm going to keep finding more of these "change to nvarchar(1024)" issues again and again. I'll just keep writing blog entries to keep the list up to date.

# re: SQL Server Backup Command with a Variable

Left by stm at 11/5/2007 8:24 AM
Gravatar
Try this for more fun:

declare @fileName nvarchar( max );
declare @fileName2 nvarchar( 32 );
set @fileName = 'c:\nvarcharmax.bak';
set @fileName2 = 'c:\nvarchar.bak';
--
backup database yourdb to disk=@fileName;
backup database yourdb to disk=@fileName2;

Both backup statement completes without any error, but c:\nvarcharmax.bak won't be written to the disk :)

(The online doc prohibit ntext/text but not nvarchar( max)...)

# re: SQL Server Backup Command with a Variable

Left by Chris Hoffman at 10/27/2008 1:28 PM
Gravatar
@stm:

To be clear, it does make a backup, but it uses a really random filename, and puts it under the server's default backup directory (e.g., C:\Program Files\Microsoft SQL Server 2005\MSSQL\Backup)

# re: SQL Server Backup Command with a Variable

Left by מתנפחים at 11/23/2008 7:18 PM
Gravatar
interesting post (-:

# re: SQL Server Backup Command with a Variable

Left by HaranathReddy at 9/1/2009 12:43 AM
Gravatar
good post.Really needfull.Thanks

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 5 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910