Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jayram
Starting Member
47 Posts |
Posted - 2012-06-21 : 13:57:22
|
Hello,I want to copy a mdf file to a new locationi believe these are the steps1. detach or take offline2. copy the mdf file from source location to target location3. bring onlinei do this through enterprise manager but can it all be automated in a scripte.g:for step 1, i would-- Take the Database OfflineALTER DATABASE [myDB] SET OFFLINE WITHROLLBACK IMMEDIATEGObut i do not know how to copy the .mdf file? is there some copy or xcopy command that can be used?thanksJay |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-06-21 : 14:09:19
|
you can use copy command in xp_cmdshell.Might want to look at powershellYou should also take a backup first (you could also do a drop and restore with move).==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
jayram
Starting Member
47 Posts |
Posted - 2012-06-21 : 14:15:24
|
Thanks Nigel.would the below work!!--Take offlineALTER DATABASE testDB SET OFFLINE WITHROLLBACK IMMEDIATEGO--Dynamically copydeclare @cmd varchar(4000)set @cmd = 'xcopy F:\MSSQL\Data\testDB.mdf' + cast(getdate() as varchar(10) + '>> \\RemoteServer\Share\Folder\'exec (@cmd)-- bring the Database OnlineALTER DATABASE zBRSDATA_2012_6_14 SET ONLINEGO |
|
|
|
|
|