将多个CASE语句连接成一个别名

时间:2012-06-05 19:24:04

标签: sql tsql sql-server-2005

在以前的一些关于如何处理我遇到的遗留代码问题的帮助之后,似乎我的问题的最佳方法是连接case语句以返回我可以在PHP中解析的值。

我正在尝试做这样的事情,但它返回了很多行,并最终得到了这个错误:

  

最大存储过程,函数,触发器或视图嵌套级别   超过(限制32)。

SELECT org.org_id,
       org.org_name_1,
       Datename(YEAR, member.enroll_date) AS enroll_year,
       Max(CASE
             WHEN board.member_from IS NULL THEN 0
             ELSE 1
           END)                           AS board_member,
       CASE
         WHEN ( org.delete_reason = 'OUT'
                AND org.org_delete_flag = 'Y'
                AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
         ELSE ''
       END + CASE
               WHEN ( stat.carrier = 'BS'
                      AND stat.status_id IS NOT NULL
                      AND stat.termination_date IS NULL
                      AND stat.flat_dues > 0 ) THEN 'insurance_member|'
               ELSE ''
             END + CASE
                     WHEN ( stat.carrier = 'BS'
                            AND stat.status_id IS NOT NULL
                            AND stat.termination_date IS NULL
                            AND stat.flat_dues = 0
                            AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
                     ELSE ''
                   END + CASE
                           WHEN ( member.enroll_date IS NOT NULL
                                  AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
                           ELSE ''
                         END + CASE
                                 WHEN ( org.org_relationship_parent = 'Y'
                                        AND org.dues_category = 'MBR'
                                        AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
                                 ELSE ''
                               END + CASE
                                       WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
                                       ELSE ''
                                     END + CASE
                                             WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
                                             ELSE ''
                                           END + CASE
                                                   WHEN ( org.dues_category = 'FREE'
                                                          AND org.org_status_flag = 'P' ) THEN 'associate_member|'
                                                   ELSE ''
                                                 END
                                          --ELSE 'non_member'
                                          --END 
                                          AS org_status,
       60                                 AS expires_in,
       CASE
         WHEN stat.dues_type = 'M' THEN
           CASE
             WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
             ELSE 0
           END
         ELSE
           CASE
             WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
             ELSE member.payments
           END
       END                                AS dues_level,
       CASE
         WHEN ( org.affiliate_code = 'PCCE'
                AND org.dues_category = 'MBR'
                AND org.org_status_flag = 'R' ) THEN 1
         ELSE 0
       END                                AS pcce_membr,
       --          '$'+CONVERT(VARCHAR,@dues) AS dues_level,
       Ltrim(@product_level)              AS product_level,
       Ltrim(@involve_level)              AS involvement_level
FROM   organiz AS org
       LEFT JOIN affilbil AS member
         ON member.status_id = org.org_id
            AND member.dues_category = 'MBR'
       LEFT JOIN individu AS ind
         ON ind.org_id = org.org_id
       LEFT JOIN commembr AS board
         ON board.status_id = ind.ind_id
            AND board.committee_code = '5'
            AND board.member_to IS NULL
       LEFT JOIN statinsmorn AS stat
         ON stat.status_id = org.org_id
            AND stat.carrier = 'BS'
            AND stat.planz = 'PCI'
WHERE  org.org_id = @org_id
GROUP  BY org.org_id,
          org.org_name_1,
          member.enroll_date,
          org.delete_reason,
          org.org_status_flag,
          org.org_delete_flag,
          stat.status_id,
          stat.flat_dues,
          stat.dues_type,
          stat.termination_date,
          org.org_misc_data_9,
          org_relationship_parent,
          org.dues_category,
          member.status_flag,
          member.dues_billed_annual,
          member.payments,
          stat.carrier,
          org.Affiliate_Code 

1 个答案:

答案 0 :(得分:0)

嗯,这很令人尴尬。

当我对存储过程进行更改时,我无意中在底部调用了相同的过程。所以我一遍又一遍地递归调用相同的程序。 DOH。