2016-06-28 137 views
0

我有一個存儲過程,我在循環遊標中的結果。我遇到的問題是,license_attributes值在不應該出現時始終爲空。如果我在存儲過程之外執行select語句,或者作爲調試在存儲過程中執行光標外部的select語句,我會得到我期望的結果(非空值)mysql存儲過程group_concat在遊標中選擇返回null

這是部分在光標選擇總是返回NULL:

 (SELECT 
      CONCAT('{""',sf.Asset_Attribute__c.Type__c,'"": {',GROUP_CONCAT(
      '""',sf.Asset_Attribute__c.Key__c,'"":""',LOWER(sf.Asset_Attribute__c.Value__c),'""' 
      ),'}}') 
      FROM 
       sf.Asset_Attribute__c 
      WHERE 
       sf.Asset_Attribute__c.Asset__c = license_id 
      GROUP BY sf.Asset_Attribute__c.Asset__c) AS `license_attributes` 

這裏是存儲過程的部分:

GETCLOUDACCOUNTS:BEGIN 

    DECLARE no_more_cloud_accounts_records boolean DEFAULT FALSE; 

    DECLARE company VARCHAR(255) DEFAULT null; 
    DECLARE license_status VARCHAR(50) DEFAULT null; 
    DECLARE license_id VARCHAR(18) DEFAULT null; 
    DECLARE cloud_owner_email VARCHAR(255) DEFAULT null; 
    DECLARE entitlement_plan VARCHAR(255) DEFAULT null; 
    DECLARE role VARCHAR(500) DEFAULT null; 
    DECLARE is_trial BOOLEAN DEFAULT false; 
    DECLARE license_attributes VARCHAR(2000) DEFAULT null; 
    DECLARE zuora_account_id VARCHAR(100) DEFAULT ''; 
    DECLARE zuora_account_number VARCHAR(50) DEFAULT null; 
    DECLARE zuora_account_status VARCHAR(50) DEFAULT null; 
    DECLARE zuora_account_last_invoice_date DATETIME DEFAULT null; 
    DECLARE has_active_subscriptions BOOLEAN DEFAULT false; 

    DECLARE cloud_accounts_cursor CURSOR FOR 
     SELECT 
      (SELECT `sf`.`Contact`.`CompanyName__c` FROM `sf`.`Contact` WHERE `sf`.`Asset`.`ContactId`=`sf`.`Contact`.`Id`) AS `company`, 
      `sf`.`License_Key_Association__c`.`License_Key_Status__c` AS `license_status`, 
      `sf`.`License_Key_Association__c`.`License_Key__c` AS `license_id`, 
      `sf`.`Asset`.`ContactEmail__c` AS `cloud_owner_email`, 
      (SELECT `sf`.`Contact`.`CloudEntitlementPlan__c` FROM `sf`.`Contact` WHERE `sf`.`Asset`.`ContactId`=`sf`.`Contact`.`Id`) AS `entitlement_plan`, 
      `sf`.`License_Key_Association__c`.`Role__c` AS `role`, 
      IF((SELECT `sf`.`Product2`.`IsCommercial__c` FROM `sf`.`Product2` WHERE `sf`.`Product2`.`Id`=`sf`.`Asset`.`Product2Id`) = 0,true,false) AS `is_trial`, 
      (SELECT 
       CONCAT('{""',sf.Asset_Attribute__c.Type__c,'"": {',GROUP_CONCAT(
       '""',sf.Asset_Attribute__c.Key__c,'"":""',LOWER(sf.Asset_Attribute__c.Value__c),'""' 
       ),'}}') 
       FROM 
        sf.Asset_Attribute__c 
       WHERE 
        sf.Asset_Attribute__c.Asset__c = license_id 
       GROUP BY sf.Asset_Attribute__c.Asset__c) AS `license_attributes` 
     FROM 
      `sf`.`License_Key_Association__c` 
     LEFT JOIN `sf`.`Asset` 
      ON `sf`.`License_Key_Association__c`.`License_Key__c` = `sf`.`Asset`.`Id` 
     JOIN `sf`.`Contact` 
      ON `sf`.`Contact`.`Id` = `sf`.`License_Key_Association__c`.`Contact__c` 
     WHERE 
      `sf`.`Contact`.`ExternalID__c`='someexternalidhere'; 

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_cloud_accounts_records = true; 
     SELECT 
      (SELECT `sf`.`Contact`.`CompanyName__c` FROM `sf`.`Contact` WHERE `sf`.`Asset`.`ContactId`=`sf`.`Contact`.`Id`) AS `company`, 
      `sf`.`License_Key_Association__c`.`License_Key_Status__c` AS `license_status`, 
      `sf`.`License_Key_Association__c`.`License_Key__c` AS `license_id`, 
      `sf`.`Asset`.`ContactEmail__c` AS `cloud_owner_email`, 
      (SELECT `sf`.`Contact`.`CloudEntitlementPlan__c` FROM `sf`.`Contact` WHERE `sf`.`Asset`.`ContactId`=`sf`.`Contact`.`Id`) AS `entitlement_plan`, 
      `sf`.`License_Key_Association__c`.`Role__c` AS `role`, 
      IF((SELECT `sf`.`Product2`.`IsCommercial__c` FROM `sf`.`Product2` WHERE `sf`.`Product2`.`Id`=`sf`.`Asset`.`Product2Id`) = 0,true,false) AS `is_trial`, 
      (SELECT 
       CONCAT('{""',sf.Asset_Attribute__c.Type__c,'"": {',GROUP_CONCAT(
       '""',sf.Asset_Attribute__c.Key__c,'"":""',LOWER(sf.Asset_Attribute__c.Value__c),'""' 
       ),'}}') 
       FROM 
        sf.Asset_Attribute__c 
       WHERE 
        sf.Asset_Attribute__c.Asset__c = license_id 
       GROUP BY sf.Asset_Attribute__c.Asset__c) AS `license_attributes` 
     FROM 
      `sf`.`License_Key_Association__c` 
     LEFT JOIN `sf`.`Asset` 
      ON `sf`.`License_Key_Association__c`.`License_Key__c` = `sf`.`Asset`.`Id` 
     JOIN `sf`.`Contact` 
      ON `sf`.`Contact`.`Id` = `sf`.`License_Key_Association__c`.`Contact__c` 
     WHERE 
      `sf`.`Contact`.`ExternalID__c`[email protected]_externalId; 

    OPEN cloud_accounts_cursor; 
    CLOUDACCOUNTSLOOP: loop 

     fetch cloud_accounts_cursor into company, license_status, license_id, cloud_owner_email, entitlement_plan, role, is_trial, license_attributes; 

     IF is_trial = true THEN 
      SET has_active_subscriptions = true; 
     END IF; 

     SET zuora_account_id = `z`.`getZAccountId`(cloud_owner_email); 

     IF zuora_account_id IS NOT NULL THEN 
      SELECT `accountNumber`,`status`,`lastInvoiceDate` INTO zuora_account_number,zuora_account_status,zuora_account_last_invoice_date FROM zuora.Account WHERE id=zuora_account_id; 

      IF has_active_subscriptions = false THEN 
       SET has_active_subscriptions = (SELECT IF((SELECT COUNT(*) FROM `z`.`RatePlan` 
        RIGHT JOIN `z`.`ProductRatePlan` ON `z`.`RatePlan`.`productRatePlanId` = `z`.`ProductRatePlan`.`id` 
        LEFT JOIN `z`.`Subscription` ON `z`.`RatePlan`.`subscriptionId` = `z`.`Subscription`.`id` 
        WHERE 
        `z`.`ProductRatePlan`.`wowzaRatePlanCode__c` IN ((SELECT `code` FROM `z`.`zCloudRatePlanCodes`)) 
        AND `z`.`Subscription`.`status` = 'Active' 
        AND `z`.`Subscription`.`accountId` = zuora_account_id) > 0, true, false)); 
      END IF; 
     END IF; 

     REPLACE INTO `sf`.`zCloudAccounts` (`user_email`,`company`,`license_status`,`license_id`,`cloud_owner_email`,`entitlement_plan`,`role`,`is_trial`,`attributes`,`zuora_account_id`,`zuora_account_number`,`zuora_account_status`,`zuora_account_last_invoice_date`,`has_active_subscriptions`) VALUES(@p_userEmail,company,license_status,license_id,cloud_owner_email,entitlement_plan,role,is_trial,license_attributes,zuora_account_id,zuora_account_number,zuora_account_status,zuora_account_last_invoice_date,has_active_subscriptions); 

     IF no_more_cloud_accounts_records THEN 
      CLOSE cloud_accounts_cursor; 
      LEAVE CLOUDACCOUNTSLOOP; 
     end if; 

    END LOOP CLOUDACCOUNTSLOOP; 

END GETCLOUDACCOUNTS; 

如果我執行GETCLOUDACCOUNTS塊之外的全部選擇stateout,我得到的結果我想到:

company, license_status, license_id, cloud_owner_email, entitlement_plan, role, is_trial, license_attributes 
Test Company, Active, 02iq0000000jKgMAAU, [email protected], Standard, Owner, 0, {""cloud"": {""cloud_num_247_t_streams"":""0"",""cloud_num_247_p_streams"":""0""}} 
Test Company, Active, 02iq0000000xlBBAAY, [email protected], Standard, Admin;wcl_admin;wcl_support, 0, {""cloud"": {""cloud_num_247_t_streams"":""1"",""cloud_num_247_p_streams"":""1"",""test_attribute"":""true"",""api_access"":""true""}} 

但塊顯示license_attributes內的結果爲空:

company, license_status, license_id, cloud_owner_email, entitlement_plan, role, is_trial, license_attributes 
Test Company, Active, 02iq0000000jKgMAAU, [email protected], Standard, Owner, 0, null 
Test Company, Active, 02iq0000000xlBBAAY, [email protected], Standard, Admin;wcl_admin;wcl_support, 0, null 

任何幫助非常感謝!

回答

1

我懷疑問題是與程序變量license_id有關。

在SELECT列表中的關聯的子查詢包括

WHERE sf.Asset_Attribute__c.Asset__c = license_id 
             ^^^^^^^^^^ 

局部變量優先於列引用。由於license_id被聲明爲碼塊內的變量,

DECLARE license_id VARCHAR(18) DEFAULT null; 

在SELECT語句的引用license_id是這一程序變量的引用。

在代碼塊之外,可能不存在名爲license_id的局部變量。因此,引用license_id的相同SQL SELECT語句不是對變量的引用,而是對列的引用。

我沒有查到所有的邏輯,或license_id變量的內容。但我懷疑這解釋了在語句中觀察到的行爲差異,在代碼塊內部與塊外執行。

+0

果然!謝謝。將其更改爲引用'sf'.'License_Key_Association__c'.'License_Key__c'而不是license_id變量,並且瞧! – kambythet

+0

我不知道爲什麼這個問題收到了downvote。我不認爲這是一個壞問題。我提出這個問題(+10)並不是因爲這是一個很好的問題,而是爲了平衡不合適的downvote。 – spencer7593

+0

謝謝。我也不確定爲什麼它被低估了。我認爲我符合有效問題的所有標準。 – kambythet

-2

可以COALESCE照顧NULL值的你GROUP_CONCAT

COALESCE(`YourColumnName`,'') 

你也可以使用一個顯著不同的字符串向您展示NULL值是從哪裏來的,這樣你就可以修復查詢。

COALESCE(`YourColumnName`,'**UNEXPECTED NULL**')