Executing Queries

Overview

Once a connection has been opened, a query or stored procedure can be executed in many ways. 

Detailed
Executing a simple SQL statement (no parameters)

‘Assume a connection has already been opened

‘ Set the command object properties (active connection, command type, etc.)
objCmd.ActiveConnection = objConn
objCmd.CommandType      = adCmdText
objCmd.CommandText      = "select * from SomeTable"

‘ Execute the command object (returns read-only, forward-only cursor)
set objRS = objCmd.Execute

Executing a parameterized query

Parameters in a parameterized query are indicated by a question mark character, ‘?’. ADO will automatically create and append a parameter to the command object for each ‘?’ it finds in the query.

‘Assume a connection has already been opened
objCmd.ActiveConnection = objConn
objCmd.CommandType      = adCmdTxt
objCmd.CommandText      = "select * from T1 where f1 = ?"

‘ ADO already created a parameter for use. Set its value
With objCmd.Parameters.Item(0)
    .Value     = "White"
    .Direction = adParamInput
    .Type      = adVarChar
    .Size      = 40
End With

‘Execute the command object (returns read-only, forward-only cursor)
set objRs = objCmd.Execute

Executing a parameterized stored procedure

Note that with adCmdStoredProc constant, the query string is executed using the ODBC escape function syntax: {? = call procedure(?)}. Never prepare the execution of a stored procedure. This example assumes that the stored proc takes two input parameters

create procedure StoredProcName 
@Param1 int, 
@Param2 int 
AS
...
return 0

‘Assume a connection has already been opened
objCmd.ActiveConnection = objConn
objCmd.CommandType      = adCmdStoredProc
objCmd.CommandText      = StoredProcName

‘ Create the parameters one by one and add them to the parameters collection
set intParam = objCmd.CreateParamter("ID", adInteger, adParamInput, 4, 10)
objCmd.Parameters.Append intParam ' this param has a value of 10

set strParam = objCmd.CreateParameter("name", adChar, adParamInput, 255, "yazan")
objCmd.Parameters.Append strParam ' this param has a value of "yazan"

‘Execute the command object (returns read-only, forward-only cursor)
set objRs = objCmd.Execute

Executing a Parameterized Stored Proc with return values

' Create a command object to execute a stored proc
Dim oCmd As New ADODB.Command
oCmd.ActiveConnection = gCN
oCmd.CommandText      = "CmdWithParamExample"
oCmd.CommandType      = adCmdStoredProc

' Set up parameters for the stored proc: It takes an input, an output and 
' returns a value. All three are integers No need to create a parameter dim
' for each parameter. Use the  same parameter object
Dim oPrm As ADODB.Parameter

    ' Return value
    Set oPrm = oCmd.CreateParameter("Ret", adInteger, adParamReturnValue, 4)
    oCmd.Parameters.Append oPrm

    ' Input
    Set oPrm = oCmd.CreateParameter("In", adInteger, adParamInput, 4, 10)
    oCmd.Parameters.Append oPrm

    ' Output
    Set oPrm = oCmd.CreateParameter("Out", adInteger, adParamOutput, 4, -10)
    oCmd.Parameters.Append oPrm

' Execute the command. Note that since we're using a client-side cursor, ADO 
' will return a static cursor and not dynamic as we have requested!
Dim oRs As New ADODB.Recordset
oRs.CursorLocation = adUseClient
oRs.Open oCmd, , adOpenDynamic

' Since it's static, we can get the count
Dim lRecCount As Integer
lRecCount = oRs.RecordCount

' The output and return values are only available after the recordset has 
' been closed
Debug.Print oCmd.Parameters.Item("In")
Debug.Print oCmd.Parameters.Item("Out")
Debug.Print oCmd.Parameters.Item("Ret")

' Clean up
Set oRs = Nothing
Set oCmd = Nothing

CREATE PROCEDURE CmdWithParamExample
@Param1 int,
@RetrunParam int output

// Perform some SQL operations
...

// Assign a value to the output parameter
set @ReturnParam = 100

// return a value from the stored procedure
return -1