oracle sqlplus调用.sql文件 bat调用sql 并出力spool log

++++++++++++++++++++++++++++++++sqlplus调用.sql+++++++++++++++++++++++++++++++++

登陆sqlplus

sqlplus / as sysdba

sql>@D:test.sql

--------------------------------test.sql----------------------------------------

set echo off
--打开DBMS_OUTPUTlog

set serveroutput on
--spool log 将显示到控制台上的内容出力到log文件里

spool on
--spool log 出力位置

spool C:\test.lst

WHENEVER SQLERROR EXIT 2 ROLLBACK
--非过程语句写到BEGIN END 之外
select * from aoan.testTBL;

DECLARE
    allKensu NUMBER;

--过程语句写到BEGIN END 块里
BEGIN
   select count(*) into allKensu from aoan.table1;

--
    DBMS_OUTPUT.PUT_LINE('count:' || allKensu);
END;
/

set serveroutput off
spool off

--结束sqlplus否则执行完后一直停留在sqlplus状态,如果是从shell中调用的则必须要有exit否则shell永远等待sqlplus的结束

exit sql.sqlcode

--------------------------------------------------echo off状态下的spool log------------------------------------------------------------------------

  安全化実施                                                                   
- ----------                                                                   
1 aaa                                                                         
2 bbb                                                                         
3 ccc                                                                         
9 ddd                                                                       

count:4                                                                       

PL/SQL存储过程正常完了。

--------------------------------------------------echo on状态下的spool log---------------------------------------------------------------------------------

SQL> WHENEVER SQLERROR EXIT 2 ROLLBACK

SQL> select * from aoan.table1;

  安全化実施                                                                   
- ----------                                                                   
1 aaa                                                                         
2 bbb                                                                         
3 ccc                                                                         
9 ddd                                                                       

SQL>
SQL> DECLARE
  2    allKensu NUMBER;
  3  BEGIN
  4        select count(*) into allKensu from aoan.table1;
  5    DBMS_OUTPUT.PUT_LINE('count:' || allKensu);
  6  END;
  7  /
count:4                                                                       

PL/SQL 存储过程正常完了。

SQL>
SQL> set serveroutput off
SQL> spool off

++++++++++++++++++++++++++++++++bat调用.sql+++++++++++++++++++++++++++++++++

sqlplus aoan/aoan@orcl @D:test.sql > test.log

::在控制台上显示sql返回值,异常返回2,正常返回sqlcode即0
echo %errorlevel%

pause

你可能感兴趣的:(oracle)