Use Stored Procedures with Output Parameters

Use Stored Procedures with Output Parameters

If you already know that the execution of your SQL statement will return a single row of data, then you should always prefer stored procedures with output parameters in place of single-row select statements.

There are several advantages of using stored procedures over embedding simple SQL statements in your application code. On top of all those advantages, what you save is instead of opening a recordset for fetching the data, you simply call a stored procedure with output parameters. When you use a recordset, the query results returned by the data source object include data and metadata. Often the metadata is much larger than the data or is a significant part of the query results. Because of this, you may want to use a stored procedure with output parameters instead.

For example, if you want to get the Name and Description of an Item identified by a Item Code what you can do is write a stored procedure which takes in one input paramter for Item Code and two output parameters for Name and Description. Here is a simple method which can be used to accomplish this:

 Private Sub GetNameDescFromSampleTable(ByVal SampleIdNo As Long, _ByRef SampleName As String, ByRef SampleDesc As String)On Error GoTo ErrorHandlerDim oConnection As ADODB.Connection ' Connection ObjectDim oCommand As ADODB.Command ' Command Object' Create a new connection objectSet oConnection = New ADODB.Connection' Open the connectionoConnection.Open "DSN=Sample", "sa", ""' Create a new command objectSet oCommand = New ADODB.Command' Set active connection of command object to the already ' open connectionSet oCommand.ActiveConnection = oConnection' Set the name of stored procedure in CommandText propertyoCommand.CommandText = "sp_GetSampleNameAndDescription"oCommand.CommandType = adCmdStoredProc' Add input and output parameters to the ' parameters collection of the command objectoCommand.Parameters.Append _oCommand.CreateParameter("@SampleIdNo", adInteger, adParamInput, , _SampleIdNo)oCommand.Parameters.Append _oCommand.CreateParameter("@SampleName", adVarChar, adParamOutput, 20)oCommand.Parameters.Append _oCommand.CreateParameter("@SampleDesc", adVarChar, adParamOutput, 200)' Call Execute of command object which ' internally executes the stored procedureoCommand.Execute' Take out the values of output parametersSampleName = oCommand.Parameters("@SampleName")SampleDesc = oCommand.Parameters("@SampleDesc")' Close the connection and release connection and command objectsoConnection.CloseSet oConnection = NothingSet oCommand = NothingExit SubErrorHandler:' Display error messageMsgBox "Error Number = " & Err.Number & ", Description = " & _Err.Description, vbCritical, "GetNameDescFromSampleTable Error"End Sub
See also  Why ChatGPT Is So Important Today

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist