0

对于以下查询,我得到如下结果。但是对于这些情况,LIKE 条件并不成立。SQl Fiddle 简单地说,为什么 numMatches 列有 5 个匹配“缺失”关键字。 我将 missing 作为搜索词传递给 parameter @SearchItems

ALTER PROCEDURE ntwolose.SPSearchFilter (@SearchItems varchar,
@StartPos int,
@EndPos int)

AS

  SELECT
    *
  FROM (SELECT
    numMatches,
    ProductCode,
    ProductName,
    ProductType,
    Brand,
    Color,
    [Size],
    Model,
    GetPlace,
    LostPlace,
    DatePosted,
    TimePosted,
    Username,
    Updatedate,
    Status,
    Title,
    ROW_NUMBER() OVER (ORDER BY numMatches DESC) AS RatingRank
  FROM (SELECT
    ProductCode,
    ProductName,
    ProductType,
    Brand,
    Color,
    [Size],
    Model,
    GetPlace,
    LostPlace,
    DatePosted,
    TimePosted,
    Username,
    Updatedate,
    Status,
    Title,
    (CASE
        WHEN ProductCode LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN ProductName LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN Brand LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN Color LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN [Size] LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN Model LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN GetPlace LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN LostPlace LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN DatePosted LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN Username LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN Title LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END +
      CASE
        WHEN ProductType LIKE '%' + @SearchItems + '%' THEN 1
        ELSE 0
      END

    )
    AS numMatches
  FROM TBLProductDetails)
  AS RatedResultTable)
  AS PagedResultSet
  WHERE RatingRank BETWEEN @StartPos AND @EndPos

numMatches                                                                                                                                                                                                                                                       ProductCode                                        ProductName                                        ProductType                                        Brand                                              Color                                              Size                                               Model                                              GetPlace                                           LostPlace                                          DatePosted                     TimePosted                                         Username                                           Updatedate                     Status                                                                                                                                                                                                                                                           Title                                                                                                                                                                                                                                                            RatingRank                                                                                                                                                                                                                                                       
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------ -------------------------------------------------- -------------------------------------------------- ------------------------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
5                                                                                                                                                                                                                                                                cv,cf,cf                                           cgm cvh                                            cvhhcv                                             cvmgcvh                                            cvmgcmhc                                           cmv hcvh                                           2wes                                               dddd                                               cmhcvmh                                            2012-12-12 00:00:00.0000000    12.11                                              a2a                                                2013-05-20 17:27:49.6370000    1                                                                                                                                                                                                                                                                sree                                                                                                                                                                                                                                                             1                                                                                                                                                                                                                                                                
4                                                                                                                                                                                                                                                                P90                                                C510                                               Mobile                                             SOny                                               Black                                              12X12                                              M90                                                Ernakulam                                                                                             2013-12-02 00:00:00.0000000    12.opm                                             a@a                                                2013-05-22 19:12:05.0330000    1                                                                                                                                                                                                                                                                Sony Ercison Missing                                                                                                                                                                                                                                             2                                                                                                                                                                                                                                                                
3                                                                                                                                                                                                                                                                P 9090                                             Black Leather                                      Leather                                            Bata                                               Black                                              34 inches                                          M90                                                Malapuram                                          Kerala                                             2013-02-03 00:00:00.0000000    3:4 pm                                             sreekalac1990                                      2013-05-27 17:11:37.5570000    1                                                                                                                                                                                                                                                                Got Mirror Bags                                                                                                                                                                                                                                                  3                                                                                                                                                                                                                                                                
3                                                                                                                                                                                                                                                                P12                                                Documents                                          Mobile                                             Noikiya                                            red                                                123                                                678                                                Calicut                                            Cochin                                             2013-12-02 00:00:00.0000000    2:9:0                                              sree@gmail.comm                                    2013-05-17 10:17:13.8670000    1                                                                                                                                                                                                                                                                nair                                                                                                                                                                                                                                                             4                                                                                                                                                                                                                                                                
3                                                                                                                                                                                                                                                                P15                                                Documents                                          Mobile                                             Noikiya                                            red                                                123                                                678                                                Cochin                                             Calicut                                            2013-12-02 00:00:00.0000000    2:9:0                                              sree@gmail.comm                                    2013-05-17 10:17:13.0000000    1                                                                                                                                                                                                                                                                bigshen                                                                                                                                                                                                                                                          5                                                                                                                                                                                                                                                                
2                                                                                                                                                                                                                                                                P123                                               Lg                                                 Torch                                              LG                                                 white                                              12                                                 MD90                                               Ernakulam                                                                                             2013-12-02 00:00:00.0000000    12.00pm                                            a@a                                                2013-05-20 09:51:50.9930000    1                                                                                                                                                                                                                                                                c                                                                                                                                                                                                                                                                6                                                                                                                                                                                                                                                                
2                                                                                                                                                                                                                                                                P9099                                              fgvf                                               fdfv                                               fbf                                                ffbh                                               34                                                 fhbfgh                                             fvgbngfvn                                          hgnjhgmj                                           2013-05-05 00:00:00.0000000    3                                                  sreekalac1990                                      2013-05-27 17:18:04.7100000    1                                                                                                                                                                                                                                                                Subvibn Missing                                                                                                                                                                                                                                                  7                                                                                                                                                                                                                                                                
2                                                                                                                                                                                                                                                                123                                                mobile                                             ret4546                                            samsung                                            ertet                                              dgfdg                                              12                                                 1234                                               ewtret345                                          2012-12-12 00:00:00.0000000    12:89                                              123243                                             2013-05-20 17:05:44.2170000    1                                                                                                                                                                                                                                                                Title                                                                                                                                                                                                                                                            8                                                                                                                                                                                                                                                                
1                                                                                                                                                                                                                                                                fbgnh12                                            7266                                               mobile                                             nokia                                              Silver                                             72 x 66                                            12343                                              dddd                                               Pallikkal                                          2013-12-04 00:00:00.0000000    12.00 Am                                           a2a                                                2013-05-20 17:19:50.1970000    1                                                                                                                                                                                                                                                                kala                                                                                                                                                                                                                                                             9                                                                                                                                                                                                                                                                
0                                                                                                                                                                                                                                                                fghf                                               fghfh                                              fhfgh                                              fghgfh                                             fghfh                                              fghfgh                                             fhfgh                                              fhgfgh                                             fghfgh                                             2013-12-04 00:00:00.0000000    fghfg                                              a@a                                                2013-05-28 20:29:09.6700000    1                                                                                                                                                                                                                                                                dhgfgh                                                                                                                                                                                                                                                           10                                                                                                                                                                                                                                                               
0                                                                                                                                                                                                                                                                aaaaaaaaaaaaa                                      jsdofjs                                            dhfgkjdsgkjkj                                      djsfdsjf                                           sdfjlskd                                           5465                                               dfsdf                                              filed                                              hjdsfjfjks                                         2012-12-12 00:00:00.0000000    11.10 Pm                                           a2a                                                2013-05-20 17:25:03.2730000    1                                                                                                                                                                                                                                                                title                                                                                                                                                                                                                                                            11                                                                                                                                                                                                                                                               
0                                                                                                                                                                                                                                                                sdil                                               k                                                  89                                                 jkkb                                               uihui                                              889                                                xcv                                                jbkbk                                              jkbk                                               2013-03-03 00:00:00.0000000    2                                                  a@a                                                2013-05-29 09:57:48.3330000    1                                                                                                                                                                                                                                                                Shoe                                                                                                                                                                                                                                                             12                                                                                        
4

1 回答 1

3

对您的问题的简短回答是,您实际上并没有通过'missing'. @SearchTerms您可能正在传递 value 'm'。原因是@SearchTermsisvarchar而不是类似的定义,varchar(50)并且系统可能假设varchar(1).

SQL 小提琴版本

于 2013-05-30T05:22:48.727 回答