SQLServer备份指定表
先定义表类型:
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) AS DECLARE @now VARCHAR(8) = CONVERT(VARCHAR(8),GETDATE(),112), @tb_name VARCHAR(100) DECLARE @bak_db_name VARCHAR(100) = ‘db_bak_‘ + @now EXEC(‘CREATE DATABASE ‘ + @bak_db_name) DECLARE c_tbs CURSOR LOCAL FOR SELECT * FROM @tbs OPEN c_tbs FETCH NEXT FROM c_tbs INTO @tb_name WHILE @@FETCH_STATUS = 0 BEGIN DECLARE @sql VARCHAR(1000) = ‘SELECT * INTO ‘ + @bak_db_name + ‘.dbo.‘ + REPLACE(@tb_name,‘.‘,‘_‘) + ‘ FROM ‘ + @tb_name EXEC(@sql) FETCH NEXT FROM c_tbs INTO @tb_name END CLOSE c_tbs DEALLOCATE c_tbs DECLARE @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_tables INSERT INTO @tmp_tb VALUES (‘table_a‘),(‘table_b‘) EXEC sp_bak_tables @tmp_tb ‘D:\BACKUP\‘
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。