问题
I am trying to write an sql script in toad for oracle version 9.5 that when run will allow the user to select data from a txt file from the c drive where the parameters / variables are entered. The version of oracle is 10g
回答1:
Sounds like an Oracle external directory is perfect for the job. Keep in mind this approach is fraught with difficulties if this is a text file that users can write to. Users will do a hundred things you never believed possible that will cause errors and more.
From the article:
Create a directory
grant read, write on directory data_dir to your_user;
Create an external table
CREATE TABLE test_ext (
test_code VARCHAR2(5),
test_name VARCHAR2(50)
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tab_data
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
(
test_code CHAR(5),
test_name CHAR(50)
)
)
LOCATION ('test1.txt','test2.txt')
)
PARALLEL 5
REJECT LIMIT UNLIMITED;
and then read from the table
SELECT *
FROM test_ext
ORDER BY test_name;
Edit: you can still do this with directories that are not located on the database server but more work is required and this represents even more of a risk to database security and data quality. This approach does not scale either. Do you intend to add a new directory every time a new user is added?
Steps to allow Oracle to access files located on another machine (assuming Windows operating system)
- create a windows or domain user who will have read file permissions on every directory that you wish to access
- on the database server run services.msc and change the user that the Oracle database service runs on to the your new domain user. Add this user to the local group called ORA_DBA on the database server
- restart the database for the changes to take effect
- create a directory in the database using a path like //clientPc/sharedFolder
- grant read permissions to your database user
- on the client machine grant read to the domain user on that folder
- verify connectivity by using UTLFILE to read a sample file on the client pc
来源:https://stackoverflow.com/questions/29186006/script-that-pulls-data-from-a-txt-file-in-the-where-clause