Article ID: 195047
Article Last Modified on 8/30/2004
* Begin code.
* Demonstrates three ways to call a stored procedure that accepts
* parameters.
*
* The stored procedure used is BYROYALTY in pubs, which queries the
* titleauthor table for royalty amounts that equal the
* passed value, and returns a recordset.
#DEFINE adInteger 3
#DEFINE adParamOutput 2
#DEFINE adUseClient 3
#DEFINE adModeReadWrite 3
#DEFINE adCmdText 1
#DEFINE adExecuteNoRecords 128
CLEAR
oConnection = CREATEOBJECT("ADODB.Connection")
oCommand = CREATEOBJECT("ADODB.Command")
oRecordSet = CREATEOBJECT("ADODB.Recordset")
oParameters = CREATEOBJECT("ADODB.Parameter")
lcConnString = "driver={SQL Server};" + ;
"Server=CHICKENHAWK;" + ;
"DATABASE=pubs"
lcUID = "sa"
lcPWD = ""
WITH oConnection
.CursorLocation = adUseClient
.ATTRIBUTES = adModeReadWrite
.OPEN(lcConnString,lcUID,lcPWD, )
ENDWITH
************************************************************
* Here's the easiest way to implement:
*
* Tell the command object that the CommandType is
* a regular command, and pass the parameter you want
* in the CommandText. Most providers can interpret
* the default adCmdUnknown, or the common adCmdText correctly.
*
* However, it will not let you return a value.
WITH oCommand
.CommandText = "byroyalty (40)"
.ActiveConnection = oConnection
ENDWITH
oRecordSet.OPEN(oCommand)
* display the recordset on the desktop
=ShowRS()
WAIT WINDOW "Method 1 complete - press a key to continue"
** released rs and command object
release oRecordset
release oCommand
************************************************************
* A second method is to pass parameters with a command,
* automatically populating the parameters collection.
*
* The programmer does not have to know the parameter binding
* information, the Parameters collection refresh method
* gets it for you from the server.
*
* However, this method has to go to the server
* before calling the Stored Procedure, resulting in a likely
* performance hit.
*
* This command text string says:
* Return a parameter (?) from a call to SP byroyalty
* which accepts one input parameter (?).
oCommand = CREATEOBJECT("ADODB.Command")
oRecordSet = CREATEOBJECT("ADODB.Recordset")
WITH oCommand
* This command text string says:
* call SP byroyalty, which accepts one input parameter (?).
* removed comment
* remove ? = here for input parameter
.CommandText = "{call byroyalty (?)}"
.ActiveConnection = oConnection
.PARAMETERS.REFRESH
ENDWITH
* Specify the parameter
oCommand.PARAMETERS(0).VALUE = 40
oRecordSet = oCommand.Execute
=ShowRS()
WAIT WINDOW "Method 2 complete - press a key to continue"
** release rs and cmd
release oCommand
release oRecordset
************************************************************
*
* A third method to implement it.
* Create both parameters manually and append them to the
* parameters collection.
*
* The programmer has to know the binding information,
* but there's no performance hit as with method 2.
oCommand = CREATEOBJECT("ADODB.Command")
oRecordSet = CREATEOBJECT("ADODB.Recordset")
oParameters = CREATEOBJECT("ADODB.Parameter")
WITH oCommand
.commandtype = adCmdText
* removed ?= here for nonexistent input parm
.commandtext = "{call byroyalty (?)}"
* removed definition of input parameter
.PARAMETERS.APPEND (oCommand.CreateParameter("@percentage",;
adInteger, 1, 4, 40))
.ActiveConnection = oConnection
ENDWITH
oRecordSet = oCommand.Execute
=ShowRS()
WAIT WINDOW "Method 3 complete - press a key to continue"
* function ShowRs: Print the returned recordset on the desktop.
FUNCTION ShowRS()
oRecordSet.MoveFirst
? "Records returned: ", oRecordSet.RecordCount
* and print the au_id field values
DO WHILE ! oRecordSet.EOF
? oRecordSet.FIELDS("au_id").VALUE
oRecordSet.MoveNext
ENDDO
?
* End Code
The constants used were defined using the Microsoft Visual Basic 6.0 object
browser.
Additional query words: Query
Keywords: kbhowto kbsqlprog kbdatabase KB195047