oracle SQL*Plus 可以从它运行的机器上读取环境变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30107510/
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-19 02:51:11  来源:igfitidea点击:

Can SQL*Plus read environment variables from the machine is it running on?

oracleenvironment-variablessqlplus

提问by John O

I'm aware that the database engine itself is (often) on another machine and that SQL*Plus has no direct way of reading thoseenvironment variables, but I'm in a tricky situation where I merely need the environment variables from the machine the client itself is running on.

我知道数据库引擎本身(通常)在另一台机器上,并且 SQL*Plus 没有直接读取这些环境变量的方法,但我处于一个棘手的情况,我只需要来自机器的环境变量客户端本身正在运行。

Is there a way to cheat these values into the SQL*Plus client from within a single script that will be run in SQL*Plus? The script consists of a single begin/end PL/SQL block, but if I need to use SQL*Plus directives of the set/define/variable sort that shouldn't be a problem either.

有没有办法从将在 SQL*Plus 中运行的单个脚本中将这些值欺骗到 SQL*Plus 客户端?该脚本由单个开始/结束 PL/SQL 块组成,但是如果我需要使用 SQL*Plus 设置/定义/变量排序的指令,那也不应该是一个问题。

What I can't do is alter the way that the SQL*Plus executable itself is started (I don't have access to pass the values in as arguments).

我不能做的是改变 SQL*Plus 可执行文件本身的启动方式(我无权将值作为参数传递)。

Is there any way to accomplish this?

有什么办法可以做到这一点吗?

Note: dbms_system.get_env()seems to retrieve environment variables from the server itself, which is what I do not want.

注意:dbms_system.get_env()似乎从服务器本身检索环境变量,这是我不想要的。

采纳答案by Alex Poole

You can get a few client-related things from the USERENVcontext, but not arbitrary environment variables.

你可以从一些客户相关的东西USERENV情况下,但不是任意的环境变量。

If you can create a file on your local machine you could use the hostcommandto set a substitution variable based on an environment variable:

如果您可以在本地计算机上创建文件,则可以使用host命令根据环境变量设置替换变量:

SQL > host echo define homedir=$HOME > /tmp/gethome.sql

SQL > @/tmp/gethome.sql
SQL > host rm -f /tmp/gethome.sql

SQL > select '&homedir.' as home from dual;

HOME
------------
/home/apoole

1 row selected.

Not very pretty, but if you can't pass the variables on the command line as positional parameters then your options are rather limited.

不是很漂亮,但是如果您不能将命令行上的变量作为位置参数传递,那么您的选择就相当有限。

This is using a Unix-y paths and commands of course, but you can do the same sort of thing in Windows.

这当然是使用 Unix-y 路径和命令,但您可以在 Windows 中做同样的事情。

回答by Nicholas Sushkin

If you could pass the variable via sqlplus argument passing mechanism. you could do the following,

如果您可以通过sqlplus 参数传递机制传递变量。你可以做到以下几点

cat > myscript.sql <<!
select '&1' as HOME from DUAL;
!

sqlplus user/pwd@db @myscript.sql $HOME

回答by Lokesh

Sys_contextshould solve your problem. You can set custom environment variable in database using DBMS_SESSION.SET_CONTEXT ('MY_NAMESPACE', 'MY_PARAMETER', v_input_parameter);and then fetch it using SYS_CONTEXT ('MY_NAMESPACE', 'MY_PARAMETER');So you can run a initial pl/sql block to set variable in session and then use it as per requirement.

Sys_context应该可以解决您的问题。您可以使用在数据库中设置自定义环境变量,DBMS_SESSION.SET_CONTEXT ('MY_NAMESPACE', 'MY_PARAMETER', v_input_parameter);然后使用获取它SYS_CONTEXT ('MY_NAMESPACE', 'MY_PARAMETER');所以您可以运行初始 pl/sql 块以在会话中设置变量,然后根据需要使用它。

You can see an example here: http://blog.contractoracle.com/2010/03/using-dbmssessionsetcontext-to-store.html

你可以在这里看到一个例子:http: //blog.contractoracle.com/2010/03/using-dbmssessionsetcontext-to-store.html