在.ora文件中设置环境变量

时间:2018-02-07 00:41:46

标签: oracle environment-variables

是否可以在.ora文件中使用环境变量进行oracle连接? e.g:

my_connection= (DESCRIPTION=
    (ADDRESS=
      (PROTOCOL=TCP)
      (HOST=$MY_HOST)
      (PORT=$MY_PORT)
    )
    (CONNECT_DATA=
      (SERVER=dedicated)
      (SERVICE_NAME=$MY_SERVICE_NAME)
    )
  )

我尝试像上面那样设置它们但无法连接。我想知道是否还有其他方法可以让它发挥作用?

2 个答案:

答案 0 :(得分:0)

不,tnsnames.ora不能有env引用。这是一个静态文件。

一个选项是大多数细节都在视图中的数据库中,例如v $ instance,v $ LISTENER_NETWORK,UTL_INADDR.get_host_address,dba_services

举个例子,这里有一些sql生成我的本地XE tnsnames文件。

SQL> with q as ( select  UTL_INADDR.get_host_address ip, host_name from v$instance)
  2  select value || '= (DESCRIPTION= ' || a.addr 
  3*         || '(CONNECT_DATA= (SERVER=dedicated) (SERVICE_NAME='||value||')))' tnsnames
  4  from v$LISTENER_NETWORK,
  5       ( select replace(value,q.host_name,q.ip) addr 
  6           from v$LISTENER_NETWORK,q 
  7          where type='LOCAL LISTENER') a
  8  where type = 'SERVICE NAME';


SQL> /
TNSNAMES                                                                                                                      
xe= (DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=172.17.0.2)(PORT=1521))(CONNECT_DATA= (SERVER=dedicated) (SERVICE_NAME=xe)))   

答案 1 :(得分:0)

配置配置文件sqlnet.ora以便轻松连接。

NAMES.DIRECTORY_PATH= (TNSNAMES,ezconnect)   

创建测试脚本test_echo.sh

 #!/bin/sh

username=\"Scott\"
password=\"@T!ger\"
#ezconnect=10.89.251.205:1521/esmd
host=10.89.251.205
port=1521
service=esmd
ezconnect=$host:$port/$service

echo username:  $username
echo password:  $password
echo host:      $host
echo port:      $port
echo service:   $service
echo ezconnect  $ezconnect

echo -e 'show user \n  select 1 from dual;\nexit;' |  sqlplus -s  $username/$password@$ezconnect

oracle@esmd:~> ./test_echo.sh
username: "Scott"
password: "@T!ger"
host: 10.89.251.205
port: 1521
service: esmd
ezconnect 10.89.251.205:1521/esmd
USER is "Scott"

         1
----------
         1