Implementation of outputting ORACLE database records to text files

Source: Internet
Author: User

How can ORACLE database records be output to text files? The following describes how to output an ORACLE database record to a text file. It is helpful for you to learn about ORACLE database records.

In some cases, for example, to exchange data with other databases, we need to output the data in the ORACLE database, save it to an intermediate file, and then process it. So how to output data in the ORACLE database?

Assume that the data is text-type data characters, numbers, and dates. We have at least two ways to output the data. One is to use the tabulation function to output all the data in the form of tables, save the table to a text file. Another method is simpler: Use the UTL_FILE package to directly input data to a text file.

Before using the UTL_FILE package, you must define the writable directory of UTL_FILE in the ORACLE database initialization file (initsid. ora), for example

 
 
  1. UTL_FILE=/eXPort/home/oracle/output 

In addition, it is important to note that the maximum size of a field in the output record is 1023 bytes. If a field exceeds this limit, it must be truncated into several parts, as shown in the following example.
Here is an example. You can copy it first, and then use it after modification based on the actual situation of your database.

 
 
  1. DECLARE
  2. OutputFile UTL_FILE.FILE_TYPE;
  3. Vart_title perd11.ART _ TITLE % TYPE;
  4. Vart_author perd11.ART _ AUTHOR % TYPE;
  5. Vart_keyWord perd11.ART _ KEYWORD % TYPE;
  6. Vart_abstract perd11.ART _ ABSTRACT % TYPE;
  7. Vart_abs1 perd11.ART _ ABSTRACT % TYPE;
  8. Vart_abs2 perd11.ART _ ABSTRACT % TYPE;
  9. Vart_authorinstitute perd11.ART _ AUTHORINSTITUTE % TYPE;
  10. Vperd_volume perd11.PERD _ VOLUME % TYPE;
  11. Vperd_issue perd11.PERD _ ISSUE % TYPE;
  12. Vperd_date perd11.PERD _ DATE % TYPE;
  13. Vpub_name perd11.PUB _ NAME % TYPE;
  14. Vpub_issn perd11.PUB _ ISSN % TYPE;
  15. Vclass_name perd11.CLASS _ NAME % TYPE;
  16.  
  17. CURSOR cperd11 IS
  18. Select * from perd11;
  19.  
  20. BEGIN
  21. OutputFile: = UTL_FILE.FOPEN
  22. ('/Export/home/oracle/output', 'perd11. out', 'A ');
  23. Open cperd11;
  24. LOOP
  25. Fetch cperd11 into vart_no, vart_title,
  26. Vart_author, vart_keyword, vart_abstract,
  27. Vart_authorinstitute, vperd_volume,
  28. Vperd_issue, vperd_date, vpub_name,
  29. Vpub_issn, vclass_name;
  30. IF length (vart_abstract)> 1000 THEN
  31. Vart_abs1: = substr (vart_abstract, 0,1000 );
  32. Vart_abs2: = substr (vart_abstract, 1001 );
  33. ELSE
  34. Vart_abs1: = vart_abstract;
  35. Vart_abs2: = ";
  36. End if;
  37. UTL_FILE.PUTF (OutputFile ,'
  38. Journal category] \ n % s \ n', vclass_name );
  39. UTL_FILE.PUTF (OutputFile ,'
  40. Journal publication number] \ n % s \ n', vpub_issn );
  41. UTL_FILE.PUTF (OutputFile ,'
  42. Journal name] \ n % s \ n', vpub_name );
  43. UTL_FILE.PUTF (OutputFile ,'
  44. Journal volume number] \ n % s \ n', vperd_volume );
  45. UTL_FILE.PUTF (OutputFile ,'
  46. Journal id] \ n % s \ n', vperd_issue );
  47. UTL_FILE.PUTF (OutputFile ,'
  48. Publication date] \ n % s \ n', vperd_date );
  49. UTL_FILE.PUTF (OutputFile ,'
  50. Chinese title: \ n % s \ n', vart_chntitle );
  51. UTL_FILE.PUTF (OutputFile ,'
  52. 【Author】 \ n % s \ n', vart_chnauthor );
  53.  
  54. UTL_FILE.PUTF (OutputFile ,'
  55. Author unit: \ n % s \ n', vart_authorinstitute );
  56. UTL_FILE.PUTF (OutputFile ,'
  57. Chinese keywords: \ n % s \ n', vart_chnkeyword );
  58. UTL_FILE.PUTF (OutputFile ,'
  59. Chinese Abstract: \ n % s \ n', vart_engabs1 );
  60. UTL_FILE.PUTF (OutputFile ,'
  61. Chinese digest 2] \ n % s \ n', vart_engabs2 );
  62. UTL_FILE.FFLUSH (OutputFile );
  63. Exit when cperd11 % NOTFOUND;
  64. End loop;
  65. Close cperd11;
  66.  
  67. END;
  68.  
  69. -

Implementation of Oracle's fixed number of records

Explain how to modify the table structure in Oracle

The section of the Oracle database

Oracle STARTUP script in Linux

Summary of Oracle Database startup and shutdown Methods

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.