FILE: SAMPLE: Retrieving Output Parameters From Stored ProcedureID: Q183001
|
This Stored Procedure sample SPOutPut, which is described further in this article, shows how to derive from CDatabase in order to retrieve output parameters from a Stored Procedure within MFC.
The following file is available for download from the Microsoft Software Library:
~ SPOutPut.exeFor more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online ServicesThe first and main purpose of the SPOutPut sample is to show how to use MFC and derive from the CDatabase class in order to retrieve output parameters from a stored procedure that doesn't return a recordset. To accomplish the above, we need to override CDatabase's BindParameter function and call ODBC APIs directly as follows:
// This binds the RETURN value.
nRetCode = ::SQLBindParameter(hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_SLONG,
SQL_INTEGER, 11, 0,
(void*)&m_lDbOutParam, 0,
&m_lBufLength);
// This binds the OUTPUT value.
nRetCode = ::SQLBindParameter(hstmt, 2, SQL_PARAM_OUTPUT,
SQL_C_SLONG, SQL_INTEGER, 11, 0,
(void*)&m_lDbOutParam2, 0,
&m_lBufLength2);
ASSERT (nRetCode == SQL_SUCCESS);
The returned parameters are publicly declared so that these can be accessed
from other classes.
// Parameter order must be identical to what is declared in the SP.
pFX->SetFieldType(CFieldExchange::outputColumn);
RFX_Long(pFX, _T("colRecID"), m_colRecID);
RFX_Text(pFX, _T("colData"), m_colData);
// This is the RETURN value.
pFX->SetFieldType(CFieldExchange::outputParam);
RFX_Int(pFX, _T("output"), m_outputParam);
// This is the INPUT value.
pFX->SetFieldType(CFieldExchange::inputParam);
RFX_Int(pFX, _T("colData"), m_colDataParam);
// This is the OUTPUT value.
pFX->SetFieldType(CFieldExchange::outputParam);
RFX_Int(pFX, _T("output"), m_outputParam2);
However, what happens with our return and output parameters if we bound a
recordset as above, but the criteria given to the Stored Procedure doesn't
return any records?
while( rs.FlushResultSet())
{
while (!rs.IsEOF())
{
rs.MoveNext();
}
}
m_nOutParam = rs.m_outputParam; // Display the output parameter.
A third approach records, which is left as an exercise for the reader, to
retrieve the return value and output parameters if the stored procedure
doesn't return any would be to derive from CRecordset. Theoretically, if
you override the CRecordset's Move() method and return without doing
anything you would be able to use its RFX bindings to get the values.
Additional query words: kbvc500 kbvc600
Keywords : kbcode kbfile kbDatabase kbMFC kbODBC kbVC
Version : WINNT:5.0
Platform : winnt
Issue type : kbhowto
Last Reviewed: July 29, 1999