vpetkovic
12/24/2019 - 1:26 AM

Moving DB to Another location

-- Check the status of database as well old location
SELECT name, physical_name AS OldLocation, state_desc AS OnlineStatus
FROM sys.master_files  
WHERE database_id = DB_ID(N'foo')  
GO

-- Set DB offline
ALTER DATABASE foo SET OFFLINE WITH ROLLBACK IMMEDIATE;

-- CHange the location of the Database for both db and log
USE master;
ALTER DATABASE VPersonal   
    MODIFY FILE ( NAME = VPersonal,   
                  FILENAME = 'E:\foo.mdf');  
GO
 
 USE master; --do this all from the master
ALTER DATABASE VPersonal   
    MODIFY FILE ( NAME = VPersonal_log,   
                  FILENAME = 'E:\foo_log.ldf');  
GO

-- If you get access denied on the new location open SQL Server Configuration Manager (C:\Windows\SysWOW64\SQLServerManager[version].msc) find "SQL Server", right click, properties and change the logon user to one that can access new location
-- Restart SQL Server service and THEN set db online
-- ALTER DATABASE foo SET ONLINE;  

-- Check the status of database new location
SELECT name, physical_name AS NewLocation, state_desc AS OnlineStatus
FROM sys.master_files  
WHERE database_id = DB_ID(N'foo')  
GO