Pass Parameters from a batch file to sqlplus script

时光总嘲笑我的痴心妄想 提交于 2019-12-08 00:15:01

问题


I am trying to get one file with my user name and passwords for some scripts that I run every day. I have several scripts working now using a batch file that has my user names and passwords.

My Password Batch file looks like this. parms.bat

Rem Oracle Db
set odbUsername=myUserName
set odbpassword=myPassword

My other scripts call that batch file and get the information ok. I have a couple sql scripts that I also need to use these usernames and passwords. I can't seem to get them to work. But I am fairly new to sqlplus scripting.

My Sql Scripts looks like this.

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;

I call this script with a batch file that runs through windows task scheduler.

It looks like this.

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

So I would like to pass the user name to the sql from the batch file. I have read several things and tried about 30 and none seem to work. Thank you for your help in advance.


回答1:


You can pass a parameter this way:

script.sql:

select '&1' from dual;

the call:

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:


Just typing that out made me think about passing it during the call vs inside the sql to connect. I then edited my SQL and took out the connect statement

So this worked...

echo Get Parameters
call C:\FileTransfers\AutomationScripts\parms.bat
echo %odbUsername%
echo %odbpassword%

sqlplus myusername@oracledb/%odbpassword%@C:\FileTransfers\AutomationScripts\GoodSence\SpoolGoodSenceDataSet.sql 


来源:https://stackoverflow.com/questions/39834548/pass-parameters-from-a-batch-file-to-sqlplus-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!