SQL*Plus连接符拼接输出

在日常工作中,可能需要使用重复的命令,修改的只是某个不同字段的值,可以使用连接字符串进行拼接

#本篇文档:

一、使用连接符拼接SQL

二、Spool输出查询结果

三、Spool输出xml/  html格式内容

 

 

1.1连接符

举例A:对某个用户下的表进行drop

>select 'Drop table '||owner||'.'||table_name||';' from dba_tables where owner='HR'
and table_name not in('EMPLOYEES','DEPARTMENTS'); 'DROPTABLE'||OWNER||'.'||TABLE_NAME ---------------------------------------------------- Drop table HR.LOCATIONS;

1.2连接符's  特殊用法

>select 'department name '||department_name||q'['s sal is]'||salary||';' from hr.employees e,hr.departments d where e.DEPARTMENT_ID=d.DEPARTMENT_ID;

department name Administration's sal is4400;
department name Marketing's sal is13000;


2,1 Spool内容

参数:

set heading off           设置显示列名:查询的显示字段名称取消

set feedback off         设置显示“ 已选择行”:最后显示的查询行结果取消

Spool

查询结果输出到一个文件:可以进行编辑,粘贴,使用:

spool /home/oracle/drop_hr_table.sql

select 'Drop table '||owner||'.'||table_name||';' from dba_tables where owner='HR'
 and table_name not in('EMPLOYEES','DEPARTMENTS');

>spool  off

More Spool

[oracle@sh ~]$ more drop_hr_table.sql

01:20:39 SYS@env >select 'Drop table '||owner||'.'||table_name||';' from dba_tables where owner='HR' 01:20:41   2   and table_name not in('EMPLOYEES','DEPARTMENTS');

Drop table HR.LOCATIONS;                                                                                                

Drop table HR.JOBS;  
#有一个缺陷:如上查询有执行的sql语句

 

 

 

2.2 Spool 输出xml/hrml格式内容        --采用盖国强老师书籍学习

参数:

linesize 行长度200

term  是否显示输出内容

verify   输出变量内容

feedback 返回的记录行数量

markup html  输出html格式内容


main.sql 设置环境,调用具体执行脚本

set linesize 200

set term off verify off feedback off pagesize 999

set markup html on entmap on spool on preformat off

spool tables.xls

@get_tables.sql

spool off

exit

 

get_tables.sql具体执行脚本

select owner,table_name,tablespace_name,blocks,last_analyzed from all_tables order by 1,2;


执行:

sqlplus "/ as sysdba" @main

 

 
 
#以上是XML格式:修改sppol table.xls  修改为html则输出html格式
$ cp main.sql main.html

$ vi main.html

spool tables.html

#执行

sqlplus "/ as sysdba" @main.html

 

#显示输出分隔符号
SQL> set colsep |

SQL> select username,password,default_tablespace from dba_users


USERNAME  |PASSWORD  |DEFAULT_TABLESPACE

----------|----------|------------------------------

PERFSTAT  |          |STATSPACK

 

 

posted @ 2018-05-19 11:50  绿茶有点甜  阅读(348)  评论(0编辑  收藏  举报