详见SQL*Plus® User's Guide and Reference Release 11.2
5 Using Scripts in SQL*Plus
1.定义环境变量
_EDITOR
如:
DEFINE _EDITOR = vi
然后,
编辑
EDIT SALES
在文档中写要执行的sql语句
最后
@sales执行
SQL> l
1 select
2 *
3 from
4* v$instance
1 select
2 *
3 from
4* v$instance
最后一行带*表示当前行,选择前面的数字回车改变当前行
所有的操作如下:
Command | Abbreviation | Purpose |
---|---|---|
APPEND text |
A text |
adds text at the end of the current line |
CHANGE/old/new |
C/old/new |
changes old to new in the current line |
CHANGE/text |
C/text |
deletes text from the current line |
CLEAR BUFFER |
CL BUFF |
deletes all lines |
DEL |
(none) |
deletes the current line |
DEL n |
(none) |
deletes line n |
DEL * |
(none) |
deletes the current line |
DEL n * |
(none) |
deletes line n through the current line |
DEL LAST |
(none) |
deletes the last line |
DEL m n |
(none) |
deletes a range of lines (m to n) |
DEL * n |
(none) |
deletes the current line through line n |
INPUT |
I |
adds one or more lines |
INPUT text |
I text |
adds a line consisting of text |
LIST |
; or L |
lists all lines in the SQL buffer |
LIST n |
L n or n |
lists line n |
LIST * |
L * |
lists the current line |
LIST n * |
L n * |
lists line n through the current line |
LIST LAST |
L LAST |
lists the last line |
LIST m n |
L m n |
lists a range of lines (m to n) |
LIST * n |
L * n |
lists the current line through line n |
2.注释
(1)使用REMARK
(2)使用/*...*/
(3)使用 --
3. get取得系统文件中的内容
SELECT &GROUP_COL, MAX(&NUMBER_COL) MAXIMUM
FROM &TABLE
GROUP BY &GROUP_COL
.
SAVE STATS
GET STATS
SELECT &GROUP_COL, |
2
2* MAX(&NUMBER_COL) MAXIMUM |
APPEND ,
2* MAX(&NUMBER_COL) MAXIMUM, |
CHANGE/&/&&
2* MAX(&&NUMBER_COL) MAXIMUM, |
I
3i |
MIN (&&NUMBER_COL) MINIMUM,
4i |
SUM(&&NUMBER_COL) TOTAL,
5i |
AVG(&&NUMBER_COL) AVERAGE
6i |
1
1* SELECT &GROUP_COL, |
CHANGE/&/&&
1* SELECT &&GROUP_COL, |
7
7* GROUP BY &GROUP_COL |
CHANGE/&/&&/
7* GROUP BY &&GROUP_COL |
SAVE STATS2
Created file STATS2 |
4.输出提示信息:
脚本中使用
PROMPT
输出提示信息
例:
PROMPT Enter a title of up to 30 characters
ACCEPT MYTITLE PROMPT 'Title: '
TTITLE LEFT MYTITLE SKIP 2
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY
FROM EMP_DETAILS_VIEW
WHERE JOB_ID='SA_MAN'
5. 清空缓冲
CLEAR BUFFER
6.清屏
CLEAR SCREEN
把所有列恢复到原来的格式:
CLEAR COLUMNS