<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en-us" xml:lang="en-us"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="security" content="public" /> <meta name="Robots" content="index,follow" /> <meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' /> <meta name="DC.Type" content="reference" /> <meta name="DC.Title" content="Retrieve results" /> <meta name="abstract" content="In order to work with all of the rows in a result set, call the SQLFetch API until no more rows are returned." /> <meta name="description" content="In order to work with all of the rows in a result set, call the SQLFetch API until no more rows are returned." /> <meta name="DC.Relation" scheme="URI" content="rzaikodbcapiinfo.htm" /> <meta name="copyright" content="(C) Copyright IBM Corporation 1999, 2006" /> <meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1999, 2006" /> <meta name="DC.Format" content="XHTML" /> <meta name="DC.Identifier" content="retrieveresults" /> <meta name="DC.Language" content="en-us" /> <!-- All rights reserved. Licensed Materials Property of IBM --> <!-- US Government Users Restricted Rights --> <!-- Use, duplication or disclosure restricted by --> <!-- GSA ADP Schedule Contract with IBM Corp. --> <link rel="stylesheet" type="text/css" href="./ibmdita.css" /> <link rel="stylesheet" type="text/css" href="./ic.css" /> <title>Retrieve results</title> </head> <body id="retrieveresults"><a name="retrieveresults"><!-- --></a> <!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script> <h1 class="topictitle1">Retrieve results</h1> <div><p>In order to work with all of the rows in a result set, call the <strong>SQLFetch</strong> API until no more rows are returned.</p> <div class="section"><p>Running some <span class="keyword">SQL</span> statements returns results to the application program. Running an <span class="keyword">SQL</span> SELECT statement returns the selected rows in a result set. The <strong><span class="keyword">SQL</span>Fetch</strong> API then sequentially retrieves the selected rows from the result set into the application program's internal storage. </p> </div> <div class="section"><p>You also may issue a Select statement where you do not specify what columns you want returned. For example, <samp class="codeph">SELECT * FROM RWM.DBFIL</samp> selects all columns. You may not know what columns or how many columns will be returned. </p> <dl><dt class="dlterm">SQLNumResultCols</dt> <dd>Returns the number of columns in a result set. <ul><li>A storage buffer that receives the information is passed as a parameter.</li> </ul> <pre> SQLSMALLINT nResultCols; rc = SQLNumResultCols(hstmt, &nResultCols);</pre> </dd> <dt class="dlterm">SQLDescribeCol</dt> <dd>Returns the result descriptor for one column in a result set. <ul><li><strong>Column name</strong></li> <li><strong>Column type</strong></li> <li><strong>Column size</strong> <p>This is used with <strong>SQLNumResultCols</strong> to retrieve information about the columns returned. Using this approach, as opposed to hard coding the information in the program, makes for more flexible programs.</p> <p>The programmer first uses <strong>SQLNumResultCols</strong> to find out how many columns were returned in the result set by a select statement. Then a loop is set up to use <strong>SQLDescribeCol</strong> to retrieve information about each column.</p> </li> </ul> <p>In C, this statement is coded: </p> <pre> SQLCHAR szColName[51]; SQLSMALLINT lenColName, colSQLtype, scale, nullable; SQLUSMALLINT colNum = 1; SQLUINTEGER cbColDef; rc = SQLDescribeCol(hstmt, colNum, szColName, sizeof(szColName), &lenColName, &colSQLtype, &cbColDef, &scale, &nullable);</pre> </dd> <dt class="dlterm">SQLBindCol</dt> <dd>Assigns the storage and data type for a column in a result set: <ul><li>Storage buffer that receives the information.</li> <li>Length of storage buffer.</li> <li>Data type conversion.</li> </ul> <p>In C, this statement is coded: </p> <pre>SQLUSMALLINT colNum = 1; SQLUINTEGER cbColDef; SQLINTEGER idNum, indPtr, strlen_or_indPtr; SQLCHAR szIDName[51]; colNum = 1; rc = SQLBindCol(hstmt, colNum, SQL_C_LONG, &idNum, sizeof(SQLINTEGER), &indPtr); colNum = 2; rc = SQLBindCol(hstmt, colNum, SQL_C_CHAR, szIDName, sizeof(szIDName), &strlen_or_indPtr);</pre> <div class="note"><span class="notetitle">Note:</span> If you use this with Visual Basic, it is recommended that you use an array of Byte data type in place of String data types.</div> </dd> <dt class="dlterm">SQLFetch</dt> <dd>Each time <strong>SQLFetch</strong> is called, the driver fetches the next row. Bound columns are stored in the locations specified. Data for unbound columns may be retrieved using <strong>SQLGetData</strong>. <div class="p">In C, this statement is coded: <pre>rc = SQLFetch(hstmt);</pre> </div> <p>Visual Basic does not directly support pointers or fixed memory location ANSI character null-terminated strings. For this reason, it is best to use another method to bind Character and Binary parameters. One method is to convert Visual Basic String data types to/from an array of Byte data types and bind the array of Byte. Another method is to use the <strong>SQLGetData</strong> function instead of <strong>SQLBindCol</strong>.</p> </dd> <dt class="dlterm">SQLGetData</dt> <dd>Retrieves data for unbound columns after a fetch. In this example, three columns are returned and <strong>SQLGetData</strong> is used to move them to the correct storage location. <p>In C, this statement is coded: </p> <pre>SQLCHAR szTheName[16], szCredit[2]; float iDiscount, iTax; rc = SQLFetch(hstmt); rc = SQLGetData(hstmt, 1, SQL_C_CHAR, szTheName, 16, &strlen_or_indPtr); rc = SQLGetData(hstmt, 2, SQL_C_FLOAT, &iDiscount, sizeof(float), &indPtr); rc = SQLGetData(hstmt, 3, SQL_C_CHAR, szCredit, 2, &strlen_or_indPtr); rc = SQLGetData(hstmt, 4, SQL_C_FLOAT, &iTax, sizeof(float), &indPtr); </pre> <p>In Visual Basic, this statement is coded: </p> <pre> rc = SQLFetch(hStmt) If rc = SQL_NO_DATA_FOUND Then Call DisplayWarning("No record found!") rc = SQLCloseCursor(hStmt) If rc <> SQL_SUCCESS Then Call DspSQLDiagRec(SQL_HANDLE_STMT, hStmt, "Close cursor failed.") End If Else ' Reset lcbBuffer for the call to SQLGetData lcbBuffer = 0 'Get part ID from the fetched record rc = SQLGetData(hStmt, 1, SQL_C_LONG, _ lPartIDReceived, Len(lPartIDReceived), lcbBuffer) If rc <> SQL_SUCCESS And rc <> SQL_SUCCESS_WITH_INFO Then _ Call DspSQLDiagRec(SQL_HANDLE_STMT, hStmt, _ "Problem getting data for PartID column") 'Get part description from the fetched record rc = SQLGetData(hStmt, 2, SQL_C_CHAR, _ szDescription(0), 257, lcbBuffer) If rc <> SQL_SUCCESS And rc <> SQL_SUCCESS_WITH_INFO Then _ Call DspSQLDiagRec(SQL_HANDLE_STMT, hStmt, _ "Problem getting data for PartDescription column") 'Get part provider from the fetched record rc = SQLGetData(hStmt, 3, SQL_C_CHAR, _ szProvider(0), 257, lcbBuffer) If rc <> SQL_SUCCESS And rc <> SQL_SUCCESS_WITH_INFO Then _ Call DspSQLDiagRec(SQL_HANDLE_STMT, hStmt, _ "Problem getting data for PartProvider column") Call DisplayMessage("Record found!") rc = SQLCloseCursor(hStmt) If rc <> SQL_SUCCESS Then _ Call DspSQLDiagRec(SQL_HANDLE_STMT, hStmt, "Close cursor failed.") End If</pre> </dd> </dl> </div> </div> <div> <div class="familylinks"> <div class="parentlink"><strong>Parent topic:</strong> <a href="rzaikodbcapiinfo.htm" title="Identify the files required to build an ODBC application.">Files required to build an ODBC application</a></div> </div> </div> </body> </html>