Read and Write files in Oracle stored procedures

Source: Internet
Author: User

Reading and Writing files in Oracle stored procedures is an important means to achieve interaction between files and database tables. The following describes the knowledge of reading and writing files in Oracle stored procedures in detail and hopes to help you.

Sometimes we need to use programs between files and database tables to achieve interaction between the two. Here we can use the UTL_FILE package to implement I/O operations on files. the following describes the file write tables and table data write files respectively.

[1] export table information to a file

We recommend that you create a folder/home/zxin10/file on SUSE, and then authorize its chmod g + w file (otherwise, the file cannot be exported ), then, the path (/home/zxin10/file) You specified is directed to the system table sys. dir $ is registered (otherwise, information cannot be exported to the file). After the operation, you can query sys. dir $ you can see that the OS _PATH In the table contains the path you specified.

Registration Method: Execute the SQL statement create or replace directory BBB as '/home/zxin10/file';

The storage process is as follows: (when writing a file, you do not need to create the file name first, and the specified file will be automatically created in the Program)

 
 
  1. Create or replace procedure V3_SUB_FETCH_TEST_2
  2. (
  3. V_TEMP VARCHAR2,
  4. -- 1 indicates success, and 0 indicates failure.
  5. V_retvalue OUT NUMBER
  6. )
  7. AS
  8. -- Cursor Definition
  9. Type ref_cursor_type is ref cursor;
  10. Cursor_select ref_cursor_type;
  11. Select_cname varchar2 (1000 );
  12. V_file_handle utl_file.file_type;
  13. V_ SQL varchar2 (1000 );
  14. V_filepath Varchar2 (500 );
  15. V_filename Varchar2 (500 );
  16. -- Buffer zone
  17. V_results Varchar2 (500 );
  18. V_pid varchar2 (1000 );
  19. V_cpcnshortname Varchar2 (500 );
  20. Begin
  21. V_filepath: = V_TEMP;
  22. If v_filepath is null then
  23. V_filepath: = '/home/zxin10/file3 ';
  24. End if;
  25. V_filename: = 'free _ '| substr (to_char (sysdate, 'yyyymmddhh24mi'),) | '. all ';
  26. -- Cursor start
  27. Select_cname: = 'select cpid, cpcnshortname from zxdbm_ismp.scp_basic ';
  28. -- Open a file handle, and the first parameter of fopen must be in uppercase.
  29. V_file_handle: = utl_file.fopen ('bbb ', v_filename, 'A ');
  30. Open cursor_select For select_cname;
  31. Fetch cursor_select into v_pid, v_cpcnshortname;
  32. While cursor_select % Found
  33. Loop
  34. V_results: = v_pid | '| v_cpcnshortname;
  35. -- Write v_results to a file
  36. Utl_file.put_line (v_file_handle, v_results );
  37. Fetch cursor_select into v_pid, v_cpcnshortname;
  38. End Loop;
  39. Close cursor_select; -- Close the cursor
  40. Utl_file.fClose (v_file_handle); -- close the handle
  41. V_retvalue: = 1;
  42. Exception when others then
  43. V_retvalue: = 0;
  44. End V3_SUB_FETCH_TEST_2;

[2] import file information to the table

As above, chmod is performed on the specified file path first, and then you want to register the path for sys. dir $ of Oracle.

The file zte. apsuic is located in/home/zxin10/file, and its data format is:
1 | 22 | cheng
2 | 33 | zhou
3 | 44 | heng
4 | 55 | yaya

Table LOADDATA script:

 
 
  1. -- Create table  
  2. create table LOADDATA  
  3. (  
  4.   ID   VARCHAR2(50),  
  5.   AGE  VARCHAR2(50),  
  6.   NAME VARCHAR2(50)  
  7. )  
  8.     / 

The program is as follows: (when reading a file, the specified file name must exist in advance; otherwise, the program will fail)

 
 
  1. Create or replace directory BBB as '/home/zxin10/file ';
  2. /
  3. -- The function is to register the specific file path information in Oracle (the registration information is stored in the sys. dir $ table)
  4.  
  5. Create or replace procedure V3_SUB_FETCH_TEST_3
  6. (
  7. -- Import the information in the file to the table
  8. V_TEMP VARCHAR2,
  9. V_retvalue out number -- 1 successful, 0 failed
  10. AS
  11. V_file_handle utl_file.file_type;
  12. V_ SQL varchar2 (1000 );
  13. V_filepath Varchar2 (500 );
  14. V_filename Varchar2 (500 );
  15. -- File-to-table field ing
  16. V_id varchar2 (1000 );
  17. V_age varchar2 (1000 );
  18. V_name varchar2 (1000 );
  19. -- Buffer zone
  20. V_str varchar2 (1000 );
  21. -- Column pointer
  22. V_ I number;
  23. -- String location parsing pointer
  24. V_sposition1 number;
  25. V_sposition2 number;
  26. Begin
  27. V_filepath: = V_TEMP;
  28. If v_filepath is null then
  29. V_filepath: = '/home/zxin10/file ';
  30. End if;
  31. V_filename: = 'zte. apsuic ';
  32. -- V_ SQL: = 'create or replace directory CCC as ''' | v_filepath | '''';
  33. -- Execute immediate v_ SQL;
  34. V_file_handle: = utl_file.fopen ('ccc ', v_filename, 'R ');
  35. Loop
  36. -- Read the file information to the buffer v_str and read a row each time
  37. Utl_file.get_line (v_file_handle, v_str );
  38. -- Dbms_output.put_line (v_str );
  39. -- For the number of columns in each row
  40. V_ I: = 1;
  41. -- Move the pointer to the string each time
  42. V_sposition1: = 1;
  43. -- There are three columns of information in each row in the file, which are repeated three times.
  44. For I IN 1 .. 3 loop
  45. -- When instr (v_str, '|', 6) where v_str is 1 | 22 | wuzhuocheng, it returns 0
  46. V_sposition2: = instr (v_str, '|', v_sposition1 );
  47. -- String Parsing is normal
  48. If v_sposition2 <> 0 then
  49. If v_ I = 1 then
  50. V_id: = substr (v_str, v_sposition1, v_sposition2-v_sposition1); -- First Column
  51. Elsif v_ I = 2 then
  52. V_age: = substr (v_str, v_sposition1, v_sposition2-v_sposition1); -- Second Column
  53. Elsif v_ I = 3 then
  54. V_name: = substr (v_str, v_sposition1, v_sposition2-v_sposition1); -- the third column
  55. Else
  56. Return;
  57. End if;
  58. -- String parsing exception
  59. Else
  60. If v_ I = 1 then
  61. V_id: = substr (v_str, v_sposition1); -- First Column
  62. Elsif v_ I = 2 then
  63. V_age: = substr (v_str, v_sposition1); -- Second Column
  64. Elsif v_ I = 3 then
  65. V_name: = substr (v_str, v_sposition1); -- the third column
  66. Else
  67. Return;
  68. End if;
  69. End if;
  70. V_sposition1: = v_sposition2 + 1;
  71. V_ I: = v_ I + 1;
  72. End loop;
  73. -- Insert information into the table after each column is cyclically completed
  74. Insert into zxdbm_ismp.loaddata values (v_id, v_age, v_name );
  75. End Loop;
  76. -- Close the handle
  77. Utl_file.fClose (v_file_handle );
  78. V_retvalue: = 1;
  79. Exception when others then
  80. V_retvalue: = 0;
  81. End V3_SUB_FETCH_TEST_3;

Oracle read/write file bfilename instance

Oracle XE built-in database creation process

Entire Process of creating Oracle Materialized View

ORACLE instance creation process

Statement syntax for oracle time addition and subtraction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.