2

I need to create an oracle directory in oracle installation folder. In my case this path is D:\app\Administrator\product\11.2.0\dbhome_1. I tried create directory ora_dir as '\', but this make reference to D:\. Is there any way to create the directory pointing to oracle_home?

Regards.

Aramillo
  • 3,176
  • 3
  • 24
  • 49
  • I'm not sure that information is easily available within the database - it's kind of irrelevant to the running processes, and especially to clients. How will you be creating the directory - by running an SQL\*Plus script on the server, say? If so you could pas the environment variable as a positional parameter, perhaps? – Alex Poole Feb 25 '15 at 16:46
  • Hi, @AlexPoole. I need to create the directory in a valid path(inside oracle instalation folder, dbhome) from a procedure using this line `execute immediate q'[CREATE or replace DIRECTORY DIRECTORY_DIR AS 'ORACLE_HOME']';`. This procedure can be deployed in any database, so, the goal is that directory take the oracle home path of that database – Aramillo Feb 25 '15 at 17:11

2 Answers2

4

If you're already doing this dynamically from a procedure, you could get the ORACLE_HOME environment variable (as it was set when the instance started anyway) via a Java call, but you need a separate function:

create or replace function getenv(name varchar2) return varchar2
as language java
name 'java.lang.System.getenv(java.lang.String) return java.lang.String';
/

create or replace procedure p42 as
begin
  execute immediate q'[CREATE or replace DIRECTORY DIRECTORY_DIR AS ']'
    || getenv('ORACLE_HOME') || q'[']';
end;
/

Assuming both compile OK, when executed the directory is created with the path from the environment variable:

exec p42;

PL/SQL procedure successfully completed.

select directory_path
from all_directories
where directory_name = 'DIRECTORY_DIR';

DIRECTORY_PATH
----------------------------------------
/dboracle/orabase/product/11.2.0

If your procedure is in a package, the function can be in there too, privately if you prefer. And you don't need any additional privileges to use the Java call, just create procedure, which you already have in this case.

Unfortunately this doesn't work in Windows as ORACLE_HOME is set in the registry, not the environment; it may be possible to get information from the registry but you may also have more than one Oracle Home so you'd have to determine which registry key to use.

There is also (as Aramillo found) an [undocumented] built-in DBMS_SYSTEM package that can provide the same information; with that you could do:

create or replace procedure p42 as
  l_oracle_home varchar2(100);
begin
  dbms_system.get_env('ORACLE_HOME', l_oracle_home);
  execute immediate q'[CREATE or replace DIRECTORY DIRECTORY_DIR AS ']'
    || l_oracle_home || q'[']';
end;
/

But you would need to have execute permission granted on that package, and it's undocumented status might give you pause; some of the functionality seems to be restricted to SYSDBA anyway (note 159968.1).

While poking around My Oracle Support looking for DBMS_SYSTEM references, I also stumbled over this option:

select nvl(substr(file_spec, 1, instr(file_spec, 'lib') -2),
  substr(file_spec, 1, instr(lower(file_spec), 'bin') -2)) as oracle_home
from dba_libraries
where library_name='DBMS_SUMADV_LIB';

... which apparently also works on Windows, but you need sufficient privileges to see the data dictionary entries.

Community
  • 1
  • 1
Alex Poole
  • 183,384
  • 11
  • 179
  • 318
  • Hi, alex. This could be a solution, but give me null directory path for DIRECTORY_DIR, and as you said this should work as well in windows. (`select getenv('ORACLE_HOME') from dual;` -- this is returning null) – Aramillo Feb 25 '15 at 18:32
  • 1
    @Aramillo - maybe the environment variable isn't set when Oracle starts as a service? I don't know... sorry, somehow I've never run Oracle on Windows. – Alex Poole Feb 25 '15 at 18:35
  • 1
    Problem is i don't set ORACLE_HOME environment variable in my system, so, looks like in windows this is not necessary, not as well in linux, where seems to be a requirement. Thanks anyway :) – Aramillo Feb 25 '15 at 18:44
  • 1
    @Aramillo - I guess it's [set in the registry, not the environment](http://docs.oracle.com/cd/E11882_01/install.112/e47798/intro.htm#NTDBI2641), and accessing that [looks tricky](http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java); plus you could have multiple homes defined so I don't know how you'd know which is correct, perhaps. Not something I'm able to play with, sorry. It was a nice idea... – Alex Poole Feb 25 '15 at 18:45
  • Yes, that's could be possible, if you had multiple homes you wouldn't know which you want, now i'm little confusing ggg. I'll keep looking to see what i find – Aramillo Feb 25 '15 at 18:54
  • 1
    Hi Alex, please take a look at this procedure dbms_system.get_env [here](http://psoug.org/reference/dbms_system.html). Seems to be other solution. Thanks – Aramillo Feb 25 '15 at 19:44
  • @Aramillo - nice, and much simpler, as long as you have permission to execute it. (The Java one only needs `create procedure`, which you already have). You should probably add that as a new answer, and I might then remove this one. Does that work for the Windows registry settings too? – Alex Poole Feb 25 '15 at 19:48
  • Please if you can add this to your post, will be better, and the asnwer will be complete. – Aramillo Feb 25 '15 at 19:49
  • And, i don't know if this will works if there are oracle_home environment variables defined, in my case don't give troubles because i don't have one. – Aramillo Feb 25 '15 at 20:09
  • @Aramillo - OK, I've added that, and another method I found while investigating that. Thanks. Can't help thinking you deserve most of the credit for this though... *8-) – Alex Poole Feb 25 '15 at 20:13
  • That query works great to, i think that together we got a better answer. Thanks for all. :) – Aramillo Feb 25 '15 at 20:19
0

You need to provide the full path:

CREATE DIRECTORY ORA_DIR AS 'D:\app\Administrator\product\11.2.0\dbhome_1';
tilley31
  • 668
  • 13
  • 19
  • Yes, @tilley31, this works but i need this to be generic,if oracle were installed in C partition or by Admin user instead of Administrator the path will be like `C:\app\Admin\product\11.2.0\dbhome_1` – Aramillo Feb 25 '15 at 16:28
  • I don't think it's possible with SQL, you would have to use a shell. I see you're in Windows, so I can't help with that. – tilley31 Feb 25 '15 at 16:51