DBMS_SQL系统包的使用

转自:http://taosst.javaeye.com/blog/284770

  1. PL/SQL中使用动态SQL编程
  2. 在PL/SQL程序设计过程中,会遇到很多必须使用动态sql的地方,oracle系统所提供的DMBS_SQL包可以帮助你解决问题。
  3. (一)介绍
  4. DBMS_SQL 系统包提供了很多函数及过程,现在简要阐述其中使用频率较高的几种:
  5. function open_cursor:打开一个动态游标,并返回一个整型;
  6. procedure close_cursor(c in out integer );关闭一个动态游标,参数为open_cursor所打开的游标;
  7. procedure parse(c in integer ,statement in varchar2,language_flag in integer ):对动态游标所提供的sql语句进行解析,参数C表示游标,statement为sql语句,language-flag为解析sql语句所用oracle版本,一般有V6,V7跟native(在不明白所连 database 版本时,使用native);
  8. procedure define_column(c in integer ,position in integer , column any datatype,[column_size in integer ]):定义动态游标所能得到的对应值,其中c为动态游标,positon为对应动态sql中的位置(从1开始), column 为该值所对应的变量,可以为任何类型,column_size只有在 column 为定义长度的类型中使用如VARCHAR2, CHAR 等(该过程有很多种情况,此处只对一般使用到的类型进行表述);
  9. function execute (c in integer ):执行游标,并返回处理一个整型,1表示成功,0表示失败,代表处理结果(对 insert , delete , update 才有意义,而对 select 语句而言可以忽略);
  10. function fetch_rows(c in integer ):对游标进行循环取数据,并返回一个整数,为0时表示已经取到游标末端;
  11. procedure column_value(c in integer ,position in integer ,value):将所取得的游标数据赋值到相应的变量,c为游标,position为位置,value则为对应的变量;
  12. procedure bind_variable(c in integer , name in varchar2,value):定义动态sql语句(DML)中所对应字段的值,c为游标, name 为字段名称,value为字段的值;
  13. 以上是在程序中经常使用到的几个函数及过程,其他函数及过程请参照oracle所提供定义语句dbmssql.sql
  14. (二)一般过程
  15. 对于一般的select 操作,如果使用动态的sql语句则需要进行以下几个步骤:
  16. open cursor --->parse--->definecolumn--->excute--->fetchrows--->closecursor;
  17. 而对于dml操作(insert , update )则需要进行以下几个步骤:
  18. open cursor --->parse--->bindvariable--->execute--->closecursor;
  19. 对于delete 操作只需要进行以下几个步骤:
  20. open cursor --->parse--->execute--->closecursor;
  21. (三)实例应用
  22. 1.declare
  23. v_cidinteger ;
  24. v_updatestrvarchar2(100);
  25. v_rowupdatedinteger ;
  26. begin
  27. v_cid:=dbms_sql .open_cursor;
  28. v_updatestr:='updateempsetcomm=400whereempno=7499' ;
  29. dbms_sql .parse(v_cid,v_updatestr,dbms_sql .native);
  30. v_rowupdated:=dbms_sql .execute (v_cid);
  31. dbms_sql .close_cursor(v_cid);
  32. exception
  33. when others then
  34. dbms_sql .close_cursor(v_cid);
  35. raise;
  36. end ;
  37. 2.create or replace function updatecomm(p_commemp.comm%type,p_empnoemp.empno%type
  38. return integer as
  39. v_cidinteger ;
  40. v_updatestrvarchar2(100);
  41. v_rowupdatedinteger ;
  42. begin
  43. v_cid:=dbms_sql .open_cursor;
  44. v_updatestr:='updateempsetcomm=:commwhereempno=:empno' ;
  45. dbms_sql .parse(v_cid,v_updatestr,dbms_sql .native);
  46. dbms_sql .bind_variable(v_cid,'comm' , 'p_comm' );
  47. dbms_sql .bind_variable(v_cid,'empno' , 'p_empno' );
  48. v_rowupdated:=dbms_sql .execute (v_cid);
  49. dbms_sql .close_cursor(v_cid);
  50. return p_rowsupdated;
  51. exception
  52. when others then
  53. dbms_sql .close_cursor(v_cid);
  54. raise;
  55. end ;
  56. 调用--
  57. declare
  58. ainteger ;
  59. begin
  60. a:=updatecomm(5000,a);
  61. dbms_output.put_line(a);
  62. end ;
  63. 3.create or replace procedure dynamiccopy(p_deptno1emp.deptno%type default null ,p_deptno2emp.deptno%type default null )
  64. as
  65. v_cidinteger ;
  66. v_selectvarchar2(100);
  67. v_empnochar (4);
  68. v_enamevarchar2(10);
  69. v_deptnochar (2);
  70. v_dummyinteger ;
  71. begin
  72. v_cid:=dbms_sql .open_cursor;
  73. v_select:='selectempno,ename,deptnofromempwheredeptnoin(:d1,:d2)' ;
  74. dbms_sql .parse(v_cid,v_select,dbms_sql .native);
  75. dbms_sql .bind_variable(v_cid,'d1' ,p_deptno1);
  76. dbms_sql .bind_variable(v_cid,'d2' ,p_deptno2);
  77. dbms_sql .define_column(v_cid,1,v_empno,4);
  78. dbms_sql .define_column(v_cid,2,v_ename,10);
  79. dbms_sql .define_column(v_cid,3,v_deptno,2);
  80. v_dummy:=dbms_sql .execute (v_cid);
  81. loop
  82. ifdbms_sql .fetch_rows(v_cid)=0then
  83. exit;
  84. end if;
  85. dbms_sql .column_value(v_cid,1,v_empno);
  86. dbms_sql .column_value(v_cid,2,v_ename);
  87. dbms_sql .column_value(v_cid,3,v_deptno);
  88. insert into emp1(empno,ename,deptno) values (v_empno,v_ename,v_deptno);
  89. end loop;
  90. dbms_sql .close_cursor(v_cid);
  91. commit ;
  92. exception
  93. when others then
  94. dbms_sql .close_cursor(v_cid);
  95. raise;
  96. end ;
  97. 4.DDL语句:DDL中联编变量是非法的,即使在解析后不能够调用bind_variable过程。另外,DDL解析后立即执行,不需要调用EXECUTE 过程,即使调用了也没有用。
  98. create or replace procedure recreatetable(p_table in varchar2,p_description in varchar2)
  99. as
  100. v_cursornumber;
  101. v_createstringvarchar2(100);
  102. v_dropstringvarchar2(100);
  103. begin
  104. v_cursor:=dbms_sql .open_cursor;
  105. v_dropstring:='droptable' ||p_table;
  106. begin
  107. dbms_sql .parse(v_cursor,v_dropstring,dbms_sql .v7);
  108. exception
  109. when others then
  110. ifsqlcode!=-942then
  111. raise;
  112. end if;
  113. end ;
  114. v_createstring:='createtable' ||p_table||p_description;
  115. dbms_sql .parse(v_cursor,v_createstring,dbms_sql .native);
  116. dbms_sql .close_cursor(v_cursor);
  117. exception
  118. when others then
  119. dbms_sql .close_cursor(v_cursor);
  120. raise;
  121. end ;

你可能感兴趣的:(oracle,sql,C++,c,C#)