取得表中数据的insert语句
Build Insert Statements for the Existing Data in Tables
下面这个脚本实现了取得一个非空表中的所有insert语句
This script builds insert statements for the existing data in the tables. One can run the generated script to repopulate the data.
-- By: Ashish Kumar
-- Date Created: 10/01/2001
-- EMail: kumara@jagat.com
-- Code Version: 1.0.1 -- Objective:
-- You can use the following code to extract the existing data from tables in the form
-- of insert statements. The generated script can be run at a later time to re-create your data.
-- This code is no match for EXPORT and IMPORT utilities.
-- Use it for *quick and dirty* situations.
-- The code handles only date, char, varchar2, and numeric data types. -- Change History: -- The example used in the code uses scott schema. -- AUTHOR MAKES NO WARRANTIES FOR THIS CODE. -- Step 1: 创建下面的函数
create or replace Function ExtractData(v_table_name varchar2) return varchar2 As
b_found boolean:=false;
v_tempa varchar2(8000);
v_tempb varchar2(8000);
v_tempc varchar2(255);
begin
for tab_rec in (select table_name from user_tables where table_name=upper(v_table_name))
loop
b_found:=true;
v_tempa:='select ''insert into '||tab_rec.table_name||' (';
for col_rec in (select * from user_tab_columns
where
table_name=tab_rec.table_name
order by
column_id)
loop
if col_rec.column_id=1 then
v_tempa:=v_tempa||'''||chr(10)||''';
else
v_tempa:=v_tempa||',''||chr(10)||''';
v_tempb:=v_tempb||',''||chr(10)||''';
end if;
v_tempa:=v_tempa||col_rec.column_name;
if instr(col_rec.data_type,'CHAR') > 0 then
v_tempc:='''''''''||'||col_rec.column_name||'||''''''''';
elsif instr(col_rec.data_type,'DATE') > 0 then
v_tempc:='''to_date(''''''||to_char('||col_rec.column_name||',''mm/dd/yyyy hh24:mi'')||'''''',''''mm/dd/yyyy hh24:mi'''')''';
else
v_tempc:=col_rec.column_name;
end if;
v_tempb:=v_tempb||'''||decode('||col_rec.column_name||',Null,''Null'','||v_tempc||')||''';
end loop;
v_tempa:=v_tempa||') values ('||v_tempb||');'' from '||tab_rec.table_name||';';
end loop;
if Not b_found then
v_tempa:='-- Table '||v_table_name||' not found';
else
v_tempa:=v_tempa||chr(10)||'select ''-- commit;'' from dual;';
end if;
return v_tempa;
end;
/
show errors -- STEP 2: Run the following code to extract the data. 新建一个文本,文本中包括下面的内容
set head off
set pages 0
set trims on
set lines 2000
set feed off
set echo off
var retline varchar2(4000)
spool /home/oracle/hxy/t1.sql
select 'set echo off' from dual;
select 'spool /home/oracle/hxy/recreatedata.sql' from dual;
select 'select ''-- This data was extracted on ''||to_char(sysdate,''mm/dd/yyyy hh24:mi'') from dual;' from dual; -- Repeat the following two lines as many times as tables you want to extract
exec :retline:=ExtractData('dept');
print :retline; exec :retline:=ExtractData('emp');
print :retline; select 'spool off' from dual;
spool off
@/home/oracle/hxy/t1
-- STEP3: Run the spooled output c:\recreatedata.sql to recreate data. 查看recreatedata.sql
例如:
db111@dbrac1 /home/oracle/hxy$ cat recreatedata.sql
-- This data was extracted on 03/11/2014 21:42
insert into DEPT (
DEPTNO,
DNAME,
LOC) values (10,
'ACCOUNTING',
'NEW YORK'); insert into DEPT (
DEPTNO,
DNAME,
LOC) values (20,
'RESEARCH',
'DALLAS'); insert into DEPT (
DEPTNO,
DNAME,
LOC) values (30,
'SALES',
'CHICAGO'); insert into DEPT (
DEPTNO,
DNAME,
LOC) values (40,
'OPERATIONS',
'BOSTON'); -- commit;
insert into EMP (
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
DEPTNO) values (7369,
'SMITH',
'CLERK',
7902,
to_date('12/17/1980 00:00','mm/dd/yyyy hh24:mi'),
800,
Null,
20); insert into EMP (
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
DEPTNO) values (7499,
'ALLEN',
'SALESMAN',
7698,
to_date('02/20/1981 00:00','mm/dd/yyyy hh24:mi'),
1600,
300,
30); insert into EMP (
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
DEPTNO) values (7521,
'WARD',
'SALESMAN',
7698,
to_date('02/22/1981 00:00','mm/dd/yyyy hh24:mi'),
1250,
500,
30); insert into EMP (
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
COMM,
DEPTNO) values (7566,
'JONES',
'MANAGER',
7839,
to_date('04/02/1981 00:00','mm/dd/yyyy hh24:mi'),
2975,
Null,
20);
取得表中数据的insert语句的更多相关文章
- 将表中数据生成SQL语句
在开发过程中,经常需要我们对表中的数据进行转移,如果在同台机器,可以使用SQL自带的导入数据,但是如果想让所有的数据生成可执行的SQL语句,它的移植性最强了.首先要设计一个存储过程.具体如下: CRE ...
- 表数据转换为insert语句
/* 对象:导出物理表数据为Insert语句 描述:可以传递条件精确导出sql 加条件的前提是只知道相应的字段名及类型 */ from sysobjects where name ='proc_ins ...
- sqlserver 表中记录生成insert,可以加条件,可以生成建表语句
sqlserver 表中记录生成insert,可以加条件,可以生成建表语句 create PROCEDURE [sp_getinsert] ( ) , --如果非默认架构,可以加上架构名 例如:sch ...
- mysql--对行(表中数据)的增删改查
一.插入数据(增加)insert 1.插入数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); #指定字段来插入数据,插 ...
- SQLite中特殊的INSERT语句
SQLite中特殊的INSERT语句 在SQLite中,INSERT是基本语句,用来向表中插入数据.但是当表中存在字段存在唯一.非空.检查.主键等约束时,插入的数据很容易和约束冲突,造成插入操作失 ...
- mysql 表中数据不存在则插入,否则更新数据
在很多时候我们会操作数据库表,但是在向表中插入数据时,会遇到表中已经存在该id的数据或者没有该id的数据的情况,没有该id的数据的情况时直接插入就OK,遇到已经存在该id的数据的情况则更新该id的数据 ...
- [SQL]开启事物,当两条插入语句有出现错误的时候,没有错误的就插入到表中,错误的语句不影响到正确的插入语句
begin transaction mustt insert into student values(,'kkk','j大洒扫','j','djhdjh') insert into student v ...
- MySQL查询数据表中数据记录(包括多表查询)
MySQL查询数据表中数据记录(包括多表查询) 在MySQL中创建数据库的目的是为了使用其中的数据. 使用select查询语句可以从数据库中把数据查询出来. select语句的语法格式如下: sele ...
- sql server 数据库导出表里所有数据成insert 语句
有时候,我们想把数据库的某张表里的所有数据导入到另外一个数据库或另外一台计算机上的数据库,对于sql server有这样的一种方法 下面我以sql server 2008 R2,数据库是Northwi ...
随机推荐
- Python自动化之YAML解析
准备工作 pip install PyYAML import yaml yaml语法规则 想要表示列表项,使用一个短横杠加一个空格.多个项使用同样的缩进级别作为同一列表的一部分 my_dictiona ...
- mapReduce编程之google pageRank
1 pagerank算法介绍 1.1 pagerank的假设 数量假设:每个网页都会给它的链接网页投票,假设这个网页有n个链接,则该网页给每个链接平分投1/n票. 质量假设:一个网页的pagerank ...
- 基于Selenium的模拟浏览器采集
Selenium 也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7.8.9).Mozilla Firefox.Mozil ...
- curl模拟自动登陆&采集网页数据
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- 1.1---判断字符串是否所有字符都不相同(CC150)
import java.util.*; public class Different { public boolean checkDifferent(String str) { // write co ...
- spring的对象属性相同(类型,名字)拷贝
A类: package test; /** * Created by gmq on 2015/12/4. */ public class A { private String aa; private ...
- mysql创建用户
mysql创建用户 创建用于localhost连接的用户并指定密码 mysql> create user 'pcom'@'localhost' identified by 'aaa7B2249' ...
- centos7删除已经安装的docker
centos下可以使用yum来删除docker. 列出docker包的具体的名字. $ yum list installed | grep docker docker-engine.x86_64 -0 ...
- 最简单粗暴的http文件列表
:]: port = ])else: port = 8000server_address = ('127.0.0.1', port)Handler.protocol_version = Protoco ...
- Tomcat中文乱码问题的原理和解决方法
1) 更改 D:\Tomcat\conf\server.xml,指定浏览器的编码格式为“简体中文”: 方法是找到 server.xml 中的 <Connector port="8080 ...