2016-06-28 6 views
0

カーソルの結果をループするストアドプロシージャがあります。私が抱えている問題は、license_attributesの値は常にそうであってはならない場合はnullです。ストアドプロシージャの外部でselectステートメントを実行するか、またはストアドプロシージャのカーソルの外側にあるselectステートメントを実行すると、私は期待している結果を返します(NULLではありません)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ので、SELECT文のlicense_idへの参照は、その手順の変数への参照である

DECLARE license_id VARCHAR(18) DEFAULT null; 

、コードブロック内の変数として宣言されます。

コードブロックの外には、おそらくlicense_idという名前のローカル変数がありません。したがって、同じSQL SELECT文は、license_idへの参照は変数への参照ではなく、列への参照です。

私はすべてのロジック、またはlicense_id変数の内容をトレースしていません。しかし、コードブロックとブロックの外側で実行されたステートメントで観察された動作の違いを説明していると思われます。

+0

ありがとうございました。 license_id変数の代わりに 'sf'.'License_Key_Association__c'.'License_Key__c'を参照するように変更しました。 – kambythet

+0

私はなぜ質問がdownvoteを受け取ったのか分かりません。私はそれが悪い質問ではないと思います。私は疑問を浮かべて(+10)、それは大きな問題だからではなく、望ましくないdownvoteをバランスさせるためです。 – spencer7593

+0

ありがとうございます。なぜそれが落選したのか分からなかった。私は有効な質問のためのすべての基準に合っていると思った。 – kambythet

-2

あなたはCOALESCEは、あなたのGROUP_CONCATにNULL値の世話をすることができます

COALESCE(`YourColumnName`,'') 

あなたは、クエリを修正できるように、また、NULL値がどこから来ているお見せするために大幅に異なる文字列を使用することができます。

COALESCE(`YourColumnName`,'**UNEXPECTED NULL**') 
関連する問題