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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Copy database

Author  Topic 

duf
Starting Member

39 Posts

Posted - 2012-01-12 : 02:58:40
How can I copy database to other server?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-01-12 : 03:06:20
Easiest way is to take a backup and restore over there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

duf
Starting Member

39 Posts

Posted - 2012-01-12 : 03:22:16
Thanks, but how take a backup? Is there any query?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-01-12 : 03:33:45
Backup database db_name to disk='path'

Have a look at BACKUP DATABASE in SQL Server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-12 : 03:49:28
see this to understand types of backups available

http://www.karaszi.com/SQLServer/util_backup_script_like_MP.asp

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

duf
Starting Member

39 Posts

Posted - 2012-01-12 : 04:11:06
Can I do it in command lines? What do I enter the command?
I try to type:
execute dbo.databasebackup @databases = 'mysql', @directory = 'C:\Backup', @backuptype = 'full' \g

But i got error
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-12 : 04:35:06
quote:
Originally posted by duf

Can I do it in command lines? What do I enter the command?
I try to type:
execute dbo.databasebackup @databases = 'mysql', @directory = 'C:\Backup', @backuptype = 'full' \g
But i got error


the link i posted has code which does this in t-sql query

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

duf
Starting Member

39 Posts

Posted - 2012-01-12 : 04:43:29
Sorry, but I do not how to use this code. Do I have to type it in the command line or do I have to write it to a file and execute it?

IF OBJECT_ID('BackupDbWithTs') IS NOT NULL DROP PROC BackupDbWithTs
GO
CREATE PROC BackupDbWithTs
@db_name SYSNAME
,@folder NVARCHAR(255)
,@backup_type VARCHAR(13)
,@backup_extension VARCHAR(10)
,@with_checksum CHAR(1) = 'Y'
,@do_verification CHAR(1) = 'Y'
AS
DECLARE @sql NVARCHAR(4000)
DECLARE @filename NVARCHAR(255)
DECLARE @full_path_and_filename NVARCHAR(1000)
DECLARE @err_msg NVARCHAR(2000)
DECLARE @crlf VARCHAR(2)
SET @crlf = CHAR(13) + CHAR(10)

--Verify valid backup type
IF @backup_type NOT IN('DATABASE', 'LOG', 'DIFFERENTIAL')
BEGIN
SET @err_msg = 'Backup type ' + @backup_type + ' is not valid.
Allowed values are DATABASE, LOG and DIFFERENTIAL'
RAISERROR(@err_msg, 16, 1)
RETURN -101
END

--Make sure folder name ends with '\'
IF RIGHT(@folder, 1) <> '\'
SET @folder = @folder + '\'

--Make file extension starts with '.'
IF LEFT(@backup_extension, 1) <> '.'
SET @backup_extension = '.' + @backup_extension

--Construct filename
SET @filename = @db_name + '_backup_' + REPLACE(REPLACE(REPLACE(CONVERT(CHAR(16),
CURRENT_TIMESTAMP, 120), '-', ''), ' ', ''), ':', '')

--Construct full path and file name
SET @full_path_and_filename = @folder + @filename + @backup_extension

--Construct backup command
SET @sql = 'BACKUP ' + CASE @backup_type WHEN 'LOG' THEN 'LOG' ELSE 'DATABASE' END + ' '
+ QUOTENAME(@db_name) + @crlf
SET @sql = @sql + 'TO DISK = ' + QUOTENAME(@full_path_and_filename,'''') + @crlf
SET @sql = @sql + 'WITH' + @crlf
SET @sql = @sql + ' NOINIT,' + @crlf
SET @sql = @sql + ' NAME = ' + QUOTENAME(@filename,'''') + ',' + @crlf

IF @backup_type = 'DIFFERENTIAL'
SET @sql = @sql + ' DIFFERENTIAL,' + @crlf

IF @with_checksum <> 'N'
SET @sql = @sql + ' CHECKSUM,' + @crlf

--Add backup option below if you want to!!!

--Remove trailing comma and CRLF
SET @sql = LEFT(@sql, LEN(@sql) - 3)

--PRINT @sql
EXEC(@sql)

IF @do_verification = 'Y'
RESTORE VERIFYONLY FROM DISK = @full_path_and_filename
GO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-13 : 04:34:18
you've to pass the values to procedure and execute it

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -