将参数从批处理文件传递到 Oracle SQL 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6140584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:47:37  来源:igfitidea点击:

Passing parameters to Oracle SQL file from Batch file

sqloraclebatch-file

提问by advantej

I have a SQL file that updates certain table. However, the table name depend on the machine name on which the software is installed. (Something like: TableFooBar-MyMachine). I have to write a batch file that calls an Oracle SQL script which will update this table.

我有一个更新某些表的 SQL 文件。但是,表名取决于安装软件的机器名。(类似于:TableFooBar-MyMachine)。我必须编写一个批处理文件来调用将更新此表的 Oracle SQL 脚本。

So, BatchFile --> Update. Sql --> TableFooBar-MyMachine

因此,批处理文件 --> 更新。Sql --> TableFooBar-MyMachine

The Update. Sql will have statement like:

更新。Sql 将有如下语句:

Update TableFooBar-<Machine-Name> where fooid = -99;

This batch file needs to run on many machines. There are actually many update statements on such tables. I do not want people to edit the sql files. Instead if I set the machine name in the batch file and pass it to the sql, I'm pretty much done! How do I pass the machine name parameter to the .sql file to achieve this?

这个批处理文件需要在多台机器上运行。这些表上实际上有很多更新语句。我不希望人们编辑 sql 文件。相反,如果我在批处理文件中设置机器名称并将其传递给 sql,我就大功告成了!如何将机器名称参数传递给 .sql 文件以实现此目的?

回答by schurik

you can use substitution variables

您可以使用替换变量

update.sql

更新.sql

--
Update TableFooBar-&1 set column_foo='bar' where fooid = -99;
--

and then call

然后打电话

sqlplus foo/bar@db @update.sql <Machine-Name>

回答by Daniel Hilgarth

Yes, you can do this, by creating the SQL file from the BATCH file.

是的,您可以通过从 BATCH 文件创建 SQL 文件来做到这一点。

It would look like this:

它看起来像这样:

@echo off
set SQL = update.sql
ECHO connect username/password@database
ECHO Prompt Updating tables > %SQL%
ECHO Update TableFooBar-%1 where fooid = -99; >> %SQL%

sqlplus @update.sql