Sql Server change data and log path of existing database - sql

I am having a SQL Server 2008 installation with almost 15 databases running on it. Now due to scarcity of space I would like to move the data path to another drive. What is the best practice for this. Please explain in details if including any SQL commands as I'm relatively new to SQL Server administration.
Note - I have already changed the path in SQL server properties from SQL Management Studio 2008, to the new path. But I would also like the existing databases to reside in the new path.

First, detach database:
USE master;
GO
-- Important! We need to drop the existing connections.
ALTER DATABASE DBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
EXEC sp_detach_db #dbname = N'DBName';
GO
Next step - copy files .mdf and .ldf of this database files to new location
And then attaching the database:
USE master;
EXEC sp_attach_db #dbname = N'dbName',
#filename1 = N'', --path do .mdf
#filename2 = N''; --path to .ldf
GO
If you don't want to attach and detach all databases one-by-one, you can generate SQL script to attach and detach all databases you need (execept system, of course), using curosr that searches in sys.databases dynamic management view. But don't forget to copy the database files.

One way is to detach and attach.
As for commands/steps, see the MSDN article "How to: Move a Database Using Detach and Attach (Transact-SQL)"

Related

Initiate and Get backup file from one SQL server to another via database link

From my development SQL Server, I want to initiate a backup on my production SQL Server that writes the backup to the development SQL Server disk via the database link.
For security reasons, I am unable to get drive permissions on the production SQL Server (which would allow me to initiate a restore on Dev from the Prod backup). This is a the only workaround I could come up with.
The overall goal is to get a full backup copy of Production every night and restore a copy to Development. This would give us a recent copy to work with in Dev.
I have tried the following code, but it doesn't recognize the database.
--RUN FROM DEV Database
BACKUP DATABASE [PROD_DATABASE_LINK].[blog]
TO DISK = N'\\Dev_Database_Drive\D$\Backup\blog\blog_FullBackup_Test'
WITH NOFORMAT, NOINIT,
NAME = N'blog-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
DECLARE #SQL NVARCHAR(100)
SET #SQL = 'BACKUP DATABASE [PROD_DATABASE_LINK].[dbo].[blog]TO DISK = N''\\\Database_A_Drive\D$\Backup\blog\blog_FullBackup_Test'''
EXECUTE [DEV_DATABASE_LINK].dbo.sp_executesql #SQL
Any help would be appreciated,
Eric
You can't backup a single table, and the UNC path should be \\server\share\folder\file. So the BACKUP command should look something like:
BACKUP DATABASE [blog]
TO DISK = N'\\DevServer\D$\Backup\VRVblog\VRVblog_FullBackup_Test.bak'
And to access the admin D$ share the SQL Server service account would need to be an administrator on the target server.
BACKUP can't reference a database on a linked server, but you can send that batch to the target server to execute like
declare #sql nvarchar(max) = N'
BACKUP DATABASE [blog]
TO DISK = N''\\DevServer\D$\Backup\VRVblog\VRVblog_FullBackup_Test.bak''
'
exec (#sql) at ProdServer
Sorry for the delay, but you were correct, you just can't get there from here because of permissions.
Since the question was posted, we had a contract/personnel change that has rectified the issue.

Copy SQL MDF file

I'm trying to get a copy of an MDF file but coming across the standard "file is in use" message which I believe is the SQL service holding a lock on it. Is it possible to make a copy of the mdf file without having to stop the service or in any way affect users/applications?
If not, is it possible to create a once-off full backup mdf to a different location than the existing one so that it wouldn't be locked by SQL service?
(Variations on this have been asked on this site already, but I don't think this is a duplicate. I am not trying to relocate the database, simply attempting to take a copy of the mdf - without interrupting Live operations - so I can place it on my dev machine at home and play around with the database)
this will let u make a backup :
BACKUP DATABASE MyDatabase TO DISK = 'W:\DBs\MyDatabase.bak' WITH INIT;
and if u want to backup the log file as well then just add this command before the previous one :
ALTER DATABASE MyDatabase SET RECOVERY FULL;
Then if you want to recover the database from the backup file try this command:
USE [master]
ALTER DATABASE MyDatabase
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
-- The previous command will used to close all connections to the database
-- until you recover it
Restore Database MyDatabase From Disk = 'W:\DBs\MyDatabase.bak' WITH REPLACE;
ALTER DATABASE MyDatabase SET MULTI_USER
-- This will reopen the database for users connections

How Can I Remove mdf and ldf File In SQL Server 2008?

I have this code:
IF EXISTS (SELECT * FROM sys.sysdatabases WHERE name='MyDatabase')
begin
DROP database MyDatabase
end
Go
CREATE DATABASE MyDatabase
But I see this error after executing this query:
Cannot create file 'C:\Program Files\Microsoft SQL Server\MSSQL10.FARASQL\MSSQL\DATA\MyDatabase.mdf'
Most likely your database was offline when you tried to DROP it. In such case SQL Server leaves db files on filesystem. It's intentional behavior.
According to DROP DATABASE (Transact-SQL)
Dropping a database deletes the database from an instance of SQL
Server and deletes the physical disk files used by the database. If
the database or any one of its files is offline when it is dropped,
the disk files are not deleted. These files can be deleted manually by
using Windows Explorer.
Drop the database is just to bring it offline. You need to manually delete the mdf and ldf file (see here on how to locate them); then refer to this image:
After that, you can recreate the database.

Programmatically Restore a Microsoft SQL Server Database

I wish to use ColdFusion to grab a database backup from a live server and restore it into a test environment. The grabbing bit is done, but I can't find a way of restoring the database programatically forcibly overwriting the database if its already there.
Any help would be appreicated, I figured there should be some SQL script or batch file that could do the job for me.
Obviously windows environment. SQL 2008, ColdFusion 9.
----Put database into single user mode (terminates open connections - else restore fails)
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE YourDB
FROM DISK = 'D:\temp\YourDB.bak'
WITH REPLACE
,MOVE 'YourDB_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.mdf'
,MOVE 'YourDB_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.ldf'
ALTER DATABASE YourDB SET MULTI_USER
GO

Drop Database connections in MS Sql Server 2000

I have a database that we are having problems with. Somehow the log has became 400 Gb and the database has been rendered useless. I want to drop all existing connections to the database and then detach the database.
Basically what I'm going to do is get rid of the giant log file and create a new one and reattach if it works. If not, we're going going to restore from backups.
If the log is useless, you can use these commands, but please document on them yourself before applying on a production server.
BACKUP LOG WITH NO_LOG for disgard the pages from log,
sp_helpdb for looking the name of the files of the db
DBCC SHRINKFILE('your log filename ', 0) -- for trunking the physical file to the size specified.
If you are sure there are no open transactions you can put the database in single user mode.
ALTER DATABASE [YourDB] SET SINGLE_USER WITH NO_WAIT
When you are done put it back in multi user mode
ALTER DATABASE [YourDB] SET MULTI_USER WITH NO_WAIT
Does
backup log yourdb with truncate_only then dbcc shrinkdatabase(yourdb) not shrink the logfile for you ?
Well basically no commands would execute agaisnt the database, at all. What we ended up doing was turning off the service and creating empty copies of the mdf and ldf files and replacing the ones being used by sql server. After that we restored the database from the last backup and voila, it's working again (mostly).
ALTER DATABASE [DB_NAME_HERE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE [DB_NAME_HERE] SET MULTI_USER