在日常工作中,可能需要使用重复的命令,修改的只是某个不同字段的值,可以使用连接字符串进行拼接
#本篇文档:
一、使用连接符拼接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;
参数:
set heading off 设置显示列名:查询的显示字段名称取消set feedback off 设置显示“ 已选择行”:最后显示的查询行结果取消
Spool
查询结果输出到一个文件:可以进行编辑,粘贴,使用:
spool /home/oracle/drop_hr_table.sqlselect '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.sql01: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 行长度200term 是否显示输出内容verify 输出变量内容feedback 返回的记录行数量markup html 输出html格式内容main.sql 设置环境,调用具体执行脚本set linesize 200set term off verify off feedback off pagesize 999set markup html on entmap on spool on preformat offspool tables.xls@get_tables.sqlspool offexit 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.htmlspool tables.html#执行sqlplus "/ as sysdba" @main.html
#显示输出分隔符号
SQL> set colsep |SQL> select username,password,default_tablespace from dba_usersUSERNAME |PASSWORD |DEFAULT_TABLESPACE----------|----------|------------------------------PERFSTAT | |STATSPACK