0

我正在修复以前的开发人员存储过程,该存储过程旨在根据字符串匹配条件连接一堆表,然后搜索连接的表并返回信息以及信息如何匹配的描述。

该过程当前查看连接表中的每一列,当它找到匹配项时,它会在结果表中创建一个新行并进入源表中的下一列。问题是当连续有多个匹配时,第一个匹配之后的任何内容都不会产生。

我需要一个像这样的表:

----A---B---C---D---E---F---G---H---I---
|                                      |
1.-------------------------------------|
|   N   Y   Y   N   N   N   N   N   N  |
2.--------------------------------------
|   Y   N   N   N   N   N   N   N   N  |
3.--------------------------------------
|   N   Y   N   N   Y   Y   N   N   N  |
----------------------------------------

生成一个表格:

----A---B---C---D---E---F---G---H---I---
|                                      |
1.--------------------------------------
|   found a match in row 1-B           |
2.--------------------------------------
|   found a match in row 1-C           |
3.--------------------------------------
|   found a match in row 2-A           |
4.--------------------------------------
|   found a match in row 3-B           |
5.--------------------------------------
|   found a match in row 3-E           |
6.--------------------------------------
|   found a match in row 3-F           |
----------------------------------------

有没有比为需要检查的每一列创建单独的例程更有效的方法来查找所有匹配项。我被告知游标可以解决这个问题,但是这个查询已经需要相当长的时间来运行。

如果它有帮助,这里是原始程序:

SELECT  
    CASE WHEN (rpfname LIKE @SearchString OR rplname LIKE @SearchString) AND rlrelcode = 'A' THEN 2
        WHEN ((rpfname LIKE @SearchString OR rplname LIKE @SearchString) AND (rlrelcode LIKE 'c%' OR rlrelcode LIKE 'REL%' OR rlrelcode = 'E')) OR clname1 LIKE  @SearchString  OR clname2 LIKE  @SearchString OR clcontact LIKE  @SearchString  OR clrefer LIKE  @SearchString OR mcontact LIKE  @SearchString THEN 3 
        ELSE 1 END  AS type, 
    ISNULL(CAST(mmatter AS VARCHAR(100)), '<Matter does not exist>') AS client_matter_number,
    clnum, 
    mdesc1 AS matter_description, 
    ISNULL(mbillaty, udvalue) AS ttk,
    ISNULL(mopendt, clopendt)  AS open_date, 
    mclosedt  AS close_date, 
    LTRIM(ISNULL(clname1, '') + ISNULL(' ' + clname2, '')) AS client_name, 
    CASE WHEN (rpfname LIKE  @SearchString  OR rplname LIKE  @SearchString ) THEN LTRIM(ISNULL(rpfname, '') + ISNULL(' ' + rplname, ''))
        WHEN rddesc LIKE  @SearchString  THEN LTRIM(rddesc  )
        WHEN (clname1 LIKE  @SearchString  OR clname2 LIKE  @SearchString )  THEN LTRIM(ISNULL(clname1, '') + ISNULL(' ' + clname2, ''))
        WHEN clcontact LIKE  @SearchString  THEN LTRIM(clcontact)
        WHEN clrefer LIKE  @SearchString  THEN LTRIM(clrefer)
        WHEN cddesc LIKE  @SearchString  THEN LTRIM(cddesc)
        WHEN mname LIKE  @SearchString  THEN LTRIM(mname)
        WHEN mdesc1 LIKE  @SearchString  THEN LTRIM(mdesc1)
        WHEN mddesc LIKE  @SearchString  THEN LTRIM(mddesc)
        WHEN mcontact LIKE  @SearchString  THEN LTRIM(mcontact)
        WHEN fdesc1 LIKE  @SearchString  THEN LTRIM(fdesc1)
        WHEN fdnarr LIKE  @SearchString  THEN LTRIM(fdnarr)
        WHEN subdesc1 LIKE  @SearchString   THEN LTRIM(subdesc1)
        WHEN subnarr LIKE  @SearchString   THEN LTRIM(subnarr) END AS found_search_string, 
    CASE WHEN rpfname LIKE  @SearchString  OR rplname LIKE  @SearchString THEN dbo.stcGetRelationNarrative(rlindex) 
        ELSE '' END AS notes, 
    CASE WHEN (rpfname LIKE  @SearchString  OR rplname LIKE  @SearchString) THEN rlrelation 
        ELSE '' END AS relationship,
    CASE WHEN (rpfname LIKE  @SearchString  OR rplname LIKE  @SearchString) AND rlrelcode = 'A' THEN 'A'
        WHEN ((rpfname LIKE  @SearchString  OR rplname LIKE  @SearchString) AND (rlrelcode LIKE 'c%' OR rlrelcode LIKE 'REL%' OR rlrelcode = 'E')) OR clname1 LIKE  @SearchString  OR clname2 LIKE  @SearchString OR clcontact LIKE  @SearchString  OR clrefer LIKE  @SearchString OR mcontact LIKE  @SearchString THEN 'CF' 
        ELSE '?' END  AS relation_code,
    CASE WHEN (rpfname LIKE  @SearchString  OR rplname LIKE  @SearchString ) THEN 'RP Name'
        WHEN rddesc LIKE  @SearchString  THEN 'RP Narrative' 
        WHEN (clname1 LIKE  @SearchString  OR clname2 LIKE  @SearchString)  THEN 'Client Name'
        WHEN clcontact LIKE  @SearchString  THEN 'Client Contact'
        WHEN clrefer LIKE  @SearchString  THEN 'Client Referral'
        WHEN cddesc LIKE  @SearchString  THEN 'Client Narrative'
        WHEN mname LIKE  @SearchString  THEN 'Matter Name'
        WHEN mdesc1 LIKE  @SearchString  THEN 'Matter Description'
        WHEN mddesc LIKE  @SearchString  THEN 'Matter Narrative'
        WHEN mcontact LIKE  @SearchString  THEN 'Matter Contact'
        WHEN fdesc1 LIKE  @SearchString  THEN 'Folder Description'
        WHEN fdnarr LIKE  @SearchString  THEN 'Folder Narrative'
        WHEN subdesc1 LIKE  @SearchString   THEN 'Submatter Description'
        WHEN subnarr LIKE  @SearchString   THEN 'Submatter Narrative' END AS source_of_information 
FROM crlparty rp WITH (NOLOCK) 
    LEFT OUTER JOIN 
    crllink r  WITH (NOLOCK) 
        ON r.rpindex = rp.rpindex  
    FULL OUTER JOIN  
    matter m WITH (NOLOCK) 
        ON rlmatter = m.mrelated 
    LEFT OUTER JOIN 
    mattdesc md WITH (NOLOCK) 
        ON m.mmatter = md.mmatter 
    FULL OUTER JOIN  
    client c WITH (NOLOCK)  
        ON ISNULL(m.mclient, r.rlclnum) = c.clnum 
    LEFT OUTER JOIN
    clidesc cd WITH (NOLOCK)
        ON c.clnum = cd.clnum 
    LEFT OUTER JOIN
    crldesc rpd WITH (NOLOCK)
        ON rpd.rpindex = r.rpindex
    LEFT OUTER JOIN 
    udf u WITH (NOLOCK)
        ON u.udjoin = c.clnum AND 
            u.udfindex=40
    FULL OUTER JOIN
    folder f WITH (NOLOCK) 
        ON m.mmatter = f.fmatter
    LEFT OUTER JOIN 
    foldnarr fn WITH (NOLOCK) 
        ON f.findex = fn.findex 
    FULL OUTER JOIN
    submatter s WITH (NOLOCK) 
        ON r.rlmatter = s.smatter
    LEFT OUTER JOIN 
    subnarr sn WITH (NOLOCK) 
        ON s.sindex = sn.sindex

WHERE (rp.rpfname LIKE  @SearchString  OR rp.rplname LIKE  @SearchString ) OR
    rpd.rddesc LIKE  @SearchString  OR 

    (c.clname1 LIKE  @SearchString  OR c.clname2 LIKE  @SearchString )  OR
    c.clcontact LIKE  @SearchString  OR
    c.clrefer LIKE  @SearchString  OR
    cd.cddesc LIKE  @SearchString  OR

    m.mname LIKE  @SearchString  OR
    m.mdesc1 LIKE  @SearchString  OR
    md.mddesc LIKE  @SearchString  OR 
    m.mcontact LIKE  @SearchString  OR

    f.fdesc1 LIKE  @SearchString  OR
    fn.fdnarr LIKE  @SearchString  OR

    s.subdesc1 LIKE  @SearchString   OR
    sn.subnarr LIKE  @SearchString
4

2 回答 2

0

我最终将所有逻辑从存储过程转移到应用程序。存储过程只是简单地连接表并执行一些简单的过滤器。然后我使用应用程序 (C#) 返回我想要的结果。

于 2012-07-09T17:02:14.647 回答
0

我确信有更有效的方法,但我并不完全确定大局 - 但只是简单地回答“我需要一个像......这样的桌子”的问题,这是一个快速而肮脏的

  SELECT ID, 'Found a match in row ' + CAST(Id as varchar(8))+ '-A' FROM YOURTABLE WHERE A = 'Y'
  UNION  ALL SELECT ID, 'Found a match in row ' + CAST(Id as varchar(8))+ '-B' FROM YOURTABLE  WHERE B = 'Y'
  UNION  ALL SELECT ID, 'Found a match in row ' + CAST(Id as varchar(8))+ '-C' FROM YOURTABLE WHERE C = 'Y'
于 2012-06-27T15:59:17.407 回答