【问题标题】:Pass Parameters from a batch file to sqlplus script将参数从批处理文件传递到 sqlplus 脚本
【发布时间】:2017-02-11 13:43:23
【问题描述】:

我正在尝试为我每天运行的一些脚本获取一个包含我的用户名和密码的文件。我现在有几个脚本正在使用包含我的用户名和密码的批处理文件。

我的密码批处理文件如下所示。 parms.bat

Rem Oracle Db
set odbUsername=myUserName
set odbpassword=myPassword

我的其他脚本调用该批处理文件并获取信息正常。我有几个 sql 脚本,我还需要使用这些用户名和密码。我似乎无法让他们工作。但我对 sqlplus 脚本相当陌生。

我的 Sql 脚本看起来像这样。

CONNECT myusername@Oracleddbname/mypassword

SET FEEDBACK OFF;
SET ECHO OFF;
SET TERMOUT OFF;
SET HEADING OFF;
SET PAGESIZE 0;
SET LINESIZE 500;
SET TIMING OFF;
SET TRIMSPOOL ON;
SET COLSEP ',';

SPOOL C:\FileTransfers\MeterData.csv

PROMPT CoopCode,MeterID,DateTime,Value
SELECT DISTINCT
          a.coopcode
       || ','
       || a.meterno
       || ','
       || a.readdatetime
       || ','
       || a.usage
  FROM temp_reconfigured a, temp_goodsence b
 WHERE a.coopcode = b.coopcode AND a.meterno = b.meterno
;

Spool off;

EXIT;

我使用通过 Windows 任务调度程序运行的批处理文件调用此脚本。

看起来像这样。

sqlplus /nolog        @C:\FileTransfers\AutomationScripts\GoodSence\SpoolGoodSenceDataSet.sql

所以我想将用户名从批处理文件传递给 sql。我已经阅读了几篇文章并尝试了大约 30 篇,但似乎都没有奏效。提前感谢您的帮助。

【问题讨论】:

    标签: sql oracle batch-file sqlplus


    【解决方案1】:

    你可以这样传递参数:

    script.sql:

    select '&1' from dual;
    

    来电:

    D:\>sqlplus user/password@db @d:\script.sql 'value'
    
    SQL*Plus: Release 11.2.0.2.0 Production on Lun Ott 3 17:02:10 2016
    
    Copyright (c) 1982, 2014, Oracle.  All rights reserved.
    
    
    Connected to:
    Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
    
    old   1: select '&1' from dual
    new   1: select 'value' from dual
    
    'VALU
    -----
    value
    

    【讨论】:

      【解决方案2】:

      只是输入它让我想到在调用期间传递它与在 sql 内部进行连接。然后我编辑了我的 SQL 并取出了连接语句

      所以这行得通...

      echo Get Parameters
      call C:\FileTransfers\AutomationScripts\parms.bat
      echo %odbUsername%
      echo %odbpassword%
      
      sqlplus myusername@oracledb/%odbpassword%@C:\FileTransfers\AutomationScripts\GoodSence\SpoolGoodSenceDataSet.sql 
      

      【讨论】:

        猜你喜欢
        • 2015-08-18
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多