ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
複数のSQLを一括で実行するスクリプト( Oracle 接続 ) ( No.15 )
日時: 2008/07/15 21:05
名前: lightbox



拡張子:
このスクリプトを使用しなくても、それ以上の結果を sqlplus で得る事ができます。
区切り文字が、SQL 内で使用されている場合は、そこで SQL が分割されてしまうので、
その場合は、使用者が意図的に区切り文字を "/" 以外に変更して運用する必要があります

このスクリプトの価値は、実際にスクリプトを書ける人が応用できる場合に限られます。

但し、MDB 接続 はかなり違った事ができるので、その同等のスクリプトのサンプルとして
参考にして下さい
execute.vbs
拡張子:
' **********************************************************
' 使用方法
' cscript.exe execute.vbs 対象SQLの書かれたファイルのパス
' **********************************************************
' **********************************************************
' SQLコマンド分割文字
' **********************************************************
strDelimiter = "/"
' **********************************************************
' 接続文字列作成
' **********************************************************
ConnectionString = "Provider=MSDASQL;" & _
"Driver={Microsoft ODBC for Oracle};" & _
"SERVER=PED0-034/ORCL;" & _
"UID=lightbox;" & _
"PWD=lightbox"

Set WshShell = CreateObject("WScript.Shell")
if WScript.Arguments.Count <> 0 then
	ScriptPath = WScript.Arguments(0)
else
	MsgBox "SQL が書かれたテキストへのパスを指定して下さい   "
	Wscript.Quit
end if

if Trim(ConnectionString) = "" then
	MsgBox "対象 RDBMS に接続して接続文字列を取得して下さい   "
	Wscript.Quit
end if

if vbOk <> MsgBox( "対象ファイルは " & ScriptPath & " です" & vbCrLf & vbCrLf & _
	"実行前に現在の接続文字列を確認して下さい->   " & _
	vbCrlf & vbCrLf & ConnectionString, vbOkCancel ) then
	Wscript.Quit
end if

' **********************************************************
' オブジェクト作成
' **********************************************************
Set Fs = CreateObject("Scripting.FileSystemObject")
Set Cn = CreateObject( "ADODB.Connection" )

' **********************************************************
' 接続
' **********************************************************
on error resume next
Cn.Open ConnectionString
if Err.Number <> 0 then
	Wscript.echo Err.Description
	Wscript.quit
end if
on error goto 0

' **********************************************************
' 読み出し
' **********************************************************
Set fp = Fs.OpenTextFile( ScriptPath, 1 )
strCommand = " "
nCommand = 0
Do While fp.AtEndOfStream <> True
   Buff = fp.ReadLine
   if not Left( Trim( Buff ), 2 ) = "--" then
      aCommand = Split(strCommand,strDelimiter)
      if Ubound( aCommand ) <> 0 then
         if Trim(aCommand(0)) <> "" then
            on error resume next
            nCommand = nCommand + 1
            if nCommand mod 1000 = 0 then
               Call WshShell.LogEvent( 4, _
                "ADOによる更新SQLのバッチ実行 : " & _
               nCommand & " 件の処理が終了しました" )
            end if
            Wscript.Echo nCommand & ":" & aCommand(0)
            Cn.Execute aCommand(0)
            if Err.Number <> 0 then
               Wscript.Echo nCommand & ":ERR:" & Err.Description
            end if
            on error goto 0
         end if
         For i = 1 to Ubound( aCommand )
            if i = 1 then
               strCommand = aCommand(i)
            else
               strCommand = strCommand & strDelimiter & aCommand(i)
            end if
         Next
      end if
      strCommand = strCommand & vbCrLf & Buff
   end if
Loop
fp.Close

aCommand = Split(strCommand,strDelimiter)
For i = 0 to Ubound( aCommand )
   if Trim(Replace(aCommand(i),vbCrLf,"")) <> "" then
      on error resume next
      nCommand = nCommand + 1
      Wscript.Echo nCommand & ":" & aCommand(i)
      Cn.Execute aCommand(i)
      if Err.Number <> 0 then
         Wscript.Echo  nCommand & ":ERR:" & Err.Description
      end if
      on error goto 0
   end if
Next

' **********************************************************
' 接続解除
' **********************************************************
Cn.Close

MsgBox ScriptPath & vbCrLf & "を使用した更新 SQL のバッチ実行が終了しました   "
実行サンプルテキスト
拡張子:
create table 社員テスト1
as
select * from 社員マスタ
/
create table 社員テスト2
as
select * from 社員マスタ
/
create table 社員テスト3
as
select * from 社員マスタ
/
実行サンプル
拡張子:
C:\user\lightbox\Oracle>cscript execute.vbs update_test.sql
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

1:
create table 社員テスト1
as
select * from 社員マスタ

2:
create table 社員テスト2
as
select * from 社員マスタ

3:
create table 社員テスト3
as
select * from 社員マスタ

C:\user\lightbox\Oracle>