2012-06-05 61 views
0

經過一些關於如何解決我遇到的一些遺留代碼的問題之後,似乎我的問題的最佳方法是連接case語句以返回我可以解析的值在PHP中。將多個CASE語句連接成一個別名

我試圖做這樣的事情,但它返回許多行,並最終收到此錯誤:

Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 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 
+2

你能發佈完整的查詢,包括'from'子句嗎? – Andomar

+0

@Andomar發佈。 – etm124

+0

你是不是偶然地打電話給其他人的意見呢? – HLGEM

回答

0

那麼,這是令人尷尬的。

當我對存儲過程進行更改時,我無意中在底部調用了相同的過程。所以我一遍又一遍遞歸地調用同一個過程。 DOH。