根据数据库中已存在的表,反向生成建表脚本
--以表AA11为例
select 'create table ' || a.table_name || '(' || chr(10) ||
(select TO_CHAR(wm_concat(b.column_name || ' ' || b.DATA_TYPE || '(' ||b.DATA_LENGTH || ') ' ||
decode(b.NULLABLE, 'N', ' not null ', '') ||
chr(10)))
from (select * from user_tab_columns order by COLUMN_ID) b
where b.table_name = a.table_name) || ');' || chr(10) ||
'comment on table ' || a.table_name || ' is ''' || comments||''';'||chr(10)||
(select replace(TO_CHAR(wm_concat('comment on column '||e.table_name||'.'||e.column_name||' is '''||e.comments||''';')),',',chr(10)) from (select c.TABLE_NAME,c.COLUMN_NAME,d.comments from user_tab_columns c,user_col_comments d where c.table_name=d.table_name and c.column_name=d.column_name order by c.COLUMN_ID) e where e.table_name=a.table_name)
from user_tab_comments a
WHERE A.table_name = 'AA11';
--结果如下