生成创建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
相关推荐
lhldsg27 分钟前
全民健身解决方案软件开发实战:从架构设计到部署指南
java·前端·数据库·小程序
我是大猴子29 分钟前
线上耗时问题排查
数据库
小码过河.1 小时前
oceanbase中zone和observer、分区表之间关系
前端·数据库·oceanbase
其实防守也摸鱼1 小时前
OpenClaw Windows 踩坑实战指南
运维·服务器·数据库·windows·github·copilot
SelectDB1 小时前
Apache Doris+ Paimon 2.0:构建 Agentic AI 数据闭环
大数据·数据库·数据分析
麦聪聊数据2 小时前
全链路数据安全防护(中):入口、权限、审计,筑牢安全管控铁三角
数据库
山岚的运维笔记2 小时前
mysql 专业笔记 -- 第 17 章:连接:连接三个具有相同名称 ID 的表
运维·数据库·笔记·后端·学习·mysql·dba
YangYang9YangYan2 小时前
校招视角|财务数字化岗位 SQL、工具、项目完整备考指南
大数据·数据库·sql
bksczm2 小时前
从 “什么是 MySQL“ 到库与表的完整操作(MySQL基础篇)
linux·数据库·mysql
风123456789~2 小时前
【Oracle专栏】解决clob+dblink 插入报错 ORA-22992
数据库·oracle