web-dev-qa-db-ja.com

Windowsコマンドスクリプトでsql * plusを使用してフローを制御する方法

小さなWindowsコマンドスクリプトを制御するためにsql * plusを使用しようとしています。

基本的に、データベース内のいくつかの行のステータスを表示するPL/SQLを実行します(おそらくビューまたはテーブルから選択するか、関数を実行します)。次に、行の状態に応じて、いくつかのWindowsコマンドを実行します。

私の問題は、結果をコマンドスクリプトに戻す方法です。

sqlplus user/password@server @script.sql

IF <CONDITIONAL HERE BASED on script.sql results> GOTO :runprocess

REM log and email that process had to be skipped
EXIT

:runprocess
REM run various Windows service commands
8
Cade Roux

これは私が最終的に使用したものです。

私の.cmdスクリプト:

@ECHO OFF
ECHO Checking Oracle...

for /f %%i in ('sqlplus -s user/password@database @script.sql') do @set count=%%i
echo %count%

IF %count% GTR 0 GOTO :skipped
GOTO :runprocess

ここで、script.sql:

SELECT COUNT(*)
FROM table
WHERE criteria = 1;

exit
5
Cade Roux

おそらく、呼び出されたscript.sql自体からスクリプト(または要件に応じて条件付き)を記述します。

たとえば、次のscript.sql。batファイルwindows_commands.batを作成します。

set feedback off
set echo off
set trimspool on
set termout off
set serveroutput on size 100000 format wrapped
set lines 500
set pages 0

-- create the bat file to be executed later:
spool windows_commands.bat

declare
  c number;
begin

  select count(*) into c from dual;

  -- depending on a conditional, write the stuff to be executed into the
  -- bat file (windows_commands.bat)
  if c = 1 then
     dbms_output.put_line('@echo everthing ok with dual');
  else
     dbms_output.put_line('@echo something terribly wrong with dual');
  end if;

end;
/

spool off

exit

次に、別の。batファイルからscript.sqlを呼び出すことができます。

@rem create Oracle session, call script.sql
sqlplus %user%/%password%@%db% @script.sql

@rem script.sql has created windows_commands.bat.
@rem call this newly created bat file:
call windows_commands.bat
7

.batファイルを使用することを強くお勧めします。他にもたくさんの選択肢があります:C/C++またはVB、 Windowsスクリプト または Powershell 、あるいは Perl または-などの無料ダウンロード Bash

しかし、これが.batファイルでエラーコードを返す1つの例です。

しかし、私が上記で与えたリンクのいくつかを見てください。 .batファイルを使用しないことで作業が簡単になり、将来のメンテナンスも容易になります。

私見では ...

3
paulsm4

私は、Windowsのことを行う.batファイルを作成し、必要に応じてSQLスクリプトを呼び出すことによって、このようなことをしています。 SQLを使用して、読み取り可能なテキストファイルに結果をスプールします。

...ここでコマンドを実行

sqlplus/nolog @C:\ Dump\DropRecreateUsers.sql

sqlplus/nolog @C:\ Dump\Cleanup.sql

... DOSコマンド

SQLでは、このコマンドスプールC:\ yourResults.txtを使用するか、より高度な使用法のために、プロシージャが作成され、呼び出されたときにUTL_FILEを使用して結果をテキストファイルに書き込みます。

2
kevinsky

バックアップと復元のために、Oracle XEに含まれている2つのスクリプトを確認することをお勧めします。これらのスクリプトは、WindowsプラットフォームでバッチスクリプトとOracleを処理する方法を私に多く教えてくれました。

  • C:\ oraclexe\app\Oracle\product\11.2.0\server\bin\Backup.bat
  • C:\ oraclexe\app\Oracle\product\11.2.0\server\bin\Restore.bat
1
Bjarte Brandt

接続先:Oracle Database 11g Enterprise Editionリリース11.2.0.1.0-パーティション化、OLAP、データマイニング、およびReal Application Testingオプションを備えた64ビット製品

SQL> @f:\testa.txt
0
Rizwan Basheer