SQL*PLUS使用(四)

本文深入解析SQL Plus中删除命令DEL及三种注释方式,并演示如何运行脚本、使用spool保存查询结果及巧妙删除数据库表。

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

1:sqlplus编辑sql之删除命令

SQL> help del

 DEL
 ---

 Deletes one or more lines of the SQL buffer. The buffer has no
 command history list and does not record SQL*Plus commands.

 DEL [n | n m | n * | n LAST | * | * n | * LAST | LAST]


sqlplus中 只有list 和 del命令不是对单行操作,del能够删除一行
或者多行,

del n   删除指定行n
del n m  从 n到m 行删除


del n * 从 n行到 当前行 删除


del n last  从n行到 最后一行删除


del * 删除当前行


del * n 删除 当前行到 n行


del * last  从当前行 到 最后删除


del last   删除最后一行


以上命令也适用于 list

二:sqlplus中的三种注释

在sqlplus中支持以下三种注释方式:
在脚本中加注释:
1:sqlplus 支持 remark 命令 注释单独的行
2:sql 注释是 /* ....*/
3:ANSI/ISO 制定的标准: --  支持其他数据库

SQL> help remark

 REMARK
 ------

 Begins a comment in a script. SQL*Plus does not interpret the comment
 as a command.

 REM[ARK]


注释的注意事项:
1:在语句开始的时候插入注释,sqlplus会根据刚开始的关键字判断
用户输入的语句是SQL命令,还是PL/SQL语句;
2:不要在一个语句的结束符后加注释。  会干扰sqlplus的判断
3:行注释后不能加分号;
4:不要在注释中加入& 符号

三:如何运行一个脚本

运行脚本的时候如果想看到脚本中的命令
可以:
show echo

set echo on
然后再运行脚本的时候,就显示脚本中的命令同时显示运行结果
如果
set echo off
只看到运行结果,无法看到脚本中的命令;

例如有一个脚本:为ss
脚本内容为:
select * from test  where id > 10 and id <15;
SQL> set echo on
SQL> @ss
SQL> select * from test  where id > 10 and id <15;

ID                                                 NAME
-------------------------------------------------- --------------------
11                                                 top
12                                                 top
13                                                 top
14                                                 top
11                                                 top
12                                                 top
13                                                 top
14                                                 top

已选择8行。
执行结果如上;可以看到脚本中的执行命令。

使用 SQLPATH 环境变量: 这个没有设置成功

四:进入sqlplus的时候就运行脚本

如果想在启动sqlplus的时候就运行脚本可以这样做:


如果有一个脚本叫 first.sql
如果登陆进入sqlplus就执行脚本可以这样做:


sqlplus hr/hr  @first.sql


也可以把用户名放在 first.sql 的第一行,这样用 sqlplus /nolog
这个时候 @first.sql会提示输入密码,输入密码后就可以进入了,
first.sql的脚本内容也执行。

ex:
C:\Users\topwqp>sqlplus HR/HR@ORCL @ss.sql

SQL*Plus: Release 11.1.0.6.0 - Production on 星期三 5月 29 23:38:24 2013

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


连接到:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


ID                                                 NAME
-------------------------------------------------- --------------------
11                                                 top
12                                                 top
13                                                 top
14                                                 top
11                                                 top
12                                                 top
13                                                 top
14                                                 top

已选择8行。

五:使用 spool 保存你的查询

用法:
SQL> help spool

 SPOOL
 -----

 Stores query results in a file, or optionally sends the file to a printer.

 SPO[OL] [file_name[.ext] [CRE[ATE] | REP[LACE] | APP[END]] | OFF | OUT]

通常用法:
spool  top.txt
select * from test where id > 10 and id < 15;
spool off
这样查询的结果就到了 top.txt文件中了。
ex:
SQL> spool top.txt
SQL> select * from test where id > 10 and
  2   id < 15;

ID                                                 NAME
-------------------------------------------------- --------------------
11                                                 top
12                                                 top
13                                                 top
14                                                 top
11                                                 top
12                                                 top
13                                                 top
14                                                 top

已选择8行。

SQL> spool off

查询出来 top.txt的内容为:
SQL> select * from test where id > 10 and
  2   id < 15;

ID                                                 NAME                         
-------------------------------------------------- --------------------         
11                                                 top                          
12                                                 top                          
13                                                 top                          
14                                                 top                          
11                                                 top                          
12                                                 top                          
13                                                 top                          
14                                                 top                          

已选择8行。

SQL> spool off

六:巧妙利用spool 删除表

巧妙利用spool 删除表


spool droptable.sql
spool off
set pagesize 0
set feedback  off
spool droptable.sql
select 'drop table' || object_name || ';' from user_objects where object_type='TABLE';
spool off

ex:
SQL> spool droptable.sql
SQL> spool off
SQL> set pagesize 0
SQL> set feedback off
SQL> spool droptable.sql
SQL> select 'drop table' ||  object_name || ';' from user_objects where object_t
ype = 'TABLE'
  2  ;
drop tableTEST_SEQUENCE;
drop tableTEST;
drop tableREGIONS;
drop tableLOCATIONS;
drop tableJOB_HISTORY;
drop tableJOBS;
drop tableEMPLOYEES;
drop tableDEPARTMENTS;
drop tableCOUNTRIES;
drop tableCOPY_INDEX;
drop tableCOPY_DEPARTMENTS;
SQL> spool off

输出的droptable.sql的内容为:
SQL> select 'drop table' ||  object_name || ';' from user_objects where object_type = 'TABLE'
  2  ;
drop tableTEST_SEQUENCE;                                                        
drop tableTEST;                                                                 
drop tableREGIONS;                                                              
drop tableLOCATIONS;                                                            
drop tableJOB_HISTORY;                                                          
drop tableJOBS;                                                                 
drop tableEMPLOYEES;                                                            
drop tableDEPARTMENTS;                                                          
drop tableCOUNTRIES;                                                            
drop tableCOPY_INDEX;                                                           
drop tableCOPY_DEPARTMENTS;                                                     
SQL> spool off

稍微修改就可以完成数据库中标的删除,当然也可以 用于其它用途,只要能够巧妙组合。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值