使用游标检查行中的每个列并返回每个匹配的行

时间:2012-06-27 14:41:27

标签: sql cursor sql-server-2000

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

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

我需要一张像这样的表:

----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

2 个答案:

答案 0 :(得分:0)

我确信有更有效的方法,而且我不能完全确定大局 - 但只是简单地回答问题&#34;我需要一张像...这样的表来导致&# 34;这是一个快速而又肮脏的

  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'

答案 1 :(得分:0)

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