Odd issue with LIKE operator and variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • schismaticus
    New Member
    • Nov 2007
    • 2

    Odd issue with LIKE operator and variable

    Hoping someone can shed some light on this one, using MS SQL 2000,

    I have a query like this;

    Code:
    Select T.TransactionRecordClass, T.ClassDetail from [remoteserver].otherdb.dbo.Data  T where T.TransactionRecordClass like '10000002%'

    which will return around 10000 records in under 1 second.
    My issue is that I need to make this part of a function, but as soon as I add parameters/variables ( see below ) , it goes from returning in under 1 second, to 11 minutes!!!

    Code:
    DECLARE @TRC_ID nvarchar (9)
    SET @TRC_ID = rtrim('10000002') + '%'
    
    Select T.TransactionRecordClass, T.ClassDetail from [remoteserver].otherdb.dbo.Data  T where T.TransactionRecordClass like @TRC_ID

    There is a non-clustered index on the 2 fields being returned , and about 77 million records in the table. I have attempted using Index hints, but found they are not allowed on remote queries..... So... any clues why this happens, and how to fix????

    Thanks in advance for any help
  • schismaticus
    New Member
    • Nov 2007
    • 2

    #2
    OK, found a solution...

    The field type of T.TransactionRe cordClass is "char", my variable is declared "nvarchar", but if I declare the variable as "char" then the query time is back to < 1 second

    Originally posted by schismaticus
    Hoping someone can shed some light on this one, using MS SQL 2000,

    I have a query like this;

    Code:
    Select T.TransactionRecordClass, T.ClassDetail from [remoteserver].otherdb.dbo.Data  T where T.TransactionRecordClass like '10000002%'

    which will return around 10000 records in under 1 second.
    My issue is that I need to make this part of a function, but as soon as I add parameters/variables ( see below ) , it goes from returning in under 1 second, to 11 minutes!!!

    Code:
    DECLARE @TRC_ID nvarchar (9)
    SET @TRC_ID = rtrim('10000002') + '%'
    
    Select T.TransactionRecordClass, T.ClassDetail from [remoteserver].otherdb.dbo.Data  T where T.TransactionRecordClass like @TRC_ID

    There is a non-clustered index on the 2 fields being returned , and about 77 million records in the table. I have attempted using Index hints, but found they are not allowed on remote queries..... So... any clues why this happens, and how to fix????

    Thanks in advance for any help

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      You never forget anything that you learn by intrest.

      Comment

      Working...