生成创建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
相关推荐
敲代码的嘎仔几秒前
力扣高频SQL基础50题详解
开发语言·数据库·笔记·sql·算法·leetcode·后端开发
jran-9 分钟前
MySQL多表操作 查询&子查询&外键约束
数据库·mysql
橙子圆12310 分钟前
Redis知识6之事务
数据库·redis·缓存
不会摸鱼的小鱼14 分钟前
WSL 安装 Ubuntu 22.04 到指定磁盘
数据库·postgresql·php
m0_7020365321 分钟前
mysql如何导出特定条件的查询数据_使用mysqldump加where参数
jvm·数据库·python
正在走向自律22 分钟前
标量子查询消除:数据库优化器的一场“等价变戏法”
数据库·sql 优化·金仓数据库·数据库性能调优·标量子查询·数据库优化器
逻极23 分钟前
SQLite 从入门到精通:深入理解嵌入式数据库的艺术与科学
数据库·sqlite·记忆·sqlite从入门到精通
未来之窗软件服务35 分钟前
数据库优化(九)随机抽选系统数据表 ——东方仙盟
大数据·数据库·数据库优化·仙盟创梦ide·东方仙盟
爱喝水的鱼丶1 小时前
SAP-ABAP:新手入门篇——从0到1写出你的第一个ABAP Hello World程序并完成调试运行
运维·服务器·数据库·学习·sap·abap
m0_733565461 小时前
bootstrap怎么实现响应式的文章瀑布流布局
jvm·数据库·python