【oracle运维】导出某属主用户的所有表结构(excel格式)

本文介绍了一个bash脚本,用于自动化导出指定属主用户在数据库中的所有表结构,包括表名、字段名和字段类型。通过一系列SQL查询和输出格式化操作,实现高效的数据提取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash
# Desc: 该脚本用于导出某属主用户的所有表结构: 表明、字段名、字段类型


## declare variables

sid_profile="/home/oracle/xxxprofile"
table_owner="xxx"
table_list_file="${table_owner}_tables.list"
get_table_structures_sql="get_${table_owner}_table_structures.sql"
table_structures_file="${table_owner}_table_structures.xls"
base_dir="/home/oracle/dw_standbys"

## env init
. ${sid_profile}
cd ${base_dir}

## get table list
sqlplus -S / as sysdba <<EOF
set linesize 200
set trimspool on feedback off pagesize 0
spool ${table_list_file}
select table_name from dba_tables where owner=upper('${table_owner}') order by 1;
spool off
EOF
sed -i '/^SQL>/d;/^$/d' ${table_list_file}

## generate sql script for getting table structures
cat /dev/null >${get_table_structures_sql}
# format sqlplus output as .xls file
echo -e "set term off verify off feedback off pagesize 999 \nset markup html on entmap ON spool on preformat off">> ${get_table_structures_sql}
#
echo -e "alter session set current_schema=\"${table_owner}\";" >>${get_table_structures_sql}
echo -e "spool ${table_structures_file}" >>${get_table_structures_sql}
echo -e "col Count_Tables for 999999999\ncol GaterDate for a30\n col TableOwner for a20">>${get_table_structures_sql}
echo -e "select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') GatherDate,'${table_owner}' TableOwner,(select count(1) from dba_tables where owner='${table_owner}') Count_Tables  from dual;">>${get_table_structures_sql}
echo -e "col comment for a30" >>${get_table_structures_sql}
for table_name in `cat ${table_list_file}`
do
  echo "select table_name,column_name,data_type,' ' as \"comment\" from DBA_TAB_COLS where owner=upper('${table_owner}') and table_name=upper('${table_name}');">>${get_table_structures_sql}
done
echo -e "spool off" >>${get_table_structures_sql}

## get table structures
sqlplus -S / as sysdba <<EOF
@${get_table_structures_sql}
EOF
result_status="$?"
if [ ${result_status} -eq 0 ];then
  echo "Info: the table structure file of ${table_owner} is ${table_structures_file}."
else
  echo "Critical: something is wrong with ${table_structures_file} !"
fi


/*----------------------------------------------------------------------------- * Function : to extract the structure of a table * * Author : Richard Wang * * Date : 2011-08-30 * * Version : 0.1 * ------------------------------------------------------------------------------*/ select 'Table_Name' table_name --,'Column_ID' column_id ,0 column_id ,'Column_Name' column_name ,'Data_Type' data_Type ,'PK_Column' PK_Column ,'Nullable' nullable from DUAL union all select distinct lower(all_c.table_name) table_name --,(case -- when column_id < 10 then '0' || to_char(column_id) -- when column_id 100 then 'H' || substrb(to_char(column_id),2,1) -- else '**' -- end ) column_id ,all_c.column_id column_id ,lower(all_c.column_name) column_name ,lower((case all_c.data_type when 'NUMBER' then all_c.data_type || decode(all_c.data_precision,0,'',NULL,'','(' || to_char(all_c.data_precision)) || decode(all_c.data_precision,0,'',NULL,'', decode(all_c.data_scale,0,'',NULL,'', ',' || to_char(all_c.data_scale)) || ')') when 'CHAR' then all_c.data_type || '(' || to_char(all_c.char_length) || ')' when 'VARCHAR2' then all_c.data_type || '(' || to_char(all_c.char_length) || ')' else all_c.data_type end)) data_type ,decode(tab_pk.pk_col_name,NULL,'',tab_pk.pk_col_name,'Y','N/A') PK_column ,all_c.nullable nullable from all_tab_columns all_c left outer join (select cu.table_name pk_tab_name ,cu.column_name pk_col_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P') tab_pk on all_c.table_name = tab_pk.pk_tab_name and all_c.column_name = tab_pk.pk_col_name where owner not like '%SYS%' and table_name = upper('V_CHL_PORTFOLIO') -- upper case-- order by table_name,column_id,column_name select * from all_tab_columns where table_name = 'CIS_INF_PARM_VALUE' select distinct table_name from all_tab_columns where owner not like '%SYS%' and table_name like '%DELIV%' -- upper case-- order by table_name
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值