生成创建table 的sql sed ‘s/REM //‘

复制代码
Purpose:
========

The purpose of this article is to explain how you can transfer table
definitions from one database to another database by generating script 
files.

 
How to Generate 'CREATE TABLE' Scripts from Existing Tables:
============================================================

If basic tables, that is, tables with no partitions, constraints, indexes,
etc. are sufficient for your purposes, then you can use the script listed 
at the end of this article.

If you need detailed CREATE TABLE SQL statements, then you can follow 
this example:

   exp user_name/password file=table_name.dmp rows=n tables=\('table_name'\);
     (Use brackets to escape the backslash because of shell interpretation.)

   imp user_name/password file= table_name.dmp indexfile=table_name.log

   on UNIX:
     sed 's/REM //' table_name.log >table_name.sql
   on another platforms:
     open file table_name.log in any text editor and replace string 'REM ' by '' 
     (empty string).
	 save as table_name.sql
	 
Check the file table_name.sql for the script that you need.


============ Script for basic CREATE TABLE SQL statement ================
spool table.lst
set serveroutput on size 1000000
declare
starting boolean :=true;
r_owner varchar2(30) := '&1';
r_table_name varchar2(30) := '&2';
begin 
dbms_output.put_line('create table '||r_owner||'.'||r_table_name||'(');
for r in (select column_name, data_type, data_length, data_precision, data_scale, data_default, nullable 
            from all_tab_columns
           where table_name = upper(r_table_name)
		     and owner=upper(r_owner)
		   order by column_id)
loop
  if starting then
    starting:=false;
  else
    dbms_output.put_line(',');	
  end if;

  if r.data_type='NUMBER' then
    if r.data_scale is null then
      dbms_output.put(r.column_name||' NUMBER('||r.data_precision||')');  	
    else
      dbms_output.put(r.column_name||' NUMBER('||r.data_precision||','||r.data_scale||')');
    end if;
  else if r.data_type = 'DATE' then
    dbms_output.put_line(r.column_name||' DATE');
  else if instr(r.data_type, 'CHAR') >0 then
    dbms_output.put(r.column_name||' '||r.data_type||'('||r.data_length||')');
  else
    dbms_output.put(r.column_name||' '||r.data_type);
  end if;
  end if;
  end if;
  if r.data_default is not null then
    dbms_output.put(' DEFAULT '||r.data_default);
  end if;
  if r.nullable = 'N' then
    dbms_output.put(' NOT NULL ');
  end if;
end loop;
dbms_output.put_line(' ); ');
end;
/

spool off
相关推荐
Dreams_l2 分钟前
MySQL初阶:sql事务和索引
数据库·sql·mysql
拾贰_C18 分钟前
【SpringBoot】关于MP使用中配置了数据库表前缀的问题
数据库·spring boot·oracle
狐凄32 分钟前
Python实例题:Python百行制作登陆系统
数据库
vvilkim40 分钟前
Redis 发布订阅模式深度解析:原理、应用与实践
数据库·redis·缓存
开***能1 小时前
降本增效双突破:Profinet转Modbus TCP助力包布机产能与稳定性双提升
数据库·网络协议·tcp/ip
广州智造6 小时前
OptiStruct实例:3D实体转子分析
数据库·人工智能·算法·机器学习·数学建模·3d·性能优化
技术宝哥9 小时前
Redis(2):Redis + Lua为什么可以实现原子性
数据库·redis·lua
学地理的小胖砸10 小时前
【Python 操作 MySQL 数据库】
数据库·python·mysql
dddaidai12311 小时前
Redis解析
数据库·redis·缓存
数据库幼崽11 小时前
MySQL 8.0 OCP 1Z0-908 121-130题
数据库·mysql·ocp