I have the following classic asp working, calling a stored proc in the database. I would like to convert it over so instead of calling the stored proc it passes in the sql,
There is nothing wrong with the above code you are just missing using the Refresh() method of the Parameters
collection before trying to set the named parameter values.
Set cmd = server.createobject("ADODB.Command")
With
.ActiveConnection = theDatabase
.CommandType = adCmdStoredProc
.commandText = "AddResponse"
'Query the provider for the parameter details
Call .Parameters.Refresh()
.Parameters("@id") = id
.Parameters("@pc") = pc
.Parameters("@idDate") = now
Call .Execute(, , adExecuteNoRecords)
End With
set cmd = Nothing
If you don't want to use this method the parameter definitions have to come from somewhere so the other option is to define them yourself to reflect the definitions of the stored procedure.
Set cmd = server.createobject("ADODB.Command")
With cmd
.ActiveConnection = theDatabase
.CommandType = adCmdStoredProc
.commandText = "AddResponse"
'Define parameters manually
Call .Parameters.Append(.CreateParameter("@id", adVarWChar, adParamInput, 12))
Call .Parameters.Append(.CreateParameter("@pc", adVarWChar, adParamInput, 12))
Call .Parameters.Append(.CreateParameter("@idDate", adDBTimeStamp, adParamInput, 8))
.Parameters("@id") = id
.Parameters("@pc") = pc
.Parameters("@idDate") = now
Call .Execute(, , adExecuteNoRecords)
End With
set cmd = Nothing
If you do go down the manual route a great resource for identifying what ADO DataTypeEnum constants to use is Carl Prothman - Data Type Mapping
Side-note: You have this line in your Stored Procedure;
select * from EmailResponse
Which expects to return a resultset but you specify
adExecuteNoRecords
in yourADODB.Command
Execute()
method which causes this to be ignored, if you do want to return it adjust the above to be;Dim rs ... With cmd ... Set rs = .Execute() End With
...
is used to show where code is omitted
Needs pointing out that while @dimason approach (since removed, not sure why...) is sound it does over complicate things by adding two extra parameters when they are not needed, you can just declare the parameters inside the dynamic SQL and assign them to use those locally declared variables to run the statements instead.
Dim sql
sql = ""
sql = sql & "SET NOCOUNT ON;" & vbCrLf
sql = sql & "DECLARE @id NVARCHAR(12)" & vbCrLf
sql = sql & "DECLARE @pc NVARCHAR(12)" & vbCrLf
sql = sql & "DECLARE @idDate DATETIME" & vbCrLf
sql = sql & "SELECT @id = ?, @pc = ?, @idDate = ?" & vbCrLf
sql = sql & "SELECT * FROM EmailResponse;" & vbCrLf
sql = sql & "IF NOT EXISTS (SELECT id, projectCode FROM EmailResponse WHERE id = @id and projectCode = @pc)" & vbCrLf
sql = sql & "INSERT INTO EmailResponse (id, projectCode, dateEntered) VALUEs(@id, @pc, @idDate);"
Set cmd = server.createobject("ADODB.Command")
With cmd
.ActiveConnection = theDatabase
.CommandType = adCmdText
.CommandText = sql
.Prepared = true
.Parameters.Append cmd.CreateParameter("@id", adVarChar, adParamInput, 12, id)
.Parameters.Append cmd.CreateParameter("@pc", adVarChar, adParamInput, 12, pc)
.Parameters.Append cmd.CreateParameter("@idDate", adDBTimeStamp, adParamInput, 8, Now())
Set rsOut = .Execute()
End With
Set cmd = Nothing
Parameter
to avoid errors.To switch from a stored procedure to inline SQL, you need to change
cmd.CommandType = adCmdStoredProc
to
cmd.CommandType = adCmdText
Then you need to add the query to the command text property:
cmd.CommandText = "SELECT * FROM Orders WHERE CustomerID = ?"
The above line was derived from the Command Object Parameters example on MSDN.