Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use a variable in a script to 'use' database

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello,

I am trying to move hundreds of databases from one server to another and trying to build a script to help along. Both servers are SQL 2000

I need to do one at a time over the next week after each database is ready. I am trying this:

Code:
declare @dbname varchar(100)
set @dbname = 'Batch1'

create database @dbname

restore database @dbname
from disk = '\\server\share\@dbname'
with init

use @dbname
sp_changedbowner USC

The variable will work on everything except for the 'create' and 'use' databases.

I get the error: Incorrect syntax near @dbname

I thought it might have to do something with my Varchar variable type and the use of single quotes, but I have tried without quotes and it hasn't helped.

Thanks!

 
You can do this with dynamic sql. It would look something like this:

Code:
declare @dbname varchar(100)
set @dbname = 'Batch1'

Declare @SQL VarChar(8000)

Set @SQL = '
create database @dbname

restore database @dbname
from disk = ''\\server\share\@dbname''
with init

use @dbname
sp_changedbowner USC
'

Set @SQL = Replace(@SQL, '@dbname', @dbname)

Print @SQL
--Exec (@SQL)

Once you get the code to print properly to the messages window, you can comment the print line and uncomment the exec line.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Wow that's a great idea! Seems to work great. My only problem now is that I can't:

Code:
create database mydb

restore database mydb

I get a message that the database doesn't exist. I think I would have to put a GO in between the two. But, when I put GO in between the two I get a bunch of errors (probably from losing my variable). Any idea around that?
 
Try doing it in steps, like this:

Code:
declare @dbname varchar(100)
set @dbname = 'Batch1'

Declare @SQL VarChar(8000)

-- Step 1, create the database
Set @SQL = 'create database @dbname'
Set @SQL = Replace(@SQL, '@dbname', @dbname)
Print @SQL
--Exec (@SQL)

-- Step 2, restore it.
Set @SQL = 'restore database @dbname
from disk = ''\\server\share\@dbname''
with init'
Set @SQL = Replace(@SQL, '@dbname', @dbname)
Print @SQL
--Exec (@SQL)

-- Step 3, change the owner
Set @SQL = 'use @dbname
sp_changedbowner USC'

Set @SQL = Replace(@SQL, '@dbname', @dbname)
Print @SQL
--Exec (@SQL)

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Brilliant! Thanks everyone, doing in steps worked great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top