📄 otl4_ex494.htm
字号:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="Author" content="Sergei Kuchin">
<meta name="GENERATOR"
content="Mozilla/4.77 [en] (Win95; U) [Netscape]">
<meta name="KeyWords"
content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library">
<title>Example 494 (UTF8, PLSQL/index-by tables as parameters, and
NVARCHAR2s)</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 494 (UTF8, PLSQL/index-by tables as
parameters, and NVARCHAR2s)</h1>
</center>
This example demonstrates how to use OTL <a href="otl3_pl_tab.htm">template
& dynamic PL/SQL table containers</a> for reading/writing PL/SQL
tables
from/to the otl_stream.
<h2>Source Code</h2>
<pre>#include <iostream><br>using namespace std;<br><br>#include <stdio.h></pre>
<pre>#define OTL_ORA9I // Compile OTL 4.0/OCI9i<br>#define <a
href="otl3_compile.htm#OTL_ORA_UTF8">OTL_ORA_UTF8</a> // Enable UTF8 OTL for OCI9i<br>#include <otlv4.h> // include the OTL 4.0 header file<br><br><a
href="otl3_connect_class.htm">otl_connect</a> db; // connect object</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<int,inout[100]>,:t2<nchar[31],out[200]>); "<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</a><100> t1; // PL/SQL table of int[100]<br> <a
href="otl3_pl_tab.htm">otl_cstr_tab</a><200,130> t2; <br> // PL/SQL table of char[200][130]<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<<t1; // write t1 into the stream, since it is input<br><br> s>>t1; // read t1 from the stream since it is input/output<br> s>>t2; // read t2 from the stream since it is output.</pre>
<pre> cout<<"T1_Len="<<t1.len()<<endl;<br> for(int i=0;i<t1.len();++i)<br> if(t1.is_null(i))<br> cout<<"T1["<<i<<"]=NULL"<<endl;<br> else<br> cout<<"T1["<<i<<"]="<<t1.v[i]<<endl;<br><br> cout<<endl<<endl<<"T2_Len="<<t2.len()<<endl;<br> for(int j=0;j<t2.len();++j)<br> if(t2.is_null(j))<br> cout<<"T2["<<j<<"]=NULL"<<endl;<br> else {<br> cout<<"T2["<<j<<"]=";<br> unsigned char* t2j=reinterpret_cast<unsigned char*>(&t2.v[j]);<br> while(*t2j)printf("%2x ", *t2j++);<br> cout<<", "<<t2.v[j];<br> cout<<endl;<br> }<br><br>}<br><br><br>int main()<br>{<br> putenv(const_cast<char*>("NLS_LANG=.AL32UTF8")); // set your Oracle Client NLS_LANG <br> // if its default was set to something else<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 NVARCHAR2(128) INDEX BY BINARY_INTEGER; "<br> " /* size is defined in bytes for Oracle 8i... */ "<br> " "<br> " PROCEDURE prc_test(v1 IN OUT my_numeric_table, v2 OUT my_varchar_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, v2 OUT my_varchar_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> " END; "<br> " "<br> "END; "<br> );<br><br><br> plsql();<br><br> }<br><br> catch(<a
href="otl3_exception_class.htm">otl_exception</a>& p){ // intercept OTL exceptions<br> cerr<<p.msg<<endl; // print out error message<br> cerr<<p.stm_text<<endl; // print out SQL that caused the error<br> cerr<<p.var_info<<endl; // print out the variable that caused the error<br> }<br><br> db.logoff(); // disconnect from Oracle<br><br> return 0;<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>T2_Len=3<br>T2[0]=48 65 6c 6c 6f , Hello<br>T2[1]=57 6f 72 6c 64 , World<br>T2[2]=21 21 21 , !!!<br><hr
width="100%"></pre>
<center><a href="otl3_examples.htm">Examples</a> <a href="otl3.htm">Contents</a><a
href="home.htm">Go
Home</a></center>
<p>Copyright © 1996, 2008, Sergei Kuchin, email: <a
href="mailto:skuchin@aceweb.com">skuchin@aceweb.com</a>,
<a href="mailto:skuchin@gmail.com">skuchin@gmail.com
<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></a>.
</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 + -