SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Moving Databases with the ALTER Command


Moving Databases with the ALTER Command

Author
Message
imSQrLy
imSQrLy
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 473
Comments posted to this topic are about the item Moving Databases with the ALTER Command

Jimmy

"I'm still learning the things i thought i knew!"
Private Gripweed
Private Gripweed
SSC-Enthusiastic
SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)

Group: General Forum Members
Points: 188 Visits: 130
The script works well with one exception. If your logical name for the database files contains spaces, it will cause issues.

If you alter the stored procedure and enclose the logical name for the database file in double quotes, this fixes the issue. See below:

set @sql = 'ALTER DATABASE ' + @DbName + ' MODIFY FILE (NAME = "' + @LogicalName + '", FILENAME = "' + @NewPath + '")'
Johan van Tonder
Johan van Tonder
SSC Veteran
SSC Veteran (225 reputation)SSC Veteran (225 reputation)SSC Veteran (225 reputation)SSC Veteran (225 reputation)SSC Veteran (225 reputation)SSC Veteran (225 reputation)SSC Veteran (225 reputation)SSC Veteran (225 reputation)

Group: General Forum Members
Points: 225 Visits: 347
Very good method.

I'm just not sure if I would want to be any one of the users connected to the DB while the script is run, since I will get killed ;-)

Keep in mind this script will kill any connected users by setting the db to single user with SET SINGLE_USER WITH ROLLBACK IMMEDIATE


Yes, I know what's being meant, but that gave me laugh though :-)
Private Gripweed
Private Gripweed
SSC-Enthusiastic
SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)

Group: General Forum Members
Points: 188 Visits: 130
LOL that is funny, I did not think about it that way.
rondad
rondad
SSC-Enthusiastic
SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)

Group: General Forum Members
Points: 123 Visits: 27
Has anyone tested the script with using a unc path for the new target location rather than a "hard-coded" drive letter?

Ron
Private Gripweed
Private Gripweed
SSC-Enthusiastic
SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)

Group: General Forum Members
Points: 188 Visits: 130
Easily tested. But I have not
K. Brian Kelley
K. Brian Kelley
Keeper of the Duck
Keeper of the Duck (39K reputation)

Group: Moderators
Points: 39462 Visits: 1917
EDIT: Brian needs to read more closely. Smile

K. Brian Kelley
@‌kbriankelley
imSQrLy
imSQrLy
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 473
Johan van Tonder (8/26/2009)
Very good method.

I'm just not sure if I would want to be any one of the users connected to the DB while the script is run, since I will get killed ;-)

Keep in mind this script will kill any connected users by setting the db to single user with SET SINGLE_USER WITH ROLLBACK IMMEDIATE


Yes, I know what's being meant, but that gave me laugh though :-)


Bang your dead!
I haven't laughed like this all week.

Jimmy

"I'm still learning the things i thought i knew!"
imSQrLy
imSQrLy
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 473
Private Gripweed (8/26/2009)
The script works well with one exception. If your logical name for the database files contains spaces, it will cause issues.

If you alter the stored procedure and enclose the logical name for the database file in double quotes, this fixes the issue. See below:

set @sql = 'ALTER DATABASE ' + @DbName + ' MODIFY FILE (NAME = "' + @LogicalName + '", FILENAME = "' + @NewPath + '")'


Thanks for pointing that out. I will update my script.

Jimmy

"I'm still learning the things i thought i knew!"
imSQrLy
imSQrLy
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 473
rondad (8/26/2009)
Has anyone tested the script with using a unc path for the new target location rather than a "hard-coded" drive letter?

Ron


I have not tested it like that.

Jimmy

"I'm still learning the things i thought i knew!"
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search