这篇文章介绍一下如何对sqlplus执行的sql语句结果进行判断。
使用Oracle的精简版创建docker方式的demo环境,详细可参看:
在sqlplus中执行sql语句,如果直接使用命令行的方式调用时会碰到两个问题:
在脚本调用里,解决方式如下
这里使用命令行的方式进行验证,直接拷贝到脚本中即可以使用脚本的方式与sqlplus进行集成。
oracle@e871d42341c0:~$ sqlplus system/liumiao123@XE < desc student
> delete from student;
> select * from student;
> insert into student values (1001, 'liumiaocn');
> insert into student values (1001, 'liumiao');
> insert into student values (1003, 'michael');
> commit;
> select * from student;
> EOF
SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:18:51 2018
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> Name Null? Type
----------------------------------------- -------- ----------------------------
STUID NOT NULL NUMBER(4)
STUNAME VARCHAR2(50)
SQL>
3 rows deleted.
SQL>
no rows selected
SQL>
1 row created.
SQL> insert into student values (1001, 'liumiao')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
SQL>
1 row created.
SQL>
Commit complete.
SQL>
STUID STUNAME
---------- --------------------------------------------------
1001 liumiaocn
1003 michael
SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
oracle@e871d42341c0:~$ echo $?
0
oracle@e871d42341c0:~$
从上文可以看到,三行insert的sql语句由于第二行的主键重复,出现错误,但是最终的结果使用命令行的方式无法对结果进行判断,这是控制台方式的常见场景,比如sftp或者ftp等也有此特点,一般常用的对应方式无法通过返回值进行判断,只能通过输出来进行判断。
输出分为标准输出和标准错误两种,输入输出的FD分别为:
接下来我们看一下上文中的信息那些是标准输出,哪些是标准错误:
oracle@e871d42341c0:~$ sqlplus system/abcd1234@XE <output.error 1>output.info
> desc student
> delete from student;
> select * from student;
> insert into student values (1001, 'liumiaocn');
> insert into student values (1001, 'liumiao');
> insert into student values (1003, 'michael');
> commit;
> select * from student;
> EOF
oracle@e871d42341c0:~$
oracle@e871d42341c0:~$ cat output.error
oracle@e871d42341c0:~$
oracle@e871d42341c0:~$ cat output.info
SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:24:44 2018
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> Name Null? Type
----------------------------------------- -------- ----------------------------
STUID NOT NULL NUMBER(4)
STUNAME VARCHAR2(50)
SQL>
2 rows deleted.
SQL>
no rows selected
SQL>
1 row created.
SQL> insert into student values (1001, 'liumiao')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
SQL>
1 row created.
SQL>
Commit complete.
SQL>
STUID STUNAME
---------- --------------------------------------------------
1001 liumiaocn
1003 michael
SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
oracle@e871d42341c0:~$
可以看到错误信息全在标准输出中,标准错误中没有信息。
虽然上述信息中可以看到,标准错误中没有信息,这里给出的方案是对应常见的控制台方式的错误控制,为了保证标准错误的信息不被遗漏,需要将标准错误和重定向到标准输出中,在bshell中写法如下:
>输出文件名称 2>&1
结合本文的例子,使用方式如下:
oracle@e871d42341c0:~$ sqlplus system/abcd1234@XE <output.info 2>&1
> desc student
> delete from student;
> select * from student;
> insert into student values (1001, 'liumiaocn');
> insert into student values (1001, 'liumiao');
> insert into student values (1003, 'michael');
> commit;
> select * from student;
> EOF
oracle@e871d42341c0:~$
oracle@e871d42341c0:~$ cat output.info
SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 22 05:29:31 2018
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> Name Null? Type
----------------------------------------- -------- ----------------------------
STUID NOT NULL NUMBER(4)
STUNAME VARCHAR2(50)
SQL>
2 rows deleted.
SQL>
no rows selected
SQL>
1 row created.
SQL> insert into student values (1001, 'liumiao')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
SQL>
1 row created.
SQL>
Commit complete.
SQL>
STUID STUNAME
---------- --------------------------------------------------
1001 liumiaocn
1003 michael
SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
oracle@e871d42341c0:~$
使用grep确认是否存在ORA-相关的信息即可
oracle@e871d42341c0:~$ grep ORA- output.info
ORA-00001: unique constraint (SYSTEM.SYS_C007024) violated
oracle@e871d42341c0:~$ echo $?
0
oracle@e871d42341c0:~$