Oracle基础:通过sqlplus执行sql语句后的结果进行判断
这篇文章介绍一下如何对sqlplus执行的sql语句结果进行判断。
环境准备
使用Oracle的精简版创建docker方式的demo环境,详细可参看:
https://www.nhooo.com/article/153533.htm
常见问题
在sqlplus中执行sql语句,如果直接使用命令行的方式调用时会碰到两个问题:
- 问题1:需要进行交互性的输入
- 问题2:结果的判断不能通过返回值来确认
解决方式
在脚本调用里,解决方式如下
- 问题1可以通过前文提到的HereDocument来解决。
- 问题2,则可以通过对输出的判断来进行,因为Oracle的错误代码基本以ORA-打头,所以根据输出中是否包含ORA-,基本可确认是否存在错误。
执行结果判断示例
这里使用命令行的方式进行验证,直接拷贝到脚本中即可以使用脚本的方式与sqlplus进行集成。
oracle@e871d42341c0:~$sqlplussystem/liumiao123@XE<descstudent >deletefromstudent; >select*fromstudent; >insertintostudentvalues(1001,'liumiaocn'); >insertintostudentvalues(1001,'liumiao'); >insertintostudentvalues(1003,'michael'); >commit; >select*fromstudent; >EOF SQL*Plus:Release11.2.0.2.0ProductiononMonOct2205:18:512018 Copyright(c)1982,2011,Oracle.Allrightsreserved. Connectedto: OracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction SQL>NameNull?Type ----------------------------------------------------------------------------- STUIDNOTNULLNUMBER(4) STUNAMEVARCHAR2(50) SQL> 3rowsdeleted. SQL> norowsselected SQL> 1rowcreated. SQL>insertintostudentvalues(1001,'liumiao') * ERRORatline1: ORA-00001:uniqueconstraint(SYSTEM.SYS_C007024)violated SQL> 1rowcreated. SQL> Commitcomplete. SQL> STUIDSTUNAME ------------------------------------------------------------ 1001liumiaocn 1003michael SQL>DisconnectedfromOracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction oracle@e871d42341c0:~$echo$? 0 oracle@e871d42341c0:~$
从上文可以看到,三行insert的sql语句由于第二行的主键重复,出现错误,但是最终的结果使用命令行的方式无法对结果进行判断,这是控制台方式的常见场景,比如sftp或者ftp等也有此特点,一般常用的对应方式无法通过返回值进行判断,只能通过输出来进行判断。
输出信息
输出分为标准输出和标准错误两种,输入输出的FD分别为:
- 标准输入:0
- 标准输出:1
- 标准错误:2
接下来我们看一下上文中的信息那些是标准输出,哪些是标准错误:
oracle@e871d42341c0:~$sqlplussystem/abcd1234@XE<output.error1>output.info >descstudent >deletefromstudent; >select*fromstudent; >insertintostudentvalues(1001,'liumiaocn'); >insertintostudentvalues(1001,'liumiao'); >insertintostudentvalues(1003,'michael'); >commit; >select*fromstudent; >EOF oracle@e871d42341c0:~$ oracle@e871d42341c0:~$catoutput.error oracle@e871d42341c0:~$ oracle@e871d42341c0:~$catoutput.info SQL*Plus:Release11.2.0.2.0ProductiononMonOct2205:24:442018 Copyright(c)1982,2011,Oracle.Allrightsreserved. Connectedto: OracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction SQL>NameNull?Type ----------------------------------------------------------------------------- STUIDNOTNULLNUMBER(4) STUNAMEVARCHAR2(50) SQL> 2rowsdeleted. SQL> norowsselected SQL> 1rowcreated. SQL>insertintostudentvalues(1001,'liumiao') * ERRORatline1: ORA-00001:uniqueconstraint(SYSTEM.SYS_C007024)violated SQL> 1rowcreated. SQL> Commitcomplete. SQL> STUIDSTUNAME ------------------------------------------------------------ 1001liumiaocn 1003michael SQL>DisconnectedfromOracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction oracle@e871d42341c0:~$
可以看到错误信息全在标准输出中,标准错误中没有信息。
重定向标准输出与错误判断
虽然上述信息中可以看到,标准错误中没有信息,这里给出的方案是对应常见的控制台方式的错误控制,为了保证标准错误的信息不被遗漏,需要将标准错误和重定向到标准输出中,在bshell中写法如下:
>输出文件名称2>&1
结合本文的例子,使用方式如下:
oracle@e871d42341c0:~$sqlplussystem/abcd1234@XE<output.info2>&1 >descstudent >deletefromstudent; >select*fromstudent; >insertintostudentvalues(1001,'liumiaocn'); >insertintostudentvalues(1001,'liumiao'); >insertintostudentvalues(1003,'michael'); >commit; >select*fromstudent; >EOF oracle@e871d42341c0:~$ oracle@e871d42341c0:~$catoutput.info SQL*Plus:Release11.2.0.2.0ProductiononMonOct2205:29:312018 Copyright(c)1982,2011,Oracle.Allrightsreserved. Connectedto: OracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction SQL>NameNull?Type ----------------------------------------------------------------------------- STUIDNOTNULLNUMBER(4) STUNAMEVARCHAR2(50) SQL> 2rowsdeleted. SQL> norowsselected SQL> 1rowcreated. SQL>insertintostudentvalues(1001,'liumiao') * ERRORatline1: ORA-00001:uniqueconstraint(SYSTEM.SYS_C007024)violated SQL> 1rowcreated. SQL> Commitcomplete. SQL> STUIDSTUNAME ------------------------------------------------------------ 1001liumiaocn 1003michael SQL>DisconnectedfromOracleDatabase11gExpressEditionRelease11.2.0.2.0-64bitProduction oracle@e871d42341c0:~$
结果判断
使用grep确认是否存在ORA-相关的信息即可
oracle@e871d42341c0:~$grepORA-output.info
ORA-00001:uniqueconstraint(SYSTEM.SYS_C007024)violated
oracle@e871d42341c0:~$echo$?
0
oracle@e871d42341c0:~$
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接