日期:2014-05-17  浏览次数:21013 次

怎样写语句导出某个table的 CREATE TABLE 的语句啊?
怎样写语句导出某个table的   CREATE   TABLE   的语句啊?

------解决方案--------------------
你用toad等工具可以很方便地查看。
自己写语句,麻烦:
shiqiguo 网友的一篇帖子:
create or replace procedure get_table_ddl
AUTHID CURRENT_USER
as
/**************************************************************
函数名称:get_table_ddl
函数功能:得到所有表建表的DDL语句
**************************************************************/
file_handle UTL_FILE.FILE_TYPE;
l_user varchar2(30); --表的schema的名称
l_tablename varchar2(50); --表的名称

--得到用户的所有的表
cursor table_cur is
select table_name
from user_tables;

--得到表的列的名称的游标
cursor snatch_columns is select
owner,
table_name,
column_name,
data_type,
data_length,
data_precision,
data_scale,
data_default,
nullable
from all_tab_columns
where table_name = l_tablename
and owner= l_user
order by column_id;

--得到表的索引的信息的游标
CURSOR constraint_index is select
a.table_owner,
a.index_name,
a.table_owner,
a.table_name,
a.uniqueness,
a.tablespace_name,
a.ini_trans,
a.max_trans,
a.initial_extent,
a.next_extent,
a.min_extents,
a.max_extents,
a.pct_increase,
a.pct_free
from user_indexes a ,user_constraints b
where a.table_owner =l_user and a.table_name =l_tablename and a.table_name=b.table_name
and a.index_name=b.constraint_name and b.constraint_type = 'P ';

--得到列信息
CURSOR col_cursor (i_own VARCHAR2, c_ind VARCHAR2, c_tab VARCHAR2) IS
select column_name
from dba_ind_columns
where index_owner = i_own
and index_name = c_ind
and table_name = c_tab
order by column_position;

--定义 列变量
v_table_owner varchar2(30);
v_table_name varchar2(30);
v_column_name varchar2(50);
v_data_type varchar2(30);
v_data_length varchar2(30);
v_data_precision varchar2(30);
v_data_scale varchar2(30);
v_data_default varchar2(30);
v_nullable varchar2(30);
v_tmp_str varchar2(2000);

--定义 索引变量
lv_index_owner dba_indexes.owner%TYPE;
lv_index_name dba_indexes.index_name%TYPE;
lv_table_owner dba_indexes.table_owner%TYPE;
lv_table_name dba_indexes.table_name%TYPE;
lv_uniqueness dba_indexes.uniqueness%TYPE;
lv_tablespace_name dba_indexes.tablespace_name%TYPE;
lv_ini_trans dba_indexes.ini_trans%TYPE;
lv_max_trans dba_indexes.max_trans%TYPE;
lv_initial_extent dba_indexes.initial_extent%TYPE;
lv_next_extent dba_indexes.next_extent%TYPE;
lv_min_extents dba_indexes.min_extents%TYPE;
lv_max_extents dba_indexes.max_extents%TYPE;
lv_pct_increase dba_indexes.pct_increase%TYPE;
lv_pct_free dba_indexes.pct_free%TYPE;
lv_column_name dba_ind_columns.column_name%TYPE;
lv_first_rec BOOLEAN;
lv_string VARCHAR2(2000);


begin
select user into l_user from dual;

--open the file
--下面目录必须在参数文件中设置过
file_handle := UTL_FILE.FOPEN( 'D:\oracle\file ', 'table.sql ', 'w ');
UTL_FILE.PUTF(file_handle, '************************* 得到 '||l_user|| '用户所有建表的DDL语句****************\n\n ');
UTL_FILE.PUTF(file_handle, '现在时间是: '||To_Char(SysDate, 'DD-MON-YYYY ')|| '\n\n ');

open table_cur;
loop
fetch table_cur into l_tablename;
exit when table_cur%NOTFOUND;

UTL_FILE.PUTF(file_handle, '----------------------------\n\n ');
UTL_FILE.PUTF(file_handle, 'CREATE TABLE '||user|| '. '||l_tablename|| '(\n ');