bat文件运行sqlplus然后根据结果执行命令?

时间:2013-04-19 14:12:13

标签: oracle batch-file sqlplus

我可以让BAT文件运行SQLPlus命令,然后根据结果执行CMD命令,然后执行另一个SQLPlus命令吗?

我认为它看起来像这样

CheckRowCount.SQL文件

SELECT COUNT(*) FROM dmsn.ds3r_1xrtt_voice_trigger

BAT文件:

sqlplus %USER%/%PASSWORD%@ORACLE @CheckRowCount.SQL
if ROWCOUNT < 1 goto :EX
c:\Autobatch\Spotfire.Dxp.Automation.ClientJobSender.exe http://SERVER/spotfireautomation/JobExecutor.asmx c:\AutoBatch\backup\Trigger_Test.xml
:EX

但我不认为这是非常正确的,ROWCOUNT似乎不会起作用,而且,我将如何在CMD之后执行另一个SQLPlus命令。

我是BAT文件和SQLPlus的新手

2 个答案:

答案 0 :(得分:2)

帮自己一个忙,学习Powershell或python。我曾经构建疯狂的批处理文件来自动化流程,但是很痛苦。

Powershell在构建此类条件脚本方面要好得多,特别是能够创建和监视子线程。 Python也很棒。

这些带来的巨大优势是您可以访问数据库(以及脚本中的返回结果,以处理和响应数据。

缺点(?)是你需要学习其中一种语言,但我个人并不认为这些是缺点,因为它们比批处理文件要好得多。

否则,foxidrive的答案应该适合你。


Powershell示例(有点长,但代码可以重用):

function CreateConnection 
{
    param([string]$databaseHost 
      ,[string]$Port
      ,[string]$SID
      ,[string]$UserID
      ,[string]$Password
     )      
    process
    {   
    $ConnectString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))(CONNECT_DATA=(SID={2})));User Id={3};Password={4};"`
    -f $databaseHost,$port,$SID,$UserID,$Password
    write-host $connectString
    sleep 10
    $connection = new-object system.data.oracleclient.oracleconnection( $ConnectString)             
    return $connection
    }
}   

$DBMSHost="somehost.somedomain.com"
$DBMSPort=1521
$SID="somesid"
$UserName="user"
$Password="password"

[System.Reflection.Assembly]::LoadWithPartialName('System.Data.OracleClient') | out-null

try {
        $Connection = CreateConnection $DBMSHost $DBMSPort $SID $UserName $Password
     }
catch
    {
        write-host ("Could not access Oracle database {0}"  -f $_.Exception.ToString())
        exit
        }           

    $Query = "SELECT COUNT(*) as rowcount FROM dmsn.ds3r_1xrtt_voice_trigger"      // Added name to column to make life easier

    $data_set = new-object system.data.dataset
$adapter = new-object system.data.oracleclient.oracledataadapter ($Query, $Connection)
    [void] $adapter.Fill($data_set)
    $table = new-object system.data.datatable
    $table = $data_set.Tables[0]    // We now have the actual resukts data

    foreach ($row in $table)
    {
        if ($row.rowcount <1 )
        {
            $Application = "C:\Autobatch\Spotfire.Dxp.Automation.ClientJobSender.exe"
            $Arguments  = "http://SERVER/spotfireautomation/JobExecutor.asmx c:\AutoBatch\backup\Trigger_Test.xml"
            $CommandLine = "{0} {1}" -f $Application,$Arguments
            invoke-expression $CommandLine
        }   
    }       

答案 1 :(得分:0)

假设sqlplus命令返回一个简单的数字,那么这可能有效:

@echo off
for /f "delims=" %%a in ('sqlplus %USER%/%PASSWORD%@ORACLE @CheckRowCount.SQL') do set rowcount=%%a
echo.rowcount is set to "%ROWCOUNT%"
if %ROWCOUNT% GTR 0 (
c:\Autobatch\Spotfire.Dxp.Automation.ClientJobSender.exe http://SERVER/spotfireautomation/JobExecutor.asmx c:\AutoBatch\backup\Trigger_Test.xml
)
pause