Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

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

  1. #1

    Default Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

    I'm writing the simplest of search routines in ASP versus an Access database
    w. ADO. The application relies heavily on the database, my project includes
    a general purpose RecordSet.Open routine to handle queries. Basically, I
    pass a query to the routine, it opens a connection to the database, creates
    a recordset, clones it, closes the recordset and connection and returns the
    clone to the calling procedure. No problems there, everything smooth.

    Here comes the bizarre situation: I wrote a search routine using "SELECT *
    FROM TABLE WHERE TITLE LIKE '*" & Some Variable passed in from a form & "*'"
    (Caps for clarity). Now at first, this query did not return ANY result,
    although copy/pasting it into Access returned the expected result. So, I
    started fiddling with the query and something strange happened: I changed
    the query to "SELECT * FROM TABLE WHERE TITLE='" & Variable & "'" to test
    wether the LIKE clause was the problem. Now, when I entered a variable that
    would logically return no results (i.e. no match found for the TITLE field),
    the query indeed returned no records. But: if I enter a string into Variable
    that is actually FOUND in the TITLE field, the server (W2K w. all the
    updates) jumps to 100% CPU load and the process DLLHOST starts gobbling up
    memory until the server hits "Low virtual memory warning". No errors are
    reported in the event log, no error is returned to the client except a
    timeout when the server hits "Low virtual memory". After the client stops
    (either through timeout or manually), server memory and CPU load drop back
    to normal. Strangely, there are multiple "SELECT * FROM TABLE WHERE ID=" &
    Variable queries in the application that function flawlessly.

    MSDN and KB give no answer; I am at a loss. Behaviour can be reproduced on a
    different server. Anyone?

    Thanks,
    Robert


    Frogbčrt Guest

  2. Similar Questions and Discussions

    1. Strange behaviour using ArrayAppend
      Please have a look at the code posted below. First, I create a new Array, called 'results'. Each of its elements will be a structure. On lines 2...
    2. strange SQL behaviour
      Hello, I am running DB2 v7.2 on NT. suppose I ussue following simple SQL stmt:
    3. Very strange behaviour
      Hello All, Please help to answer a question why I cannot access port 6355 in below example. Puting the name of service (mydaemon) instead of port...
    4. Strange CLI behaviour
      Dear all, I've encountered some strange behaviour with PHP (4.3.2) using the CLI-API. When I provide an option in the first line of my script...
    5. Strange Mac/Airport behaviour
      In article <BB27092E.40F4%don40@bigpond.com>, Don Williams <don40@bigpond.com> wrote: Not sure if this will help but when I was having in-depth...
  3. #2

    Default Re: Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

    When creating dynamic sql statements in ADO, you must use the % and _
    wildcards in your criteria ("select *" still works, although its use is not
    recommended in production code - see Ken Schaeffer's reply to the post that
    came immediately before yours to see why), no matter what wildcard
    characters are used by the backend database.

    HTH,
    Bob Barrows

    Frogbčrt wrote:
    > Here comes the bizarre situation: I wrote a search routine using
    > "SELECT * FROM TABLE WHERE TITLE LIKE '*" & Some Variable passed in

    Bob Barrows Guest

  4. #3

    Default Re: Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

    "Bob Barrows" <reb_01501@yahoo.com> wrote in message
    news:OuKGK3hbDHA.2344@TK2MSFTNGP12.phx.gbl...
    > When creating dynamic sql statements in ADO, you must use the % and _
    > wildcards in your criteria ("select *" still works, although its use is
    not
    > recommended in production code - see Ken Schaeffer's reply to the post
    that
    > came immediately before yours to see why), no matter what wildcard
    > characters are used by the backend database.
    >
    > HTH,
    > Bob Barrows
    >
    Thanks for the response, Bob, but changing the wildcards does not help. I'm
    including some code for clarity's sake; this is the original code, which
    DOES NOT return any results even though the query would return results (e.g.
    in Access or SQL Server, the query would return one or more records).
    Changing the query in the second line to "select * from items where Title='"
    & SearchFor & "'" results (if a valid search term is entered) in the bizarre
    behaviour described earlier.
    SearchFor=Request.Form("TxtSearch")
    SearchString="select * from items where Title like '&" & SearchFor & "&'"
    Set MyRst=OpenRst(SearchString)
    If MyRst.RecordCount=0 then
    Response.Write "no items found."
    MyRst.Close
    Set MyRst=Nothing
    Response.End
    End If
    Do Until MyRst.EOF
    Response.Write "<br>" & MyRst.Fields("Description")
    Loop
    MyRst.Close
    Set MyRst=Nothing



    Function OpenRst(nQuery)
    Set MyConn=Server.CreateObject("ADODB.Connection")
    MyConn.Open ConnString
    Set GPRst=Server.CreateObject("ADODB.Recordset")
    GPRst.LockType=2
    GPRst.CursorType=3
    GPRst.ActiveConnection=MyConn
    GPRst.Source=nQuery
    GPRst.Open
    Set OpenRst=GPRst.Clone
    GPRst.Close
    Set GPRst=Nothing
    End Function


    Frogbčrt Guest

  5. #4

    Default Re: Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

    The reason that you see the 100% CPU is because you have:

    : Do Until MyRst.EOF
    : Response.Write "<br>" & MyRst.Fields("Description")
    : Loop

    because you have no myRST.MoveNext, you have an infinite loop.

    Also, what Bob said about wildcards is right. When using the OLEDB Provider
    you need to use the ANSI wildcards, not the Jet wildcards:

    strSQL = _
    "SELECT field1, field2, field3 " & _
    "FROM myTable " & _
    "WHERE myField LIKE '%" & strSearchFor & "%'"

    [url]www.adOpenStatic.com/faq/likequeries.asp[/url]

    Cheers
    Ken


    "Frogbčrt" <aa@aa.aa> wrote in message
    news:bindl9$omn$1@news-reader5.wanadoo.fr...
    : "Bob Barrows" <reb_01501@yahoo.com> wrote in message
    : news:OuKGK3hbDHA.2344@TK2MSFTNGP12.phx.gbl...
    : > When creating dynamic sql statements in ADO, you must use the % and _
    : > wildcards in your criteria ("select *" still works, although its use is
    : not
    : > recommended in production code - see Ken Schaeffer's reply to the post
    : that
    : > came immediately before yours to see why), no matter what wildcard
    : > characters are used by the backend database.
    : >
    : > HTH,
    : > Bob Barrows
    : >
    : Thanks for the response, Bob, but changing the wildcards does not help.
    I'm
    : including some code for clarity's sake; this is the original code, which
    : DOES NOT return any results even though the query would return results
    (e.g.
    : in Access or SQL Server, the query would return one or more records).
    : Changing the query in the second line to "select * from items where
    Title='"
    : & SearchFor & "'" results (if a valid search term is entered) in the
    bizarre
    : behaviour described earlier.
    : SearchFor=Request.Form("TxtSearch")
    : SearchString="select * from items where Title like '&" & SearchFor & "&'"
    : Set MyRst=OpenRst(SearchString)
    : If MyRst.RecordCount=0 then
    : Response.Write "no items found."
    : MyRst.Close
    : Set MyRst=Nothing
    : Response.End
    : End If
    : Do Until MyRst.EOF
    : Response.Write "<br>" & MyRst.Fields("Description")
    : Loop
    : MyRst.Close
    : Set MyRst=Nothing
    :
    :
    :
    : Function OpenRst(nQuery)
    : Set MyConn=Server.CreateObject("ADODB.Connection")
    : MyConn.Open ConnString
    : Set GPRst=Server.CreateObject("ADODB.Recordset")
    : GPRst.LockType=2
    : GPRst.CursorType=3
    : GPRst.ActiveConnection=MyConn
    : GPRst.Source=nQuery
    : GPRst.Open
    : Set OpenRst=GPRst.Clone
    : GPRst.Close
    : Set GPRst=Nothing
    : End Function
    :
    :


    Ken Schaefer Guest

  6. #5

    Default Re: Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

    Frogbčrt wrote:
    > SearchString="Select ID,Title from Items where Trim(Title) like '&" &
    > SearchFor & "&'"
    > Set MyRst=OpenRst(SearchString)
    >
    Change
    like '&" &
    to
    like '%" &

    and
    SearchFor & "&'"
    to
    SearchFor & "%'"

    The wildcard is %, not &.

    Bob Barrows


    Bob Barrows Guest

  7. #6

    Default Re: RESOLVED (sort of): Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

    Why are you using &?!? That's not a multi-character wildcard. You want %

    SearchString="SELECT ID,Title FROM Items WHERE Title LIKE '%" & SearchFor &
    "%'"

    NOTE: I replaced your & with %

    Cheers
    Ken


    "Frogbčrt" <aa@aa.aa> wrote in message
    news:binhnk$bjr$1@news-reader2.wanadoo.fr...
    : OK, following some hints from Bob and Ken (thanks) a train of thought
    ended
    : up with a workaround (which seems to get me a performance improvement as
    : well :)
    :
    : However, I somehow feel that this code is incredibly unelegant (for-next
    : loops?). And (isn't any developer like this?) I still don't know WHY the
    : original code didn't work and I want to figure it out... Ah well, perhaps
    : after the project is finished. But then again, perhaps not :)
    :
    : This is the adapted code I used:
    : SearchFor=Request.Form("TxtSearch")
    : Set MyRst=OpenRst("Select ID,Title from Items")
    : MyArray=MyRst.GetRows
    : MyRst.Close
    : Set MyRst=Nothing
    : For R=0 to Ubound(MyArray)
    : If Instr(1,MyArray(1,R),SearchFor) then
    : Response.Write MyArray(0,R) & "-" & MyArray(1,R)
    : End If
    : Next


    Ken Schaefer Guest

  8. #7

    Default Re: RESOLVED (sort of): Strange behaviour of SQL Query SELECT WHERE LIKE '*something*'

    Jep, seen that one, but I dropped the query alltogether (re. code snippet)
    and worked around it, since even with the % operators the bizarre memory
    gobble occurred.

    Thanks for your help,
    Robert

    "Ken Schaefer" <kenREMOVE@THISadOpenStatic.com> wrote in message
    news:e#iCe2ibDHA.2940@TK2MSFTNGP11.phx.gbl...
    > Why are you using &?!? That's not a multi-character wildcard. You want %
    >
    > SearchString="SELECT ID,Title FROM Items WHERE Title LIKE '%" & SearchFor
    &
    > "%'"
    >
    > NOTE: I replaced your & with %
    >
    > Cheers
    > Ken
    >
    >
    > "Frogbčrt" <aa@aa.aa> wrote in message
    > news:binhnk$bjr$1@news-reader2.wanadoo.fr...
    > : OK, following some hints from Bob and Ken (thanks) a train of thought
    > ended
    > : up with a workaround (which seems to get me a performance improvement as
    > : well :)
    > :
    > : However, I somehow feel that this code is incredibly unelegant (for-next
    > : loops?). And (isn't any developer like this?) I still don't know WHY the
    > : original code didn't work and I want to figure it out... Ah well,
    perhaps
    > : after the project is finished. But then again, perhaps not :)
    > :
    > : This is the adapted code I used:
    > : SearchFor=Request.Form("TxtSearch")
    > : Set MyRst=OpenRst("Select ID,Title from Items")
    > : MyArray=MyRst.GetRows
    > : MyRst.Close
    > : Set MyRst=Nothing
    > : For R=0 to Ubound(MyArray)
    > : If Instr(1,MyArray(1,R),SearchFor) then
    > : Response.Write MyArray(0,R) & "-" & MyArray(1,R)
    > : End If
    > : Next
    >
    >

    Frogbčrt 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