SQLServer備份指定表

來源:互聯網
上載者:User

標籤:style   blog   http   ar   os   sp   for   資料   on   

先定義表類型:

CREATE TYPE t_bak_tables AS TABLE(table_name VARCHAR(200))

然後再定義備份過程:

CREATE PROC sp_bak_tables @tbs t_bak_tables READONLY, @bak_path VARCHAR(200) ASDECLARE @now VARCHAR(8) = CONVERT(VARCHAR(8),GETDATE(),112), @tb_name VARCHAR(100)DECLARE @bak_db_name VARCHAR(100) = ‘db_bak_‘ + @nowEXEC(‘CREATE DATABASE ‘ + @bak_db_name)DECLARE c_tbs CURSOR LOCAL FOR SELECT * FROM @tbsOPEN c_tbsFETCH NEXT FROM c_tbs INTO @tb_nameWHILE @@FETCH_STATUS = 0BEGINDECLARE @sql VARCHAR(1000) = ‘SELECT * INTO ‘ + @bak_db_name + ‘.dbo.‘ + REPLACE(@tb_name,‘.‘,‘_‘) + ‘ FROM ‘ + @tb_name  EXEC(@sql)  FETCH NEXT FROM c_tbs INTO @tb_nameENDCLOSE c_tbs  DEALLOCATE c_tbsDECLARE @bak_file VARCHAR(100) = @bak_path + ‘db_bak_‘ + @now + ‘_‘ + CAST(DATEDIFF(s,{d‘1970-01-01‘},GETDATE()) AS VARCHAR(100)) + ‘.bak‘EXEC(‘BACKUP DATABASE ‘ + @bak_db_name + ‘ TO DISK = ‘‘‘ + @bak_file + ‘‘‘‘)EXEC(‘DROP DATABASE ‘ + @bak_db_name)

最後調用方法如下:

DECLARE @tmp_tb bak_tablesINSERT INTO @tmp_tb VALUES (‘table_a‘),(‘table_b‘)EXEC sp_bak_tables @tmp_tb ‘D:\BACKUP\‘

參考: SQL Server 備份、還原、附加、分離資料庫 批量語句

SQLServer備份指定表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.