ADO Command Parameter Not Passing to Stored Procedure or Stored Procedure 'Ignoring' Parameter

后端 未结 3 1690
醉话见心
醉话见心 2021-01-19 16:48

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 int

相关标签:
3条回答
  • 2021-01-19 17:30

    Solved

    Thank you to Bond and especially Lankymart for your help. Lankymart, your suggestion to use SQL Profiler helped. My server has the older version I guess - Profiler.

    I found this when looking in the Profiler Trace: @SearchQuery = 'bww100052 '

    So I decided to force a 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 Command

    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 would never solve this one, it was just making no sense at all! I'll throw a few more tests at it, but it looks like trimming the variable was needed. I don't know why the extra space was added though.

    0 讨论(0)
  • 2021-01-19 17:47

    I think you causing yourself more issues by trying anything and everything. With each attempt you make slight mistakes in your syntax (like quotes in the wrong place, not specifying a CommandType etc).

    If it helps this is how I would code for that 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
    
    0 讨论(0)
  • 2021-01-19 17:51

    Try it with

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

    Notice that "'PalletCode'" has an additional set of single quotes inside of it.

    0 讨论(0)
提交回复
热议问题