Declare parameterized query in markup - sql-server

I'm trying to convert a SQL Server 2008 R2 Express query in ASP-Classic that is not parameterized so I did the following (see code below) but I keep getting an error saying:
Object doesn't support this property or method: 'Parameters'
Can someone please tell me what am I doing wrong!
Thanks!
<%
' OPEN DATABASE
dim objConn, objRS, objTRS, objUnit
dim strConnection
set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Driver={SQL Server};Server=MSSQLSrv;Database=DbTest;UID=blablabala;PWD=blablabala"
objConn.Open strConnection
set objRS = Server.CreateObject("ADODB.Recordset")
set objRS.ActiveConnection = objConn
strQuery = "SELECT USERNAME,PASSWORD from CUSTOMERS where EMAIL=?"
objRS.Parameters(0) = Request.QueryString("email")
objRS.Open strQuery
%>

If you are trying make it a parameter query, then you need to create the command object and parameters first. For example:
' create your command object
Const adCmdText = &H0001
Set objCmd = Server.CreateObject("ADODB.Command")
objCmd.ActiveConnection = YourConnectionString
objCmd.CommandType = adCmdText ' Evaluate as textual definition, not stored procedure
'now create query and add parameters
strQuery = "SELECT USERNAME,PASSWORD from CUSTOMERS where EMAIL=?"
objCmd.CommandText=strQuery
objCmd.Parameters.Append = objCmd.CreateParameter("ParameterName", ParameterType, adParamInput, parameterSize, ParameterValue)
SET objRS = objCmd.execute(strSQL)
Set objCmd=Nothing

The parameters collection is on the command object not the recordset.
Here is some VB6 code that works, the parameter name is not important as parameter order is all that counts.
Dim rst As Recordset
Dim cmd As ADODB.Command
Set cmd = New Command
With cmd
.CommandText = "SELECT USERNAME,PASSWORD from CUSTOMERS where EMAIL=?" ' this proc also returns factor info
.CommandType = adCmdText
Set .ActiveConnection = objconn
.Parameters.Append .CreateParameter("#Email", adVarChar, adParamInput, 50, "Email")
Set rst = cmd.Execute
End With

You can do something like this:
' OPEN DATABASE
dim objConn,objRS,objTRS,objUnit
Const adCmdText = &H0001
Set objConn = Server.CreateObject("ADODB.Command")
objConn.ActiveConnection = "Driver={SQL Server};Server=MSSQLSrv;Database=DbTest;UID=blablabala;PWD=blablabala"
objConn.CommandType = adCmdText
strQuery = "SELECT USERNAME,PASSWORD from CUSTOMERS where EMAIL=?"
objConn.CommandText=strQuery
objConn.Parameters(0) = Request.QueryString("email")
SET objRS = objConn.execute(strQuery)

Related

Run SQL Server stored procedure from VBA

This is my stored procedure which works fine in SQL Server Management Studio.
exec GroupCommissions #GroupNumberEntry = '01142'
Should produce a table of data.
I'm trying to run it in vba using the following code:
Dim rs As ADODB.Recordset
Dim cnSQL As ADODB.Connection
Dim sqlcommand As ADODB.Command, prm As Object
Set cnSQL = New ADODB.Connection
cnSQL.Open "Provider=SQLOLEDB; Data Source=bddc1didw1;Initial Catalog=Actuarial; Trusted_connection=Yes; Integrated Security='SSPI'"
Set sqlcommand = New ADODB.Command
sqlcommand.ActiveConnection = cnSQL
sqlcommand.CommandType = adCmdStoredProc
sqlcommand.CommandText = "GroupCommissions"
Set prm = sqlcommand.CreateParameter("GroupNumberEntry", adParamInput)
sqlcommand.Parameters.Append prm
sqlcommand.Parameters("GroupNumberEntry").Value = "01142"
Set rs = New ADODB.Recordset
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open sqlcommand
ActiveSheet.Range("a3").CopyFromRecordset rs
But it just returns blank and I can't work out what I'm doing wrong. Also is there a simpler way to do this?
As discussed below i've managed to fix the issue by adding SET NOCOUNT ON to the original stored procedure. My issue now is I want to do a second stored procedure in the same code but it only seems to work for one. They both work individually however. So either I have to reopen the connection or use 2 on the defined variables? Here is the code:
Dim rs As ADODB.Recordset
Dim cnSQL As ADODB.Connection
Dim sqlcommand As ADODB.Command, prm As Object, prm2 As Object
Set cnSQL = New ADODB.Connection
cnSQL.Open "Provider=SQLOLEDB; Data Source=bddc1didw1;Initial Catalog=Actuarial; Trusted_connection=Yes; Integrated Security='SSPI'"
Set sqlcommand = New ADODB.Command
sqlcommand.ActiveConnection = cnSQL
'groupdates
sqlcommand.CommandType = adCmdStoredProc
sqlcommand.CommandText = "GroupDate"
Set prm = sqlcommand.CreateParameter("GroupNumberEntry", adVarChar, adParamInput, 5)
Set prm2 = sqlcommand.CreateParameter("ValuationDateEntry", adDate, adParamInput)
sqlcommand.Parameters.Append prm
sqlcommand.Parameters.Append prm2
sqlcommand.Parameters("GroupNumberEntry").Value = "01132"
sqlcommand.Parameters("ValuationDateEntry").Value = "08-31-2019"
Set rs = New ADODB.Recordset
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open sqlcommand
ActiveSheet.Range("a2").CopyFromRecordset rs
'GroupCommissions
sqlcommand.CommandType = adCmdStoredProc
sqlcommand.CommandText = "GroupCommissions"
Set prm = sqlcommand.CreateParameter("GroupNumberEntry", adVarChar, adParamInput, 5)
sqlcommand.Parameters.Append prm
sqlcommand.Parameters("GroupNumberEntry").Value = "01132"
Set rs = New ADODB.Recordset
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open sqlcommand
ActiveSheet.Range("DB2").CopyFromRecordset rs
Try replacing that line with something like this:
Set prm = sqlcommand.CreateParameter("GroupNumberEntry", adVarChar, GroupNumberEntry, 255)
Set the field type and length according to how your proc is defined.
Your code looked OK to me so I copied it into Excel (2016...) and tried it. It gave me an error on that line but adding the additional parameter values to CreateParameter fixed the issue. shrug It shouldn't matter since those are optional parameters, unless there is something maybe at the provider level.
you can try just sending the SQL PROCEDURE straight through as a CALL function.
Take a look at this:
Public connDB As New ADODB.Connection
Public rs As New ADODB.Recordset
Public strSQL As String
Public strConnectionstring As String
Public strServer As String
Public strDBase As String
Public strUser As String
Public strPwd As String
Public PayrollDate As String
Sub WriteStoredProcedure()
PayrollDate = "2017/05/25"
Call ConnectDatabase
On Error GoTo errSP
strSQL = "EXEC spAgeRange '" & PayrollDate & "'"
connDB.Execute (strSQL)
Exit Sub
errSP:
MsgBox Err.Description
End Sub
Sub ConnectDatabase()
If connDB.State = 1 Then connDB.Close
On Error GoTo ErrConnect
strServer = "SERVERNAME" ‘The name or IP Address of the SQL Server
strDBase = "TestDB"
strUser = "" 'leave this blank for Windows authentication
strPwd = ""
If strPwd > "" Then
strConnectionstring = "DRIVER={SQL Server};Server=" & strServer & ";Database=" & strDBase & ";Uid=" & strUser & ";Pwd=" & strPwd & ";Connection Timeout=30;"
Else
strConnectionstring = "DRIVER={SQL Server};SERVER=" & strServer & ";Trusted_Connection=yes;DATABASE=" & strDBase 'Windows authentication
End If
connDB.ConnectionTimeout = 30
connDB.Open strConnectionstring
Exit Sub
ErrConnect:
MsgBox Err.Description
End Sub

VBA connecting to SQL Server

I am having trouble when connecting VBA to SQL Server:
Sub ConnectSQLServer()
Dim cmd As ADODB.Command
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConn As String
Dim par As ADODB.Parameter
Dim strSQL As String
strConn = "DRIVER=SQL Server;SERVER=CHU-AS-0004;DATABASE=RTC_LaplaceD_DEV;Trusted_Connection=Yes;"
Set conn = New ADODB.Connection
conn.Open strConn
Set cmd = New ADODB.Command
cmd.CommandText = "dbo.Version"
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = conn
rs.Open = "SELECT * FROM [dbo].[Version]"
cmd.Execute rs
Set conn = Nothing
Set cmd = Nothing
sConnString = ""
End Sub
I just want to select all values from the table named [dbo].[Version], but when I execute it, I get an error:
Compile error: Expected Function or Variable'
and the line with rs.Open is highlighted.
Would you help me to solve this problem?
Open is a method, and cannot be set with an =.
You probably want something like this:
rs.Open "SELECT * FROM [dbo].[Version]", conn
Once you have called Open, then you can iterate through the Recordset's rows using the EOF property and the MoveNext method:
Do While Not rs.EOF
Debug.Print rs.Fields(1)
'You could also use rs!FieldName, where FieldName is the name of a column
Loop
Change your code to the following:
Set cmd = New ADODB.Command
cmd.CommandText = "SELECT * FROM [dbo].[Version]"
cmd.CommandType = adCmdText
cmd.ActiveConnection = conn
Set rs = cmd.Execute
Do While Not rs.EOF
'do something with rs.Fields(0) '
rs.MoveNext
Loop
Set conn = Nothing
Set cmd = Nothing
rs.Fields is a zero based collection that means that the first field is 0, not 1.
You can get subsequent fields by rs.Fields(1), rs.Fields(2) etc. Or you can use Field names, with the syntax rs.Fields("MyFieldName").
Note when using a string command ("SELECT * FROM ..") the CommandType is adCmdText. The syntax for the Recordset is SET rs = cmd.Execute. Also you must invoke MoveNext in your loop, otherwise you get stuck in the loop!

Execute SQL Server stored procedure through VBA which does not return rows

How to execute special SQL Server stored procedure through Excel VBA? Special because it does not return rows like this procedure:
select * from tab1
How to execute the procedure which let's say prepares reports, like this one:
select *
into tab2
from tab1
which does not return any rows.
I am using this VBA code:
Dim cnn As Object
Set cnn = CreateObject("ADODB.Connection")
Dim cmd As Object
Set cmd = CreateObject("ADODB.Command")
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
Dim ConnectionString As String
cnn.ConnectionString = "driver={SQL Server};server=MYSERVERNAME;Database=MYDATABASE;Trusted_Connection=yes;"
cnn.Open
Sql = "EXEC [dbo].[Procedure_make_report] 'param1'"
Set rs = cnn.Execute(Sql)
I get the error:
Runtime error 2147217900
I found a clue here:
http://www.vbforums.com/showthread.php?541498-RESOLVED-runtime-error-2147217900-(80040e14)-incorrect-syntax-error
but I do not know what action query is.
Based on comments of Rory and Tom, I leave the answer which works.
Dim cnn As Object
Set cnn = CreateObject("ADODB.Connection")
Dim cmd As Object
Set cmd = CreateObject("ADODB.Command")
cmd.CommandType = 1 ' adCmdText
cmd.CommandTimeout = 120 ' number of seconds to time out
Dim ConnectionString As String
cnn.ConnectionString = "driver={SQL Server};server=MYSERVERNAME;Database=MYDATABASE;Trusted_Connection=yes;"
cnn.Open
Dim SQL As String
SQL = "EXEC [dbo].[Procedure_make_report] 'param1'"
cnn.Open
cmd.CommandText = SQL
cmd.ActiveConnection = cnn
cmd.Execute
If Not cnn Is Nothing Then
cnn.Close
Set cnn = Nothing
End If

ADO Parameterized Query: Cannot set CursorType/CursorLocation

I'm trying to write a function in ASP classic to do a safe parameterized query on a SQL Server database using ADO, and I want to return a recordset that is not forward only.
I can't find any guidance on how to use both parameters AND to control the cursor type/location.
The error I get is
Command text was not set for the command object
Here is the code I have so far:
sub ParameterizedSQL2(ByRef RS, strSQL, param1)
Set DB_Conn = Server.CreateObject("ADODB.Connection")
SET rs = Server.CreateObject("ADODB.RecordSet")
DB_Conn.Open Application("ConnectionString")
Set objCmd = Server.CreateObject("ADODB.Command")
ObjCmd.ActiveConnection = DB_Conn
Set objParam = objCmd.CreateParameter("#PAR", adVarChar, adParamInput, 250)
objCmd.Parameters.Append(objParam)
objCmd.Parameters("#PAR") = param1
objCmd.CommandType = adCmdText
ObjCmd.CommandText = strSQL 'The paramterized query of the form [SELECT * from Table WHERE Field=?]
rs.Open objCMD, , adOpenStatic, adLockOptimistic
Set DB_Conn = nothing
Set objCmd = nothing
Set objParam = nothing
End sub

Bind Access form to the results from a Stored Procedure

I am trying to return the results of a stored procedure to a form. I have managed to iterate thru the results using an ADO recordset, but cannot bind the results to the form..
Here is the VBA code:
Private Sub RetrieveSiteInformation()
Dim cmd As New ADODB.Command
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset, f As ADODB.Field
With cnn
.Provider = "SQLOLEDB"
.ConnectionString =
"data source=UKFCSVR;initial catalog=ACACB;Trusted_Connection=Yes"
.Open
End With
Dim param1 As ADODB.Parameter
If Nz(txtSiteID_Search.Value, vbNullString) <> vbNullString Then
Set param1 = cmd.CreateParameter("#SiteID", adBigInt, adParamInput)
param1.Value = txtSiteID_Search.Value
cmd.Parameters.Append param1
End If
With cmd
.ActiveConnection = cnn
.CommandText = "spSiteInformation_Retrieve"
.CommandType = adCmdStoredProc
**' THIS FAILS**
Me.Recordset = .Execute
**' THIS LOOP WORKS FINE**
' Set rs = .Execute
' rs.MoveFirst
' For Each f In rs.Fields
' Debug.Print f.Name
' Next
' With rs
' Do While Not .EOF
' Debug.Print ![CompanyName] & " " & ![Postcode]
' .MoveNext
' Loop
' End With
End With
cnn.Close
End Sub
Okay, I have tested this example. It includes changes to suit my set-up which I have left in, rather than guessing at your set-up. Most of this is taken from http://support.microsoft.com/kb/281998/EN-US/
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim param1 As New ADODB.Parameter
With cn
.Provider = "Microsoft.Access.OLEDB.10.0"
.Properties("Data Provider").Value = "SQLOLEDB"
.Properties("Data Source").Value = "Server"
.Properties("Integrated Security").Value = "SSPI"
.Properties("Initial Catalog").Value = "Test"
.Open
End With
txtSiteID_Search = 1
If Nz(txtSiteID_Search, vbNullString) <> vbNullString Then
Set param1 = cmd.CreateParameter("#SiteID", adBigInt, adParamInput)
param1.Value = txtSiteID_Search
cmd.Parameters.Append param1
End If
With cmd
.ActiveConnection = cn
.CommandText = "spSiteInformation_Retrieve"
.CommandType = adCmdStoredProc
Set Me.Recordset = .Execute
End With
Forget ADO. Create a passthru query in Access, with property ReturnsRecords = True.
Bind your form to that passthru query.
Using VBA, change the .SQL property of that QueryDef object, then open the form. You're done.
Set qry = CurrentDb.QueryDefs("myQryDef")
qry.SQL = "exec spMyStoredProc " & "'argument1'"
You need to use Set whenever you assign an object reference in VBA.
Change Me.Recordset = .Execute to Set Me.Recordset = .Execute.
Also, you probably need to open it with a supported cursor type. I don't think there's a way to change the cursor type if you use the Execute method on the Command object. You'll have to create the Recordset separately.
Set rs = New ADODB.Recordset
rs.Open cmd, , adOpenKeyset
Set Me.Recordset = rs

Resources