SQL script help: "Moving a database"

I'm trying to develop a script to change the drive letter where my SQL server looks for its databases.  I've cobbled this together, but I can't figure out my way around this error:

Msg 102, Level 15, State 1, Line 20
Incorrect syntax near '+'.
Msg 102, Level 15, State 1, Line 21
Incorrect syntax near '+'.

DECLARE @DBName varchar(50) = '';
DECLARE @NewMDFName varchar(50) = '';
DECLARE @NewLDFName varchar(50) = '';
DECLARE @OldMDFName varchar(50) = '';
DECLARE @OldLDFName varchar(50) = '';
DECLARE @LogicalName varchar(50) = '';

SET @DBName = 'TestLun1a';

SET @NewMDFName = @DBName + '.mdf';
SET @NewLDFName = @DBName + '.ldf';

USE Master;

SET @OldMDFName = (SELECT name FROM sys.master_files WHERE database_id = DB_ID(@DBName) AND type_desc = 'ROWS');
SET @OldLDFName = (SELECT name FROM sys.master_files WHERE database_id = DB_ID(@DBName) AND type_desc = 'LOG');

ALTER DATABASE [@DBName] SET OFFLINE WITH ROLLBACK IMMEDIATE;

ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldMDFName], FILENAME = 'F:\' + @NewMDFName] );
ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldLDFName], FILENAME =  'F:\' + @NewLDFName] );

ALTER DATABASE [@DBName] SET ONLINE;

Open in new window


The script is supposed to unmount the database, update SQL Server on its "new" location, then mount the database again.  Any help would be appreciated.
LVL 35
Paul MacDonaldDirector, Information SystemsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peter HutchisonSenior Network Systems SpecialistCommented:
There is a missing open square bracket for new filename.

ALTER DATABASE [@DBNAME] Modify File ( Name = [@OldMDFName], FILENAME = 'F:\' + [@NewMDFName] );
0
Paul MacDonaldDirector, Information SystemsAuthor Commented:
Thanks Peter, that was my mistake - the result of trying different solutions.  The code currently reads...
ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldMDFName], FILENAME = 'F:\' + @NewMDFName );
ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldLDFName], FILENAME = 'F:\' + @NewLDFName );

Open in new window

...but...
ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldMDFName], FILENAME = 'F:\' + [@NewMDFName] );
ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldLDFName], FILENAME = 'F:\' + [@NewLDFName] );

Open in new window

...errors out the same way.
0
Paul MacDonaldDirector, Information SystemsAuthor Commented:
Okay, solved this one on our own:
--ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldMDFName], FILENAME = [N'F:\'+ '@NewMDFName'] );
EXEC('ALTER DATABASE ' + @DBName + ' MODIFY FILE ( NAME = ' + @OldMDFName + ', FILENAME = [F:\' + @NewMDFName + '])')

----ALTER DATABASE [@DBName] MODIFY FILE ( NAME = [@OldLDFName], FILENAME = [N'F:\'+ '@NewLDFName'] );
EXEC('ALTER DATABASE ' + @DBName + ' MODIFY FILE ( NAME = ' + @OldLDFName + ', FILENAME = [F:\' + @NewLDFName + '])')

Open in new window


Thanks for trying, Peter.  Partial points to you for replying.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul MacDonaldDirector, Information SystemsAuthor Commented:
Ended up solving this one ourselves.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.