2011-02-01 19 views
2

リモートマシン上のsqlにログインするこのバッチファイルは、ストアドプロシージャを実行してから出力をテキストファイルに送信します。 IPアドレスの3番目のオクテットと出力テキストファイルの名前を1ずつ増やしてループを繰り返すので、コマンドを何度も繰り返す必要はありません。また、一定数に達すると停止したいと思います。これを行う方法はありますか?あなたが実際にではなくMS-DOSよりもcmd.exeを使用している前提でDOSのバッチファイル - ループと1ずつインクリメント

sqlcmd -U user -P password -S 192.168.1.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput01.txt 
sqlcmd -U user -P password -S 192.168.2.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput02.txt 
sqlcmd -U user -P password -S 192.168.3.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput03.txt 
sqlcmd -U user -P password -S 192.168.4.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput04.txt 
sqlcmd -U user -P password -S 192.168.5.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput05.txt 
+1

実際にはDOSについて話しているのですか、現在のWindowsシステムのcmd.exeで実行される.BATファイルを書いていますか?私はそれが後者だと思います。 –

答えて

2

、次のように変数をインクリメントし、テストするための一つの方法は次のとおりです。

@setlocal enableextensions enabledelayedexpansion 
    @echo off 
    set /a "i = 1" 
:loop 
    if !i! leq 15 (
     if !i! lss 10 (
      echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput0!i!.txt 
     ) else (
      echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput!i!.txt 
     ) 
     set /a "i = i + 1" 
     goto :loop 
    ) 
    endlocal 

これは何を少し変更したバージョンですあなたはそれらを実行するのではなく、関連するビットをエコーいる必要があり、それが出力:

sqlcmd -S 192.168.1.2 -o c:\sql\ouput01.txt 
sqlcmd -S 192.168.2.2 -o c:\sql\ouput02.txt 
sqlcmd -S 192.168.3.2 -o c:\sql\ouput03.txt 
sqlcmd -S 192.168.4.2 -o c:\sql\ouput04.txt 
sqlcmd -S 192.168.5.2 -o c:\sql\ouput05.txt 
sqlcmd -S 192.168.6.2 -o c:\sql\ouput06.txt 
sqlcmd -S 192.168.7.2 -o c:\sql\ouput07.txt 
sqlcmd -S 192.168.8.2 -o c:\sql\ouput08.txt 
sqlcmd -S 192.168.9.2 -o c:\sql\ouput09.txt 
sqlcmd -S 192.168.10.2 -o c:\sql\ouput10.txt 
sqlcmd -S 192.168.11.2 -o c:\sql\ouput11.txt 
sqlcmd -S 192.168.12.2 -o c:\sql\ouput12.txt 
sqlcmd -S 192.168.13.2 -o c:\sql\ouput13.txt 
sqlcmd -S 192.168.14.2 -o c:\sql\ouput14.txt 
sqlcmd -S 192.168.15.2 -o c:\sql\ouput15.txt 

が単にオフラインechoを取り、commanを調整しますdを押して他のビットを戻します。

8

これはあなたが望むことをします。/L指定子は、ループの通常のプログラミングのように動作するように指示します。最初のパラメータは開始整数で、次の値はステップ数、最後はカウントです。したがって、この意志ループは、1から始まる6つの整数に対して1ずつ増加:

@echo off 
FOR /L %%G IN (1, 1, 6) DO (
    echo sqlcmd -U user -P password -S 192.168.%%G.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput0%%G.txt 
) 

、あなたはIPアドレスのリストの代わりの範囲をしたい場合は、/ Lをオフのままにし、数字だけを一覧表示することができます。例えば%% G IN(1,2,3,99,121)FOR ない...

明らかに "エコー" のsqlcmdは単なるテストのためになる前には、)

+0

これは一度エコーを取り除くと機能するはずですが、クエリの所要時間に応じてSTART/WAITを使うことを考えているかもしれません。 – Vinnie

+0

ありがとうございます。これは完全に機能しました! – user597875

0

あなたはあなたがしてCMD.EXEへのアクセス権を持っている場合また、あなたはJavascriptでDOSのバッチファイルを書くことを可能にするcscriptへのアクセス権を持っています。

関連する問題