📄 fetch_multiple.html
字号:
<html>
<head>
<meta name="KEYWORDS" content=" library, C++, Oracle, SQLServer, DB2, Sybase, Informix, InterBase, SQLBase, ODBC,
SQL, server, client, database, programming, development, software,
Win32, Windows, Linux, Unix, Solaris,
Microsoft, Borland, Centura, Gupta, IBM"><meta name="DESCRIPTION" content=
"SQLAPI++ is C++ library for accessing SQL databases (Oracle, SQLServer, Sybase, DB2, InterBase, SQLBase, Informix, ODBC).
It uses native APIs, provides low-level access to database-specific features."><title>SQLAPI++ Library - How To - Fetching multiple result sets</title>
</head>
<body bgcolor="#ffffff" BACKGROUND="../images/bg.gif" text="#000000"><!--class="GlbHdr"-->
<table WIDTH="600" height="40" frame="hsides" BORDER="2" BORDERCOLOR="#004fcd" align="right" cellpadding="0" cellspacing="2" id="TABLE1">
<tr><!--class="GlbDefault"-->
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../index.html" style="TEXT-DECORATION: none">
<font color="white" size="3" face="sans-serif">
<b>Home</b></font></A></td></TD>
<!--class="GlbActive"-->
<td width="70" bgcolor="#00bb00" bordercolor="black" align="middle"><A href="index.html" style="TEXT-DECORATION: none">
<font color="white" size="3" face="sans-serif">
<b>How To</b></font></A></td>
<!--class="GlbDefault"-->
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../OnLineDoc/index.html" style="TEXT-DECORATION: none">
<font color="white" size="3" face="sans-serif">
<b>Online Documentation</b></font></A></td><!--class="GlbDefault"-->
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../Support/index.html" style="TEXT-DECORATION: none">
<font color="white" size="3" face="sans-serif">
<b>Support</b></font></A></td>
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../Download/index.html" style="TEXT-DECORATION: none">
<font color="white" size="3" face="sans-serif">
<b>Download</b></font></A></td>
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../Order/index.html" style="TEXT-DECORATION: none">
<font color="white" size="3" face="sans-serif">
<b>Order</b></font></A></td>
</tr>
</table>
<p> </p>
<p> </p>
<table width="100%" cellpadding="1" cellspacing="1">
<tr>
<td valign="top" align="left"><!--class="LeftMenu"-->
<table frame="void" CELLSPACING="2" cellpadding="2" width="165">
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="connect.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Connecting to databases</font></A> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="transactions.html" style ="TEXT-DECORATION: none" >
<font color="white" face="sans-serif" size=2>
Transactions</font></A> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="commands.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Executing an SQL command</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="procedures.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Executing stored procedures</A>
</FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="bind.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Binding input parameters</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="fetch.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Fetching result set</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/red.gif" WIDTH="12" HEIGHT="12"
>
<A href="fetch_multiple.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Fetching multiple result sets</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"
>
<A href="performance.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Optimizing Database Performance</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="return_param.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Returning output parameters</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="blobs.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Working
with Long Lob(CLob, BLob) Data</A>
</FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"><A href="datetime.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Working with date/time values</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="errors.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Error handling</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="query_exampl.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Query examples</A> </FONT> </td>
</tr>
</table>
</td>
<td width="30">
<p> </p>
</td>
<td valign="top">
<center>
<h1>Fetching multiple result sets</h1>
</center>
<p>
It is possible to process more than one
result set returned from a batch or stored procedure using
SQLAPI++. To execute a command and process multiple result sets you should do the following:</p>
<ol>
<li>Create a command object and set a
command text.
<li>Bind input parameters (if needed).
<li>Execute the command.
<li> Process result sets.</li></ol>
<p>For example, let's fetch rows from this sample
query (batch):<BR>Select 'aaa' union select 'bbb'<BR>Select 1 union
select 2 union select 3.</p>
<h4>Step 1. Creating a command object and setting a command
text.</h4>
<p>To execute a command we need two objects: <A href="../OnLineDoc/Connection.html">SAConnection</A>
(connection object) and <A href="../OnLineDoc/Command.html">SACommand</A> (command object).</p>
<p><strong><font face =""
size="2"><font face="System"><font>SACommand cmd(&Connection,
<BR>"Select 'aaa' as fstr union select 'bbb'\n"<BR>"Select 1 as
fnum union select 2 union select
3");</font></font></font></strong><font><font face="System"></font></font> </p>
<p>The line above creates a command object <em>cmd</em> based on previously created and
connected connection object <em>Connection</em>
(for creating and connection <em>Connection</em>
object see <A href="connect.html">Connecting to databases</A> ).
Second parameter is our sample command text.</p>
<h4>Step 2. Binding input parameters.</h4>
<p>The Library provides several ways for
binding input variables. To get more details see <A href="bind.html">Binding input parameters</A>.<BR>It is nothing to bind in our example.</p>
<h4>Step 3. Execute the command.</h4>
<p>To execute a command use <A href="../OnLineDoc/Command_Execute.html">SACommand::Execute</A>
method:</p>
<p><font face="System">cmd.Execute();</font></p>
<h4>Step 4. Processing the result sets.</h4>
<p>Now we have to process all result sets
returned.</p>
<p><STRONG>// Process first result
set<BR></STRONG><font face="System">while(cmd.FetchNext())<BR>{<BR> // do
something special with first result set<BR> SAString s =
cmd["fstr"];<br>
printf("fstr: %s\n", s);<br>
} </font><font face="System">
</p>
<p><STRONG>// Process second result
set<BR></STRONG><font face="System">while(cmd.FetchNext())<BR>{<BR> // do
something special with second result set<BR> long n =
cmd["fnum"];<br>
printf("fnum: %s\n", n);<br>}<br>
</font>
</p>
<p><FONT face="Times New Roman">If number of
result sets is not known at compile time we can process result sets
while <A
href="../OnLineDoc/Command_isResultSet.html">SACommand::isResultSet</A>
returns true:</FONT>
<p><font face="System">int
nResulSets =
0;<BR>while(cmd.isResultSet())<BR>{<BR>
printf("Processing result set #%d",
++nResulSets);<BR>
while(cmd.FetchNext())<BR>
{<BR> // do something
special with this result
set<BR> SAString s =
cmd[1].asString();<br> printf("field
value: %s\n", s);<br>
}<BR>
</font><font face="System">
</font>}<br></FONT>
</p>
<h3>Problems and Questions</h3>
<p>If you don't find the answer to you
questions or have some problems on using the Library, please, send
e-mail to <A href="mailto:howto@sqlapi.com">howto@sqlapi.com</A>. </p>
<p> </p>
</td>
<tr></tr>
</table></FONT>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -