How to backup on MSSQL by table level ?

MSSQL is good database.   Unlike as Oracle,  it seems that can not backup sqlserver databasee tables one by one.

 

However there is always way.

 

Thanks to

捕获.JPG.jpg

I  did modify and then , it can auto baclkup table one by one no matter how many table is.

捕获2.JPG.jpg

 

Here is script:

 

-- SQL Table Backup

-- Developed by DBATAG, www.DBATAG.com

DECLARE @table VARCHAR(128),

@file VARCHAR(255),

@cmd VARCHAR(512)

 

declare @m int

declare @n int

--declare @tmp nvarchar(MAX)

 

declare  @tableList TABLE (id int IDENTITY(1,1) NOT NULL,contents VARCHAR(128) NOT NULL)

DECLARE @SQL2 VARCHAR(MAX)

 

SET NOCOUNT ON

set @SQL2='select DB_NAME()+''.'' +schema_name() + ''.''+ name from sys.tables where type=''U'''

 

INSERT INTO @tableList (contents) exec (@SQL2)

 

set @m=(select count(*) from @tableList)

set @n=1

 

while @n<=@m

begin

set @table=(select contents from @tableList where id=@n)

 

--SET @table = 'AdventureWorks.Person.Contact' --  Table Name which you want    to backup

--SET @table = 'MES.dbo.Sys_Group'

--SET @table = 'select name from sys.tables where type='U' order by 1'

SET @file = 'C:\backup\' + @table + '_' + CONVERT(CHAR(8), GETDATE(), 112) --  Replace C:\MSSQL\Backup\ to destination dir where you want to place table data backup

+ '.bcp'

SET @cmd = 'bcp ' + @table + ' out ' + @file + ' -n -T '

EXEC master..xp_cmdshell @cmd

set @n=@n+1

end

 

 

Bingo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值