ADO command parameter does not jump to stored procedure or stored procedure "Ignore" parameter

Update 4

Updated the whole question to reflect my changes. Still not working.

This has annoyed me for two days now. I am updating an old ordering interface system our clients use written in ASP Classic, VBScript. It connects to 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 can be found 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 search term:

exec sp_PalletSearch 100, ''

and exec sp_PalletSearch 100, 'PalletCode'

ASP Web Page

So on the webpage itself ... This is the ADO command I use to get the recordset and this is where my problem begins. It just won't 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 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 search jobs are fine. This is what I will stick with if no real solution is found.


If I edit the stored procedure to get the pallet code which is equal to the search term and not LIKE and comment out

--SET @SearchQuery = '%' + COALESCE(@SearchQuery, '%') + '%'

      

then I will get an exact match. This will tell me that the ADO command is passing parameters ok. But why won't the stored procedure get LIKE results @SearchQuery

?


Another thing to note is that replacing the ADO command with the following works fine with the LIKE pallet code. I don't see this snippet as a safe option, please tell me I'm wrong. I would rather use a parameterized 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  

      


This is a big question, but I enjoy doing things effectively and correctly, and I love learning. Hope you guys can help me with this puzzle.

+3


source to share


3 answers


solved

Thanks to Bond and especially Lankimart for your help. Lankymart, your suggestion to use SQL Profiler helped. My server has an earlier version, I think Profiler.

I found this when looking at the Profiler trace: @SearchQuery = 'bww100052 '

So, I decided to force Trim inside the stored procedure: LTRIM(RTRIM(@SearchQuery))

Stored procedure



CREATE PROCEDURE dbo.sp_PalletSearch
@CustomerRef        Int,
@SearchQuery        VarChar(15) = '%'
AS    

SET NoCount On  
SET @SearchQuery = '%' + COALESCE(LTRIM(RTRIM(@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

      

ADO Team

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 = Trim(strSearchQuery)

Dim objRS
Set objRS = objCmd.Execute
Set objCmd = Nothing

      

Finally

I thought I could never solve this problem, it just doesn't make any sense! I'll do some more tests, but it looks like the variable needed to be truncated. I don't know why the extra space was added.

+1


source


I think you are causing more problems by trying anything and everything. With every try, you make small mistakes in your syntax (e.g. quotes in the wrong place, omitting CommandType

, etc.).

If it helps, this is how I will code for this stored procedure



Dim cmd, rs, sql
Dim data, rows, row

Set cmd = Server.CreateObject("ADODB.Command")
'Name of your stored procedure
sql = "dbo.sp_PalletSearch"

With cmd
  .ActiveConnection = cConn 'Assuming cConn is a connection string variable
  .CommandType = adCmdStoredProc
  .CommandText = sql
  'Define Stored Procedure parameters
  Call .Parameters.Append(.CreateParameter("@CustomerRef", adInteger, adParamInput, 4))
  Call .Parameters.Append(.CreateParameter("@SearchQuery", adVarChar, adParamInput, 15))
  'First parameter is optional so only pass if we have a value, will default to NULL.
  If Len(CustomerId) > 0 Then .Parameters("@CustomerRef").Value = CustomerID
  .Parameters("@SearchQuery").Value = strSearchQuery
  Set rs = .Execute()

  'Populate 2D-Array with data from Recordset
  If Not rs.EOF Then data = rs.GetRows()

  'Close and Release Recordset from memory
  Call rs.Close()
  Set rs = Nothing
End With
Set cmd = Nothing

If IsArray(data) Then
  rows = UBound(data, 2)
  For row = 0 To rows
    Call Response.Write("Pallet Id: " & data(0, row) & " | Pallet Code: " & data(1, row) & "</ br>")
  Next
End If

      

+1


source


Try with

objCmd.Parameters.Append objCmd.CreateParameter("@SearchQuery", adVarChar, adParamInput, 15, "'PalletCode'")

      

Note that "PalletCode" has an extra set of single quotes inside it.

-1


source







All Articles