Oracle stored procedures, export SQL to text, dynamic SQL, dbms_ SQL use

Source: Internet
Author: User
  1. Create or replace package sqlexp_pack is
  2. -- Author: yanlei
  3. -- Created: 11:25:09
  4. -- Purpose: Oracle Export
  5. -- SQL export text string
  6. Function sqltotext (sqlstr in varchar2) return long;
  7. -- SQL Export File
  8. Function sqltofile (sqlstr in varchar2, filename in varchar2)
  9. Return varchar2;
  10. End sqlexp_pack;
  11. /
  12. Create or replace package body sqlexp_pack is
  13. -- SQL export text string
  14. Function sqltotext (sqlstr in varchar2) return long is
  15. Colname varchar2 (4000 );
  16. Rownum integer;
  17. Colnum number default 0; -- number of fields
  18. Linestr varchar2 (20000 );
  19. Yltbl dbms_ SQL .desc_tab; -- table information
  20. Ylcursor integer default dbms_ SQL .open_cursor; -- SQL output cursor
  21. Rstr long;
  22. Begin
  23. Dbms_ SQL .parse (ylcursor, sqlstr, dbms_ SQL .native); -- parse dynamic SQL statements;
  24. Dbms_ SQL .describe_columns (ylcursor, colnum, yltbl); -- get the metadata of dynamic SQL
  25. For I in 1 .. colnum Loop
  26. Dbms_ SQL .define_column (ylcursor, I, colname, 4000); -- defines dynamic SQL Columns
  27. End loop;
  28. Rownum: = dbms_ SQL .execute (ylcursor); -- execute dynamic SQL statements. Return Value. For non-query statements, execute executes the statement and returns the number of rows processed. For a query, execute returns the result of determining the activity set. The returned value is 0.
  29. If rownum <> 0 then
  30. Return '';
  31. End if;
  32. Rstr: = '';
  33. While (dbms_ SQL .fetch_rows (ylcursor)> 0) loop
  34. Linestr: = '';
  35. For I in 1 .. colnum Loop
  36. Dbms_ SQL .column_value (ylcursor, I, colname); -- Obtain the field value
  37. Linestr: = linestr | colname;
  38. If I <colnum then
  39. Linestr: = linestr | ',';
  40. End if;
  41. End loop;
  42. Rstr: = rstr | linestr | CHR (10 );
  43. End loop;
  44. Dbms_ SQL .close_cursor (ylcursor );
  45. Return rstr;
  46. Exception
  47. When others then
  48. Dbms_ SQL .close_cursor (ylcursor );
  49. Raise;
  50. End sqltotext;
  51. -- SQL Export File
  52. Function sqltofile (sqlstr in varchar2, filename in varchar2)
  53. Return varchar2 is
  54. STR long;
  55. Begin
  56. STR: = sqltotext (sqlstr );
  57. Ylfileop_pack.writefile (filename, STR );
  58. Return 'successfully ';
  59. End;
  60. End sqlexp_pack;
  61. /
  62. --- Reprinted copyright by yanleigis Email: landgis@126.com

 

Related Article

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.