Update 4
Updated the whole question to reflect my changes. Still Not Working.
This has been annoying me for two days now. I'm updating an old ordering interface system that our customers use, written in ASP Classic, VBScript. It connects to an SQL database on Windows Server 2003.
Stored Procedure
I have a stored procedure that returns a list of pallet codes, filtered by customer ID and searchable by pallet code:
CREATE PROCEDURE dbo.sp_PalletSearch
@CustomerRef Int,
@SearchQuery VarChar(15) = '%'
AS
SET NoCount On
SET @SearchQuery = '%' + COALESCE(@SearchQuery, '%') + '%'
SELECT p.PalletID,
p.PalletCode
FROM dbo.v_PalletSearch p
WHERE p.CustomerRef = @CustomerRef
AND p.PalletCode LIKE @SearchQuery
ORDER BY p.PalletCode ASC
SET NoCount Off
GO
This seems to work fine in SQL Query Analyzer with and without a search term:
exec sp_PalletSearch 100, ''
and exec sp_PalletSearch 100, 'PalletCode'
ASP Web Page
So onto the web page itself... This is the ADO Command I use to get the recordset and this is where my problem starts. It just simply will not return anything:
Dim strSearchQuery
strSearchQuery = "PalletCode"
Dim objCmd
Set objCmd = Server.CreateObject("ADODB.Command")
objCmd.ActiveConnection = cConn
objCmd.CommandType = adCmdStoredProc
objCmd.CommandText = "sp_PalletSearch"
objCmd.Parameters.Append objCmd.CreateParameter("@CustomerRef", adInteger, adParamInput)
objCmd.Parameters.Append objCmd.CreateParameter("@SearchQuery", adVarChar, adParamInput, 15)
objCmd.Parameters("@CustomerRef").Value = CustomerID
objCmd.Parameters("@SearchQuery").Value = strSearchQuery
Dim objRS
Set objRS = objCmd.Execute
Set objCmd = Nothing
Do While Not objRS.EOF
Response.Write(objRS("PalletID").Name & ": " & objRS("PalletID").Value & " | " & objRS("PalletCode").Name & ": " & objRS("PalletCode").Value & "<br>")
objRS.MoveNext
Loop
objRS.Close
Set objRS = Nothing
I Have Tried...
If I edit this line in my ADO Command:
objCmd.CommandText = "sp_PalletSearch"
And change it to:
objCmd.CommandText = "{call sp_PalletSearch(?, '" & strSearchQuery & "')}"
And remove:
objCmd.CommandType = adCmdStoredProc
All searching works fine. This is what I will stick to if a real solution isn't found.
If I edit the stored procedure to get the pallet code that equals the search term instead of LIKE, and comment out
--SET @SearchQuery = '%' + COALESCE(@SearchQuery, '%') + '%'
then I will get the exact match. This would tell me that the ADO Command is passing the parameters ok. But then why won't the stored procedure get results LIKE the @SearchQuery
?
Another thing to note is that replacing the ADO Command with the following works fine with pallet code LIKE. I don't see this snippet as a secure option, please tell me if I'm wrong. I would rather use the parametrised command:
strSQL = "EXECUTE sp_PalletSearch " & CustomerID & ", '" & strSearchQuery & "' "
Set objRS = Server.CreateObject("ADODB.Recordset")
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open cConn
objRS.Open strSQL, objConn
It's a big ask, but I like to do things efficiently and correctly, and I love to learn. I hope you guys can help me with this puzzle.