当前位置: 代码迷 >> Sql Server >> MSSQL怎么对 单 表 备 份
  详细解决方案

MSSQL怎么对 单 表 备 份

热度:496   发布时间:2016-04-27 11:23:24.0
MSSQL如何对 单 表 备 份
貌似没提供对单表备份功能, 我是这么做的:有一个数据库MM,里面有表A,B,C,D,F,F, 我想对其中A,B2张表备份,
  A表字段(A1,A2,A3) B表字段(B1,B2,B3)

  我新建了NN数据库, 在NN数据库下新建查询:create table A as (select A1,A2,A3 from MM.A) 出错了
(消息 156,级别 15,状态 1,第 1 行
关键字 'as' 附近有语法错误。
 )


 恳请各位能给小弟指点迷津``

------解决方案--------------------
SQL code
select A1,A2,A3  into A1  from MM.A
------解决方案--------------------
SQL code
select A1,A2,A3 into NN.dbo.A from MM.dbo.Aselect B1,B2,B3 into NN.dbo.B from MM.dbo.B
------解决方案--------------------
SQL code
/*--备份指定表到另一数据库    备份指定数据库中的指定表列表到一个新的数据库--邹建 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]GOCREATE PROCEDURE p_BackupDataBase@s_dbname sysname='',            --要备份的数据库名,如果不指定,则备份当前数据库@d_dbname sysname='',            --备份生成的数据库名,如果不指定,则为:@s_dbname+'_bak'@tblist varchar(8000)=''        --要备份的表名列表,如果不指定,则表示所有用户表ASdeclare @sql varchar(8000),@err_msg varchar(1000)--参数检测if isnull(@s_dbname,'')='' set @s_dbname=db_name()if isnull(@d_dbname,'')='' set @[email protected]_dbname+'_bak'if exists(select 1 from master..sysdatabases where [email protected]_dbname)begin    set @err_msg='备份的数据库 [[email protected]_dbname+'] 已经存在!'    goto lb_exitendif not exists(select 1 from master..sysdatabases where [email protected]_dbname)begin    set @err_msg='要备份的数据库 [[email protected]_dbname+'] 不存在!'    goto lb_exitend--创建备份的数据库set @sql='create database [[email protected]_dbname+']'exec(@sql)--备份表declare @tbname sysnameset @sql='declare tb cursor forselect name from [[email protected]_dbname+']..sysobjects where status>0 and xtype=''U'''+case isnull(@tblist,'') when '' then ''     else ' and name in('''+replace(@tblist,',',''',''')+''')' endexec(@sql)open tbfetch next from tb into @tbnamewhile @@fetch_status=0begin    set @sql='select * into [[email protected]_dbname+']..[[email protected]        +'] from [[email protected]_dbname+']..[[email protected]+']'    exec(@sql)    fetch next from tb into @tbnameendclose tbdeallocate tblb_exit:    if @err_msg<>'' raiserror(@err_msg,1,16)go
------解决方案--------------------
探讨
引用:
没这样的语法,备份数据??可以利用 触发器


我的意思是想达到把MM中的A,B表中的数据全拷贝到NN数据库中作个备份
  相关解决方案