生成创建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
相关推荐
Java探秘者3 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
2301_786964363 小时前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
阿维的博客日记4 小时前
图文并茂解释水平分表,垂直分表,水平分库,垂直分库
数据库·分库分表
wrx繁星点点5 小时前
事务的四大特性(ACID)
java·开发语言·数据库
小小娥子6 小时前
Redis的基础认识与在ubuntu上的安装教程
java·数据库·redis·缓存
DieSnowK6 小时前
[Redis][集群][下]详细讲解
数据库·redis·分布式·缓存·集群·高可用·新手向
-XWB-6 小时前
【MySQL】数据目录迁移
数据库·mysql
老华带你飞6 小时前
公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·课程设计
我明天再来学Web渗透7 小时前
【hot100-java】【二叉树的层序遍历】
java·开发语言·数据库·sql·算法·排序算法
Data 3177 小时前
Hive数仓操作(十一)
大数据·数据库·数据仓库·hive·hadoop