FROM DBA_TS_QUOTAS
WHERE USERNAME LIKE UPPER('%&&uname%') OR UPPER('&&uname') IS NULL ORDER BY USERNAME; spool off 219
10.2.7 如何产生创建表结构的脚本
可以通过查询 Oracle 的数据字典USER_TABLES 和USER_TAB_COLUMNS 来产生创建表的 脚本。这样的要求,其实主要是decode 函数的使用。下面给出两个例子。 例 1.从USER_TAB_COLUMNS 来产生创建表的脚本:
/************************************************************/ /* 功能:自动产生创建表的脚本 */ /* 文件名:gen_cre_tab1.sql */ /* 作者:赵元杰 */ /* 日期:2001.12.10 */
/************************************************************/ setlinesize 500 setpagesize 1000 setarraysize 8 set feedback off set heading off
select decode(t1.column_id,1,'CREATE TABLE '||t1.table_name|| ' (', ' ') a, t1.column_name b,
t1.data_type||decode(t1.data_type,'DATE',
decode(t1.NULLABLE,'N',' not null'),
'VARCHAR2','('||to_char(t1.data_length)||')'|| decode(t1.NULLABLE,'N',' not null'),
'NUMBER',decode(t1.data_precision,null,‘ ‘ , '('||to_char(t1.data_precision)|| ','||to_char(t1.data_scale)||')' ) ||decode(t1.NULLABLE,'N',' not null'), 'CHAR','('||to_char(t1.data_length)||')'|| decode(t1.NULLABLE,'N',' not null'))||
decode(t1.column_id,max(t2.column_id),');',',') c FROM user_tab_columns t1,user_tab_columns t2 WHERE t1.table_name = t2.table_name and
t1.table_name in (select distinct object_name from user_objects whereobject_type='TABLE')
group by t1.column_id,t1.table_name,t1.data_type,t1.nullable, t1.data_length,t1.data_scale,t1.column_name, t1.data_precision
order by t1.table_name,t1.column_id;
例2.从USER_TABLES 和USER_TAB_COLUMNS 来产生创建表的脚本: 220
/************************************************************/ /* 功能:自动产生创建表的脚本和相应的存储参数 */ /* 文件名:gen_cre_tab2.sql */ /* 作者:赵元杰 */
/* 日期:2001.12.10 */
/************************************************************/ setlinesize 500 setpagesize 1000 setarraysize 8 set feedback off set heading off
select decode(t1.column_id,1,'CREATE TABLE '||t1.table_name|| ' (', ' ') a, t1.column_name b,
t1.data_type||decode(t1.data_type,'DATE', decode(t1.NULLABLE,'N',' not null'),
'VARCHAR2','('||to_char(t1.data_length)||')'|| decode(t1.NULLABLE,'N',' not null'),
'NUMBER',decode(t1.data_precision,null,‘ ‘ , '('||to_char(t1.data_precision)|| ','||to_char(t1.data_scale)||')' ) ||decode(t1.NULLABLE,'N',' not null'), 'CHAR','('||to_char(t1.data_length)||')'|| decode(t1.NULLABLE,'N',' not null'))|| decode(t1.column_id,max(t2.column_id),
')'||chr(10)|| 'TABLESPACE ' ||t3.tablespace_name||chr(10)|| -- ‘ PCTFREE ‘||TO_CHAR(t3.pct_free)|| chr(10)|| -- ‘ PCTUSED ‘||TO_CHAR(t3.pct_used)|| chr(10)||
‘ STORAGE( INITIAL ‘||TO_CHAR(t3.initial_extent)|| chr(10)||
‘ NEXT ‘||TO_CHAR(t3.next_extent) ||’ );’ ||chr(10) , ',') c
FROM user_tab_columns t1,user_tab_columns t2 ,user_tables t3
WHERE t1.table_name = t2.table_name and t2.table_name=t3.table_name and t1.table_name in (select distinct object_name from user_objects whereobject_type='TABLE')
group by t1.column_id,t1.table_name,t1.data_type,t1.nullable, t1.data_length,t1.data_scale,t1.column_name, t1.data_precision, t3.tablespace_name , -- t3.pct_free , t3.pct_used, t3.initial_extent ,t3.next_extent order by t1.table_name,t1.column_id; 221
10.2.8 如何产生创建视图的脚本
对于视图来说,所有的创建视图的数据来源都在USER_VIEWS 数据字典。但是
USER_VIEWS 的view_name 是视图的名称;而视图的查询文本存放在text 列上。而且text 列的类型是long 类型。所以在查询前先要设置long 类型为一个较大的值(因为long 的缺 省值为80 个字符),这样就可以查询完整的视图内容。 setlinesize 150 setpagesize 1000 setarraysize 8 set feedback off set heading off set long 5000
colview_name for a20 col text for a80
select 'CREATE OR REPLACE VIEW '||VIEW_NAME||' AS ',TEXT FROM USER_VIEWS order BY VIEW_NAME; 10.2.9 如何产生创建序号的脚本
对于序号来说,所有的创建序号的数据都存放在USER_SEQUENCES 数据字典。具体操 作如下:
select 'CREATE SEQUENCE '|| SEQUENCE_NAME|| ' START WITH '||to_char(min_value)|| ' INCREMENT BY '||to_char(INCREMENT_BY)||':' FROM USER_SEQUENCES order BY SEQUENCE_NAME; 10.2.10 如何为用户创建公共同义词
同义词是 Oracle 用于管理用户对象的一种方法,它可以隐藏实际对象,但是不影响其他 经过授权的使用。下面是创建用户的表、视图、序列的简单办法。 1.假设下面脚本的文件名为 cre_syn.txt: accept uname prompt '输入希望的用户名: ' set echo off set heading off set feedback off spool c:\\cre_syn.sql 222
select 'drop public synonym ' || table_name || ';' from all_tables where owner = upper('&&username') ;