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
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may