eg001(&替换变量)
SQL> select xh,xm from system.xs where zym='&zym';
输入 zym 的值: 计算机
原值 1: select xh,xm from system.xs where zym='&zym'
新值 1: select xh,xm from system.xs where zym='计算机'
XH XM
------ --------
061101 王林
061102 程明
061103 王燕
061104 韦严平
061106 李方方
061107 李明
061108 林一帆
061109 张强民
061110 张蔚
061111 赵琳
061113 严红
已选择11行。
SQL> edit
已写入 file afiedt.buf
1 select xh 学号,xm 姓名,avg(cj) as 平均成绩
2* from system.xs_xkb group by xh,xm
SQL> /
学号 姓名 平均成绩
------ -------- ----------
061103 王燕 71
061210 李红庆 76
061110 张蔚 91.3333333
061220 吴薇华 82
061104 韦严平 79.6666667
061101 王林 78
061204 马林林 91
061106 李方方 72
061218 孙研 70
061102 程明 78
061241 罗林琳 90
学号 姓名 平均成绩
------ -------- ----------
061111 赵琳 80.5
061109 张强民 76.5
061216 孙祥欣 81
061221 刘燕敏 79
已选择15行。
SQL> select * from system.xs_xkb where cj>=&cj; /*替换变量可以使用WHERE子句;ORDER BY子句;列表达式;表名;整个SELECT语句*/
输入 cj 的值: 90
原值 1: select * from system.xs_xkb where cj>=&cj
新值 1: select * from system.xs_xkb where cj>=90
SQL> select xs.xh,&name,kcm,&column
2 from system.xs,&kc,system.xs_kc
3 where xs.xh=xs_kc.xh and &condition
4 and kcm=&kcm
5 order by & column;
输入 name 的值: xm
输入 column 的值: cj
原值 1: select xs.xh,&name,kcm,&column
新值 1: select xs.xh,xm,kcm,cj
输入 kc 的值: system.kc
原值 2: from system.xs,&kc,system.xs_kc
新值 2: from system.xs,system.kc,system.xs_kc
输入 condition 的值: kc.kch=xs_kc.kch
原值 3: where xs.xh=xs_kc.xh and &condition
新值 3: where xs.xh=xs_kc.xh and kc.kch=xs_kc.kch
输入 kcm 的值: '离散数学'
原值 4: and kcm=&kcm
新值 4: and kcm='离散数学'
输入 column 的值: cj
原值 5: order by & column
新值 5: order by cj
XH XM KCM CJ
------ -------- ---------------- ----------
061104 韦严平 离散数学 65
061109 张强民 离散数学 70
061101 王林 离散数学 76
061102 程明 离散数学 78
061106 李方方 离散数学 80
061103 王燕 离散数学 81
061110 张蔚 离散数学 89
eg002(&&替换变量)
-----&&替换变量系统一直用同一个值处理,清除用undefine 变量名清除
SQL> edit
已写入 file afiedt.buf
1 select xs.xh,&name,kcm,&&column /*清除替换变量(undefine column)*/
2 from system.xs,&kc,system.xs_kc
3 where xs.xh=xs_kc.xh and &condition
4 and kcm=&kcm
5* order by &column
SQL> /
输入 name 的值: xm
输入 column 的值: cj
原值 1: select xs.xh,&name,kcm,&&column
新值 1: select xs.xh,xm,kcm,cj
输入 kc 的值: system.kc
原值 2: from system.xs,&kc,system.xs_kc
新值 2: from system.xs,system.kc,system.xs_kc
输入 condition 的值: kc.kch=xs_kc.kch
原值 3: where xs.xh=xs_kc.xh and &condition
新值 3: where xs.xh=xs_kc.xh and kc.kch=xs_kc.kch
输入 kcm 的值: '离散数学'
原值 4: and kcm=&kcm
新值 4: and kcm='离散数学'
原值 5: order by &column /*使用&&替换变量的好处,相同变量只输第一次就OK*/
新值 5: order by cj
XH XM KCM CJ
------ -------- ---------------- ----------
061104 韦严平 离散数学 65
061109 张强民 离散数学 70
061101 王林 离散数学 76
061102 程明 离散数学 78
061106 李方方 离散数学 80
061103 王燕 离散数学 81
061110 张蔚 离散数学 89
已选择7行。
eg003
DEFINE[variable[=value]]
UNDEFINE清除定义的变量
SQL> define specialty=通信工程
SQL> define specialty
DEFINE SPECIALTY = "通信工程" (CHAR)
SQL> select xh,xm,xb,cssj,zxf from system.xs
2 where zym='&specialty';
XH XM XB CSSJ ZXF
------ -------- -- -------------- ----------
061202 王林 男 29-10月-85 40
061210 李红庆 女 01-5月 -85 44
061201 王敏 男 10-6月 -84 42
061203 王玉民 男 26-3月 -86 42
061204 马林林 女 10-2月 -84 42
061206 李计 女 20-9月 -85 42
061216 孙祥欣 女 09-3月 -84 42
061218 孙研 男 09-10月-86 42
061220 吴薇华 女 18-3月 -86 42
061221 刘燕敏 女 12-11月-85 42
061241 罗林琳 女 30-1月 -86 50
已选择11行。
eg004
ACCEPT variable[datatype[NUMBER|CHAR|DATE]][FORMAT format][PROMPT text][HIDE]/*variable:指定接收值的变量。该名称的变量不存在,那么SQL重建该变量;datatype:变量数据类型,默认为 CHAR*/
SQL> accept num prompt'请输入课程号:'
请输入课程号:101
SQL> set verify on
SQL>
1 select xh,kcm,cj from system.xs_kc,system.kc
2 where xs_kc.kch=kc.kch and kc.kch='&num'
3* order by cj
SQL> /
原值 2: where xs_kc.kch=kc.kch and kc.kch='&num'
新值 2: where xs_kc.kch=kc.kch and kc.kch='101'
XH KCM CJ
------ ---------------- ----------
061103 计算机基础 62
061106 计算机基础 65
061218 计算机基础 70
061210 计算机基础 76
061221 计算机基础 79
061101 计算机基础 80
061216 计算机基础 81
061220 计算机基础 82
061241 计算机基础 90
061104 计算机基础 90
061111 计算机基础 91
XH KCM CJ
------ ---------------- ----------
061204 计算机基础 91
061110 计算机基础 95
已选择13行。
SQL>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23033727/viewspace-631441/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23033727/viewspace-631441/