在工作中经常需要获得表的相关DDL,PL/SQL 和ORACLE 都有该功能,不过他们的功能太强大用起来不是很方便,自己就写了一个脚本。
希望该脚本可以对大家有所有帮助。所有的脚本我已经打包压缩上传到CSDN中
http://download.csdn.net/source/2895475
缺陷:字段最后都有","生成脚本后要手动剔除
把每个字段后都有“,”修改了 --2010.12.10
--01_create_temp_table.sql
建立临时表t_get_ddl_temp,存放想生成DDL的表名和属主
t_store_ddl_script 存放生成DDL的脚本
create table t_get_ddl_temp(
owner varchar2(30), --属主
table_name varchar2(30) --表名
);
insert into t_get_ddl_temp(owner ,table_name) values ('TEST','TEST1');
insert into t_get_ddl_temp(owner ,table_name) values ('TEST','TEST2');
commit;
/*
---存放生成的DDL,
---object_type
-----1 1-1 create table
1-2 table column
1-3 end
-----2 2-1 table comments
-----3 3-1 column comments
-----4 4-1 alter primary key
4-2 key column
4-3 end
-----5 5-1 create index
5-2 index column
5-3 end
*/
create table t_store_ddl_script(
owner varchar2(30), --属主
table_name varchar2(60), --表名
object_type varchar2(10), --DDL 类型
script_id int, --语句顺序
ddl_script varchar2(4000) --语句
);
--------------------------------------------------------------------------------------------------------------------------------------
----02_create_ddl.sql,生成DDL
declare
var_owner varchar2(30);
var_table_name varchar2(30);
var_index_name varchar2(30);
var_max_col_id int;
--get tables name which you want to get ddl
cursor cur_tab_name is
select
distinct
owner,
table_name
from t_get_ddl_temp
order by table_name asc;
---get table's indexes
cursor cur_index_name is
select
index_name
from all_indexes ai
where ai.table_name = var_table_name
and ai.owner = var_owner ;
begin
--open table's cursor
open cur_tab_name;
loop
fetch cur_tab_name into var_owner,var_table_name;
exit when cur_tab_name%notfound;
---create table
--------------------------
--create table start
--------------------------
----CREATE
insert into t_store_ddl_script (owner,table_name ,object_type,script_id,ddl_script)
select var_owner,
var_table_name,
'1-1',