/*--备份指定表到另一数据库
备份指定数据库中的指定表列表到一个新的数据库
--邹建 2003.12--*/
/*--调用示例
--备份数据当前数据库的所有内容
exec p_backupdatabase
--备份当前数据库的指定表
exec p_backupdatabase @tblist='tb,tb1,tb2'
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_BackupDataBase]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_BackupDataBase]
GO
CREATE PROCEDURE p_BackupDataBase
@s_dbname sysname='', --要备份的数据库名,如果不指定,则备份当前数据库
@d_dbname sysname='', --备份生成的数据库名,如果不指定,则为:@s_dbname+'_bak'
@tblist varchar(8000)='' --要备份的表名列表,如果不指定,则表示所有用户表
AS
declare @sql varchar(8000),@err_msg varchar(1000)
--参数检测
if isnull(@s_dbname,'')='' set @s_dbname=db_name()
if isnull(@d_dbname,'')='' set @d_dbname=@s_dbname+'_bak'
if exists(select 1 from master..sysdatabases where name=@d_dbname)
begin
set @err_msg='备份的数据库 ['+@d_dbname+'] 已经存在!'
goto lb_exit
end
if not exists(select 1 from master..sysdatabases where name=@s_dbname)
begin
set @err_msg='要备份的数据库 ['+@d_dbname+'] 不存在!'
goto lb_exit
end
--创建备份的数据库
set @sql='create database ['+@d_dbname+']'
exec(@sql)
--备份表
declare @tbname sysname
set @sql='declare tb cursor for
select name from ['+@s_dbname+']..sysobjects
where status>0 and xtype=''U'''
+case isnull(@tblist,'') when '' then ''
else ' and name in('''+replace(@tblist,',',''',''')+''')' end
exec(@sql)
open tb
fetch next from tb into @tbname
while @@fetch_status=0
begin
set @sql='select * into ['+@d_dbname+']..['+@tbname
+'] from ['+@s_dbname+']..['+@tbname+']'
exec(@sql)
fetch next from tb into @tbname
end
close tb
deallocate tb
lb_exit:
if @err_msg<>'' raiserror(@err_msg,1,16)
go
备份指定数据库中的指定表列表到一个新的数据库
--邹建 2003.12--*/
/*--调用示例
--备份数据当前数据库的所有内容
exec p_backupdatabase
--备份当前数据库的指定表
exec p_backupdatabase @tblist='tb,tb1,tb2'
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_BackupDataBase]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_BackupDataBase]
GO
CREATE PROCEDURE p_BackupDataBase
@s_dbname sysname='', --要备份的数据库名,如果不指定,则备份当前数据库
@d_dbname sysname='', --备份生成的数据库名,如果不指定,则为:@s_dbname+'_bak'
@tblist varchar(8000)='' --要备份的表名列表,如果不指定,则表示所有用户表
AS
declare @sql varchar(8000),@err_msg varchar(1000)
--参数检测
if isnull(@s_dbname,'')='' set @s_dbname=db_name()
if isnull(@d_dbname,'')='' set @d_dbname=@s_dbname+'_bak'
if exists(select 1 from master..sysdatabases where name=@d_dbname)
begin
set @err_msg='备份的数据库 ['+@d_dbname+'] 已经存在!'
goto lb_exit
end
if not exists(select 1 from master..sysdatabases where name=@s_dbname)
begin
set @err_msg='要备份的数据库 ['+@d_dbname+'] 不存在!'
goto lb_exit
end
--创建备份的数据库
set @sql='create database ['+@d_dbname+']'
exec(@sql)
--备份表
declare @tbname sysname
set @sql='declare tb cursor for
select name from ['+@s_dbname+']..sysobjects
where status>0 and xtype=''U'''
+case isnull(@tblist,'') when '' then ''
else ' and name in('''+replace(@tblist,',',''',''')+''')' end
exec(@sql)
open tb
fetch next from tb into @tbname
while @@fetch_status=0
begin
set @sql='select * into ['+@d_dbname+']..['+@tbname
+'] from ['+@s_dbname+']..['+@tbname+']'
exec(@sql)
fetch next from tb into @tbname
end
close tb
deallocate tb
lb_exit:
if @err_msg<>'' raiserror(@err_msg,1,16)
go