using 'Like'...wierd results

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default using 'Like'...wierd results

    I'm calling a stored proc in SQL server from an ASP
    page. I'm not getting all the records when I call from
    the ASP page, but if I run in SQL Analyzer I get the
    correct number. Can someone help point out my mistake?

    Below is my code that I'm calling from ASP page.
    ASP Code:
    lcSearchPhrase = Request.QueryString("searchfor")
    if not isempty(lcSearchPhrase) then
    'lcSearchPhrase = "%"+lcSearchPhrase+"%"
    lcDisplayResults = "Y"
    Set oConn = Server.CreateObject("ADODB.Connection")
    oConn.Open Session("strConn")
    Set oCmd = Server.CreateObject("ADODB.Command")
    set oCmd.ActiveConnection = oConn
    oCmd.CommandText = "spSearchProducts"
    oCmd.commandtype = AdCmdStoredProc
    oCmd.Parameters.Append oCmd.CreateParameter
    ("@SearchField", adChar, adParamInput,50)
    oCmd.Parameters("@SearchField") = lcSearchPhrase
    set oRsSR = oCmd.Execute
    if oRsSR.EOF then
    lcDisplayResults = "N"
    end if
    set oCmd.ActiveConnection = nothing
    set oCmd = nothing
    end if


    SQL Code:
    PROCEDURE spSearchProducts @SearchField varchar (50) AS

    select qcnproduct.productid, qcnproduct.productname,
    qcnproduct.prodpicture,
    qcnproditem.qcnprice,
    qcnproditem.supplycount,qcnproditem.typeid,
    qcnsupplytype.typedesc
    from qcnproduct
    inner join qcnproditem
    inner join qcnsupplytype on
    qcnproditem.typeid = qcnsupplytype.typeid
    on qcnproduct.productid = qcnproditem.productid
    where qcnproduct.productname like @SearchField
    or qcnproduct.proddesc like @SearchField
    or qcnproduct.miscfacts like @SearchField
    order by qcnproduct.productname, qcnproduct.productid,
    qcnproditem.supplycount

    My problem is that if the var I send from ASP isn't in
    the productname field, the result set is empty...but if I
    run this in SQL Query Analyzer it will return the corect
    record set??????

    TIA
    -Bruce
    Bruce Duncan Guest

  2. Similar Questions and Discussions

    1. Complex join = no results (for a query that shouldreturn results)
      I am trying to use a rather complex join that returns many results when the SQL server is queried directly, but returns no results when used on a...
    2. Outputting query results to excel.. wierd!!
      OK, this is different to my previous posting, so I thought it best to post another to save confusion. I am having some really wierd things...
    3. Query results don't display properly in results table.IGNORE PREVIOUS
      :disgust; I need to display the results of a query. The query runs properly. My problem is having specific results display in specific locations in...
    4. Query results don't display properly in results table.
      :disgust; I need to display the results of a query. The query runs properly. My problem is having specific results display in specific locations in...
    5. Wierd Verity search results
      Using CFMX 6.1. I used the CF administrator to create and indes a collection of PDF files. The search works fine, but when I display the summary,...
  3. #2

    Default Re: using 'Like'...wierd results

    You need to include single quotes. So that your query ends up

    LIKE '%something%'

    rather than

    LIKE %something% or LIKE something

    --
    Aaron Bertrand
    SQL Server MVP
    [url]http://www.aspfaq.com/[/url]




    "Bruce Duncan" <anonymous@discussions.microsoft.com> wrote in message
    news:1482e01c3f8c9$7bcdddb0$a501280a@phx.gbl...
    > I'm calling a stored proc in SQL server from an ASP
    > page. I'm not getting all the records when I call from
    > the ASP page, but if I run in SQL Analyzer I get the
    > correct number. Can someone help point out my mistake?
    >
    > Below is my code that I'm calling from ASP page.
    > ASP Code:
    > lcSearchPhrase = Request.QueryString("searchfor")
    > if not isempty(lcSearchPhrase) then
    > 'lcSearchPhrase = "%"+lcSearchPhrase+"%"
    > lcDisplayResults = "Y"
    > Set oConn = Server.CreateObject("ADODB.Connection")
    > oConn.Open Session("strConn")
    > Set oCmd = Server.CreateObject("ADODB.Command")
    > set oCmd.ActiveConnection = oConn
    > oCmd.CommandText = "spSearchProducts"
    > oCmd.commandtype = AdCmdStoredProc
    > oCmd.Parameters.Append oCmd.CreateParameter
    > ("@SearchField", adChar, adParamInput,50)
    > oCmd.Parameters("@SearchField") = lcSearchPhrase
    > set oRsSR = oCmd.Execute
    > if oRsSR.EOF then
    > lcDisplayResults = "N"
    > end if
    > set oCmd.ActiveConnection = nothing
    > set oCmd = nothing
    > end if
    >
    >
    > SQL Code:
    > PROCEDURE spSearchProducts @SearchField varchar (50) AS
    >
    > select qcnproduct.productid, qcnproduct.productname,
    > qcnproduct.prodpicture,
    > qcnproditem.qcnprice,
    > qcnproditem.supplycount,qcnproditem.typeid,
    > qcnsupplytype.typedesc
    > from qcnproduct
    > inner join qcnproditem
    > inner join qcnsupplytype on
    > qcnproditem.typeid = qcnsupplytype.typeid
    > on qcnproduct.productid = qcnproditem.productid
    > where qcnproduct.productname like @SearchField
    > or qcnproduct.proddesc like @SearchField
    > or qcnproduct.miscfacts like @SearchField
    > order by qcnproduct.productname, qcnproduct.productid,
    > qcnproditem.supplycount
    >
    > My problem is that if the var I send from ASP isn't in
    > the productname field, the result set is empty...but if I
    > run this in SQL Query Analyzer it will return the corect
    > record set??????
    >
    > TIA
    > -Bruce

    Aaron Bertrand [MVP] Guest

  4. #3

    Default Re: using 'Like'...wierd results

    I tried using this line below:
    lcSearchPhrase = "'%"+lcSearchPhrase+"%'"

    and passing lcSearchPhrase to the SP but I'm still not
    getting the results???

    -Bruce

    >-----Original Message-----
    >You need to include single quotes. So that your query
    ends up
    >
    >LIKE '%something%'
    >
    >rather than
    >
    >LIKE %something% or LIKE something
    >
    >--
    >Aaron Bertrand
    >SQL Server MVP
    >[url]http://www.aspfaq.com/[/url]
    >
    >
    >
    >
    >"Bruce Duncan" <anonymous@discussions.microsoft.com>
    wrote in message
    >news:1482e01c3f8c9$7bcdddb0$a501280a@phx.gbl...
    >> I'm calling a stored proc in SQL server from an ASP
    >> page. I'm not getting all the records when I call from
    >> the ASP page, but if I run in SQL Analyzer I get the
    >> correct number. Can someone help point out my mistake?
    >>
    >> Below is my code that I'm calling from ASP page.
    >> ASP Code:
    >> lcSearchPhrase = Request.QueryString("searchfor")
    >> if not isempty(lcSearchPhrase) then
    >> 'lcSearchPhrase = "%"+lcSearchPhrase+"%"
    >> lcDisplayResults = "Y"
    >> Set oConn = Server.CreateObject
    ("ADODB.Connection")
    >> oConn.Open Session("strConn")
    >> Set oCmd = Server.CreateObject("ADODB.Command")
    >> set oCmd.ActiveConnection = oConn
    >> oCmd.CommandText = "spSearchProducts"
    >> oCmd.commandtype = AdCmdStoredProc
    >> oCmd.Parameters.Append oCmd.CreateParameter
    >> ("@SearchField", adChar, adParamInput,50)
    >> oCmd.Parameters("@SearchField") = lcSearchPhrase
    >> set oRsSR = oCmd.Execute
    >> if oRsSR.EOF then
    >> lcDisplayResults = "N"
    >> end if
    >> set oCmd.ActiveConnection = nothing
    >> set oCmd = nothing
    >> end if
    >>
    >>
    >> SQL Code:
    >> PROCEDURE spSearchProducts @SearchField varchar (50) AS
    >>
    >> select qcnproduct.productid, qcnproduct.productname,
    >> qcnproduct.prodpicture,
    >> qcnproditem.qcnprice,
    >> qcnproditem.supplycount,qcnproditem.typeid,
    >> qcnsupplytype.typedesc
    >> from qcnproduct
    >> inner join qcnproditem
    >> inner join qcnsupplytype on
    >> qcnproditem.typeid = qcnsupplytype.typeid
    >> on qcnproduct.productid = qcnproditem.productid
    >> where qcnproduct.productname like @SearchField
    >> or qcnproduct.proddesc like @SearchField
    >> or qcnproduct.miscfacts like @SearchField
    >> order by qcnproduct.productname, qcnproduct.productid,
    >> qcnproditem.supplycount
    >>
    >> My problem is that if the var I send from ASP isn't in
    >> the productname field, the result set is empty...but
    if I
    >> run this in SQL Query Analyzer it will return the
    corect
    >> record set??????
    >>
    >> TIA
    >> -Bruce
    >
    >
    >.
    >
    Guest

  5. #4

    Default Re: using 'Like'...wierd results

    I also tried this in my SQL SP:

    CREATE PROCEDURE spSearchProducts @SearchField varchar
    (50) AS

    declare @SearchFld varchar (50)
    set @SearchFld = "'%"+@SearchField+"'%"

    select qcnproduct.productid, qcnproduct.productname,
    qcnproduct.prodpicture,
    qcnproditem.qcnprice,
    qcnproditem.supplycount,qcnproditem.typeid,
    qcnsupplytype.typedesc
    from qcnproduct
    inner join qcnproditem
    inner join qcnsupplytype on
    qcnproditem.typeid = qcnsupplytype.typeid
    on qcnproduct.productid = qcnproditem.productid
    where qcnproduct.productname like @SearchFld
    or qcnproduct.proddesc like @SearchFld
    or qcnproduct.miscfacts like @SearchFld
    order by qcnproduct.productname, qcnproduct.productid,
    qcnproditem.supplycount

    This didn't work either...man...I know this should be
    easy.

    -Bruce Duncan
    >-----Original Message-----
    >You need to include single quotes. So that your query
    ends up
    >
    >LIKE '%something%'
    >
    >rather than
    >
    >LIKE %something% or LIKE something
    >
    >--
    >Aaron Bertrand
    >SQL Server MVP
    >[url]http://www.aspfaq.com/[/url]
    >
    >
    >
    >
    >"Bruce Duncan" <anonymous@discussions.microsoft.com>
    wrote in message
    >news:1482e01c3f8c9$7bcdddb0$a501280a@phx.gbl...
    >> I'm calling a stored proc in SQL server from an ASP
    >> page. I'm not getting all the records when I call from
    >> the ASP page, but if I run in SQL Analyzer I get the
    >> correct number. Can someone help point out my mistake?
    >>
    >> Below is my code that I'm calling from ASP page.
    >> ASP Code:
    >> lcSearchPhrase = Request.QueryString("searchfor")
    >> if not isempty(lcSearchPhrase) then
    >> 'lcSearchPhrase = "%"+lcSearchPhrase+"%"
    >> lcDisplayResults = "Y"
    >> Set oConn = Server.CreateObject
    ("ADODB.Connection")
    >> oConn.Open Session("strConn")
    >> Set oCmd = Server.CreateObject("ADODB.Command")
    >> set oCmd.ActiveConnection = oConn
    >> oCmd.CommandText = "spSearchProducts"
    >> oCmd.commandtype = AdCmdStoredProc
    >> oCmd.Parameters.Append oCmd.CreateParameter
    >> ("@SearchField", adChar, adParamInput,50)
    >> oCmd.Parameters("@SearchField") = lcSearchPhrase
    >> set oRsSR = oCmd.Execute
    >> if oRsSR.EOF then
    >> lcDisplayResults = "N"
    >> end if
    >> set oCmd.ActiveConnection = nothing
    >> set oCmd = nothing
    >> end if
    >>
    >>
    >> SQL Code:
    >> PROCEDURE spSearchProducts @SearchField varchar (50) AS
    >>
    >> select qcnproduct.productid, qcnproduct.productname,
    >> qcnproduct.prodpicture,
    >> qcnproditem.qcnprice,
    >> qcnproditem.supplycount,qcnproditem.typeid,
    >> qcnsupplytype.typedesc
    >> from qcnproduct
    >> inner join qcnproditem
    >> inner join qcnsupplytype on
    >> qcnproditem.typeid = qcnsupplytype.typeid
    >> on qcnproduct.productid = qcnproditem.productid
    >> where qcnproduct.productname like @SearchField
    >> or qcnproduct.proddesc like @SearchField
    >> or qcnproduct.miscfacts like @SearchField
    >> order by qcnproduct.productname, qcnproduct.productid,
    >> qcnproditem.supplycount
    >>
    >> My problem is that if the var I send from ASP isn't in
    >> the productname field, the result set is empty...but
    if I
    >> run this in SQL Query Analyzer it will return the
    corect
    >> record set??????
    >>
    >> TIA
    >> -Bruce
    >
    >
    >.
    >
    Guest

  6. #5

    Default Re: using 'Like'...wierd results

    > set @SearchFld = "'%"+@SearchField+"'%"

    Why are you using double quotes here?

    Try

    SET @SearchFld = '%' + @SearchField + '%'
    ....
    WHERE something LIKE @SearchFld

    --
    Aaron Bertrand
    SQL Server MVP
    [url]http://www.aspfaq.com/[/url]



    >
    > select qcnproduct.productid, qcnproduct.productname,
    > qcnproduct.prodpicture,
    > qcnproditem.qcnprice,
    > qcnproditem.supplycount,qcnproditem.typeid,
    > qcnsupplytype.typedesc
    > from qcnproduct
    > inner join qcnproditem
    > inner join qcnsupplytype on
    > qcnproditem.typeid = qcnsupplytype.typeid
    > on qcnproduct.productid = qcnproditem.productid
    > where qcnproduct.productname like @SearchFld
    > or qcnproduct.proddesc like @SearchFld
    > or qcnproduct.miscfacts like @SearchFld
    > order by qcnproduct.productname, qcnproduct.productid,
    > qcnproditem.supplycount
    >
    > This didn't work either...man...I know this should be
    > easy.
    >
    > -Bruce Duncan
    >
    > >-----Original Message-----
    > >You need to include single quotes. So that your query
    > ends up
    > >
    > >LIKE '%something%'
    > >
    > >rather than
    > >
    > >LIKE %something% or LIKE something
    > >
    > >--
    > >Aaron Bertrand
    > >SQL Server MVP
    > >[url]http://www.aspfaq.com/[/url]
    > >
    > >
    > >
    > >
    > >"Bruce Duncan" <anonymous@discussions.microsoft.com>
    > wrote in message
    > >news:1482e01c3f8c9$7bcdddb0$a501280a@phx.gbl...
    > >> I'm calling a stored proc in SQL server from an ASP
    > >> page. I'm not getting all the records when I call from
    > >> the ASP page, but if I run in SQL Analyzer I get the
    > >> correct number. Can someone help point out my mistake?
    > >>
    > >> Below is my code that I'm calling from ASP page.
    > >> ASP Code:
    > >> lcSearchPhrase = Request.QueryString("searchfor")
    > >> if not isempty(lcSearchPhrase) then
    > >> 'lcSearchPhrase = "%"+lcSearchPhrase+"%"
    > >> lcDisplayResults = "Y"
    > >> Set oConn = Server.CreateObject
    > ("ADODB.Connection")
    > >> oConn.Open Session("strConn")
    > >> Set oCmd = Server.CreateObject("ADODB.Command")
    > >> set oCmd.ActiveConnection = oConn
    > >> oCmd.CommandText = "spSearchProducts"
    > >> oCmd.commandtype = AdCmdStoredProc
    > >> oCmd.Parameters.Append oCmd.CreateParameter
    > >> ("@SearchField", adChar, adParamInput,50)
    > >> oCmd.Parameters("@SearchField") = lcSearchPhrase
    > >> set oRsSR = oCmd.Execute
    > >> if oRsSR.EOF then
    > >> lcDisplayResults = "N"
    > >> end if
    > >> set oCmd.ActiveConnection = nothing
    > >> set oCmd = nothing
    > >> end if
    > >>
    > >>
    > >> SQL Code:
    > >> PROCEDURE spSearchProducts @SearchField varchar (50) AS
    > >>
    > >> select qcnproduct.productid, qcnproduct.productname,
    > >> qcnproduct.prodpicture,
    > >> qcnproditem.qcnprice,
    > >> qcnproditem.supplycount,qcnproditem.typeid,
    > >> qcnsupplytype.typedesc
    > >> from qcnproduct
    > >> inner join qcnproditem
    > >> inner join qcnsupplytype on
    > >> qcnproditem.typeid = qcnsupplytype.typeid
    > >> on qcnproduct.productid = qcnproditem.productid
    > >> where qcnproduct.productname like @SearchField
    > >> or qcnproduct.proddesc like @SearchField
    > >> or qcnproduct.miscfacts like @SearchField
    > >> order by qcnproduct.productname, qcnproduct.productid,
    > >> qcnproditem.supplycount
    > >>
    > >> My problem is that if the var I send from ASP isn't in
    > >> the productname field, the result set is empty...but
    > if I
    > >> run this in SQL Query Analyzer it will return the
    > corect
    > >> record set??????
    > >>
    > >> TIA
    > >> -Bruce
    > >
    > >
    > >.
    > >

    Aaron Bertrand [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139