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.清空多张表,比如全库里的所有表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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   sduSRZ  阅读(1477)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示