⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otl3_ex49.htm

📁 ISO_C++:C++_OTL开发文档
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
   <TITLE>Example 49 (PL/SQL tables as parameters, Oracle 7)</TITLE>
   <META NAME="Author" CONTENT="Sergei Kuchin">
   <META NAME="GENERATOR" CONTENT="Mozilla/3.03Gold (Win95; I) [Netscape]">
   <META NAME="KeyWords" CONTENT="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library">
</HEAD>
<BODY>

<H1 ALIGN=CENTER>OTL 4.0, Example 49 (PL/SQL tables as parameters, <BR>
Oracle 7)</H1>

<H1>Example 49 (PL/SQL tables as parameters, Oracle 7)</H1>

<P>This example demonstrates how to use OTL <A HREF="otl3_pl_tab.htm">template
&amp; dymanic PL/SQL table containers</A> for reading/writing PL/SQL tables
from/to the otl_stream.</P>

<H2>Source Code</H2>

<PRE>#include &lt;iostream&gt;
using namespace std;

#include &lt;stdio.h&gt;</PRE>

<PRE>#define OTL_ORA7 // Compile OTL 4.0/OCI7
#include &lt;otlv4.h&gt; // include the OTL 4.0 header file

<A HREF="otl3_connect_class.htm">otl_connect</A> db; // connect object
</PRE>

<PRE>void plsql(void)
{ 
 otl_stream s(1, // buffer size needs to be set to 1 in this case
                &quot;begin &quot;
                &quot;  pkg_test.prc_test(:t1&lt;int,inout[100]&gt;,:t2&lt;char[32],out[200]&gt;); &quot;
               &quot;end;&quot;,
              db // connect object
             );

 s.set_commit(0); // Since there is no transactions, unset the stream auto-commit

 <A HREF="otl3_pl_tab.htm">otl_int_tab&lt;</A>100&gt; t1; // PL/SQL table of int[100]
 <A HREF="otl3_pl_tab.htm">otl_cstr_tab</A>&lt;200,32&gt; t2; // PL/SQL table of char[200][32]</PRE>

<PRE> t1.v[0]=1; // assign 1 to t1[0]
 t1.set_non_null(0); // specify that the value is not null

 t1.v[1]=2; // assign 2 to t1[1]
 t1.set_non_null(1); // specify that the value is not null

 t1.set_null(2); // set t1[2]=NULL
 t1.set_len(3); // set t1's length to 3</PRE>

<PRE> s&lt;&lt;t1; // write t1 into the stream, since it is input

 s&gt;&gt;t1; // read t1 from the stream since it is input/output
 s&gt;&gt;t2; // read t2 from the stream since it is output.</PRE>

<PRE> cout&lt;&lt;&quot;T1_Len=&quot;&lt;&lt;t1.len()&lt;&lt;endl;
 for(int i=0;i&lt;t1.len();++i)
  if(t1.is_null(i))
   cout&lt;&lt;&quot;T1[&quot;&lt;&lt;i&lt;&lt;&quot;]=NULL&quot;&lt;&lt;endl;
  else
   cout&lt;&lt;&quot;T1[&quot;&lt;&lt;i&lt;&lt;&quot;]=&quot;&lt;&lt;t1.v[i]&lt;&lt;endl;

 cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;&quot;T2_Len=&quot;&lt;&lt;t2.len()&lt;&lt;endl;
 for(int j=0;j&lt;t2.len();++j)
  if(t2.is_null(j))
   cout&lt;&lt;&quot;T2[&quot;&lt;&lt;j&lt;&lt;&quot;]=NULL&quot;&lt;&lt;endl;
  else
   cout&lt;&lt;&quot;T2[&quot;&lt;&lt;j&lt;&lt;&quot;]=&quot;&lt;&lt;t2.v[j]&lt;&lt;endl;

}


int main()
{
 <A HREF="otl3_connect_class.htm">otl_connect::otl_initialize</A>(); // initialize OCI environment
 try{

  db.rlogon(&quot;scott/tiger&quot;); // connect to Oracle

  otl_cursor::direct_exec
   (db,
    &quot;CREATE OR REPLACE PACKAGE pkg_test IS &quot;
    &quot;  TYPE my_numeric_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; &quot;
    &quot;  TYPE my_varchar_table IS TABLE OF VARCHAR2(31) INDEX BY BINARY_INTEGER; &quot;
    &quot; &quot;
    &quot;  PROCEDURE prc_test(v1 IN OUT my_numeric_table, v2 OUT my_varchar_table); &quot;
    &quot; &quot;
    &quot;END; &quot;
  );

  otl_cursor::direct_exec
   (db,
    &quot;CREATE OR REPLACE PACKAGE BODY pkg_test IS &quot;
    &quot; &quot;
    &quot;  PROCEDURE prc_test(v1 IN OUT my_numeric_table, v2 OUT my_varchar_table) &quot;
    &quot;  IS &quot;
    &quot;  BEGIN &quot;
    &quot;    FOR i IN 1..v1.last LOOP &quot;
    &quot;      v1(i) := v1(i)+100; &quot;
    &quot;    END LOOP; &quot;
    &quot;    v1(v1.last+1) := 0; &quot;
    &quot;    v2(1) := 'Hello'; &quot;
    &quot;    v2(2) := 'World'; &quot;
    &quot;    v2(3) := '!!!'; &quot;
    &quot;  END; &quot;
    &quot;   &quot;
    &quot;END; &quot;
  );


  plsql();

 }

 catch(<A HREF="otl3_exception_class.htm">otl_exception</A>&amp; p){ // intercept OTL exceptions
  cerr&lt;&lt;p.msg&lt;&lt;endl; // print out error message
  cerr&lt;&lt;p.stm_text&lt;&lt;endl; // print out SQL that caused the error
  cerr&lt;&lt;p.var_info&lt;&lt;endl; // print out the variable that caused the error
 }

 db.logoff(); // disconnect from Oracle

 return 0;

}
</PRE>

<H2>Output</H2>

<PRE>T1_Len=4
T1[0]=101
T1[1]=102
T1[2]=NULL
T1[3]=0


T2_Len=3
T2[0]=Hello
T2[1]=World
T2[2]=!!!





<HR WIDTH="100%"></PRE>

<CENTER><P><A HREF="otl3_examples.htm">Examples</A> <A HREF="otl3.htm">Contents</A>
<A HREF="home.htm">Go Home</A> </P></CENTER>

<P>Copyright &copy; 1996, 2008, Sergei Kuchin, email: <A HREF="mailto:skuchin@aceweb.com">skuchin@aceweb.com</A>,
<A HREF="mailto:skuchin@gmail.com">skuchin@gmail.com</A> <SCRIPT Language="JavaScript"><!-- hide from oldgmailsers
 var modDate = new Date(document.lastModified)
 document.write("<i> Last Updated:</i> " + (modDate.getMonth()+1) + "/" + 
                modDate.getDate() + "/" + "0"+(modDate.getYear())%100+".");
 //-->
 </SCRIPT>.</P>

<P><I>Permission to use, copy, modify and redistribute this document for
any purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies. </I></P>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-5456201-1");
pageTracker._trackPageview();
</script>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -