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

📄 otl4_ex313.htm

📁 ISO_C++:C++_OTL开发文档
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
  <title>Example 313 (PL/SQL tables of CHAR and VARCHAR2 together 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 313 (PL/SQL tables of CHAR and
VARCHAR2 together as parameters, <br>
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;<br>using namespace std;<br><br>#include &lt;stdio.h&gt;</pre>
<pre>#define OTL_ORA7 // Compile OTL 4.0/OCI7<br>#include &lt;otlv4.h&gt; // include the OTL 4.0 header file<br><br><a
 href="otl3_connect_class.htm">otl_connect</a> db; // connect object<br></pre>
<pre>void plsql(void)<br>{ <br> otl_stream s(1, // buffer size needs to be set to 1 in this case<br>                "begin "<br>                "  pkg_test.prc_test(:t1&lt;int,inout[100]&gt;, "<br>                "                    :t2&lt;char[32],out[200]&gt;, "<br>                "                    :t3&lt;charz[32],out[200]&gt;); "<br>               "end;",<br>              db // connect object<br>             );<br><br> s.set_commit(0); // Since there is no transactions, unset the stream auto-commit<br><br> <a
 href="otl3_pl_tab.htm">otl_int_tab&lt;</a>100&gt; t1; // PL/SQL table of int[100]<br> <a
 href="otl3_pl_tab.htm">otl_cstr_tab</a>&lt;200,32&gt; t2; // PL/SQL table of char[200][32]<br> <a
 href="otl3_pl_tab.htm">otl_cstr_tab</a>&lt;200,32&gt; t3; // PL/SQL table of char[200][32]<br></pre>
<pre> t1.v[0]=1; // assign 1 to t1[0]<br> t1.set_non_null(0); // specify that the value is not null<br><br> t1.v[1]=2; // assign 2 to t1[1]<br> t1.set_non_null(1); // specify that the value is not null<br><br> t1.set_null(2); // set t1[2]=NULL<br> 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<br><br> s&gt;&gt;t1; // read t1 from the stream since it is input/output<br> s&gt;&gt;t2; // read t2 from the stream since it is output.<br> s&gt;&gt;t3; // read t3 from the stream since it is output.<br></pre>
<pre> cout&lt;&lt;"T1_Len="&lt;&lt;t1.len()&lt;&lt;endl;<br> for(int i=0;i&lt;t1.len();++i)<br>  if(t1.is_null(i))<br>   cout&lt;&lt;"T1["&lt;&lt;i&lt;&lt;"]=NULL"&lt;&lt;endl;<br>  else<br>   cout&lt;&lt;"T1["&lt;&lt;i&lt;&lt;"]="&lt;&lt;t1.v[i]&lt;&lt;endl;<br><br> cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;"T2_Len="&lt;&lt;t2.len()&lt;&lt;endl;<br> for(int j=0;j&lt;t2.len();++j)<br>  if(t2.is_null(j))<br>   cout&lt;&lt;"T2["&lt;&lt;j&lt;&lt;"]=NULL"&lt;&lt;endl;<br>  else<br>   cout&lt;&lt;"T2["&lt;&lt;j&lt;&lt;"]="&lt;&lt;t2.v[j]&lt;&lt;endl;<br><br> cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;"T3_Len="&lt;&lt;t3.len()&lt;&lt;endl;<br> for(int k=0;k&lt;t3.len();++k)<br>  if(t3.is_null(k))<br>   cout&lt;&lt;"T3["&lt;&lt;k&lt;&lt;"]=NULL"&lt;&lt;endl;<br>  else<br>   cout&lt;&lt;"T3["&lt;&lt;k&lt;&lt;"]="&lt;&lt;t3.v[k]&lt;&lt;endl;<br><br>}<br><br><br>int main()<br>{<br> <a
 href="otl3_connect_class.htm">otl_connect::otl_initialize</a>(); // initialize OCI environment<br> try{<br><br>  db.rlogon("scott/tiger"); // connect to Oracle<br><br>  otl_cursor::direct_exec<br>   (db,<br>    "CREATE OR REPLACE PACKAGE pkg_test IS "<br>    "  TYPE my_numeric_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; "<br>    "  TYPE my_varchar_table IS TABLE OF VARCHAR2(31) INDEX BY BINARY_INTEGER; "<br>    "  TYPE my_char_table IS TABLE OF CHAR(31) INDEX BY BINARY_INTEGER; "<br>    " "<br>    "  PROCEDURE prc_test(v1 IN OUT my_numeric_table, "<br>    "                     v2 OUT my_varchar_table, "<br>    "                     v3 OUT my_char_table); "<br>    " "<br>    "END; "<br>  );<br><br>  otl_cursor::direct_exec<br>   (db,<br>    "CREATE OR REPLACE PACKAGE BODY pkg_test IS "<br>    " "<br>    "  PROCEDURE prc_test(v1 IN OUT my_numeric_table, "<br>    "                     v2 OUT my_varchar_table, "<br>    "                     v3 OUT my_char_table) "<br>    "  IS "<br>    "  BEGIN "<br>    "    FOR i IN 1..v1.last LOOP "<br>    "      v1(i) := v1(i)+100; "<br>    "    END LOOP; "<br>    "    v1(v1.last+1) := 0; "<br>    "    v2(1) := 'Hello'; "<br>    "    v2(2) := 'World'; "<br>    "    v2(3) := '!!!'; "<br>    "    v3(1) := 'Hello'; "<br>    "    v3(2) := 'World'; "<br>    "    v3(3) := '!!!'; "<br>    "  END; "<br>    "   "<br>    "END; "<br>  );<br><br><br>  plsql();<br><br> }<br><br> catch(<a
 href="otl3_exception_class.htm">otl_exception</a>&amp; p){ // intercept OTL exceptions<br>  cerr&lt;&lt;p.msg&lt;&lt;endl; // print out error message<br>  cerr&lt;&lt;p.stm_text&lt;&lt;endl; // print out SQL that caused the error<br>  cerr&lt;&lt;p.var_info&lt;&lt;endl; // print out the variable that caused the error<br> }<br><br> db.logoff(); // disconnect from Oracle<br><br> return 0;<br><br>}<br></pre>
<h2>Output</h2>
<pre>T1_Len=4<br>T1[0]=101<br>T1[1]=102<br>T1[2]=NULL<br>T1[3]=0<br><br><br>T2_Len=3<br>T2[0]=Hello<br>T2[1]=World<br>T2[2]=!!!<br><br><br>T3_Len=3<br>T3[0]=Hello<br>T3[1]=World<br>T3[2]=!!!<br><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@yahogmail</a>
<script language="JavaScript"><!-- hide from old browsers
 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 + -