SQL Server或Oracle中SQL语句实现一个旧表数据直接复制到新表以及保留表结构清空表数据

最近在倒腾数据库,包括Linux下的Oracle导入导出、Ubuntu 下Redis的部署配置,还有不同数据库的SQL数据表数据的来回复制,稍微整理下:

示例1:a 表为新表或者需要导入数据的表 b表为旧表
1.在SQL Server中解决方案
  如果原表存在复制语句如下:
  Insert into new.a select * from old.b

  如果原表不存在复制语句如下:
  Select * into new.a from old.b
2.在Oracle中解决方案

  如果原表存在复制语句如下:
  Insert into a select * from b

  如果原表不存在复制语句如下:

  Create table a as select * from b

示例2:保留表结构列清空表数据

1.清空单个表

truncate table 表名;

2.清空多张表,比如全库里的所有表:

declare @tablename varchar(30)

declare @sql varchar(2000)
declare cur_t cursor for
select name from dbo.sysobjects where xtype= 'U ' and status> =0 
open cur_t
fetch next from cur_t into @tablename
while @@FETCH_STATUS = 0
begin
   set @sql='truncate table '+ @tablename +''
   exec (@sql)
   fetch next from cur_t into @tablename
end
close cur_t
deallocate cur_t

 以上语句要谨慎使用,否则数据被清空恢复起来也很麻烦。

posted on 2018-04-25 09:53  sduSRZ  阅读(1538)  评论(0)    收藏  举报

导航