繁体   English   中英

MySQL游标:遍历多行? 出现42000错误,“多于一行”-但是期望多行吗?

[英]MySQL Cursor: looping through multiple rows? Getting 42000 error, “more than one row” - but expect multiple rows?

我认为游标的要点是要遍历多行,这些行与用于定义游标的SQL中的条件匹配。

因此,我困惑于执行此代码时遇到此错误,因为我的目标是从与“国防承包商”类型匹配的多行中操作并提取关键数据:

错误是“代码:1172 SQL状态:42000 ---结果由多于一行组成”

我做错了什么?

CREATE DEFINER=`root`@`localhost` PROCEDURE `clientschema`.`MapDistrictsToAccounts`()
    MODIFIES SQL DATA
BEGIN
 declare finished integer default 0;
 declare v_CD  varchar(45) default null;
 declare v_SSD varchar(45) default null;
 declare v_SHD varchar(45) default null;
 /* Temporary variable */
 declare v_sfdcDistrictId varchar(45) default null;
 declare v_sfdcAccountId varchar(45) default null;

 declare account_cursor cursor for
        select externalsys_id, congressional_district, state_house_district, state_senate_district from clientschema.Account where type="Defense Contractor";

/* declare not found handler*/
declare continue handler
for not found set finished = 1;

 open account_cursor;

 get_districts : LOOP
     fetch account_cursor into v_sfdcAccountId, v_CD, v_SHD, v_SSD;

      if finished = 1 THEN
      leave get_districts;
      end if;

     /* First do congressional district */
     select externalsys_id from clientschema.District where district_number__c = v_CD into v_sfdcDistrictId;
     /*if v_sfdcDistrictId = null then iterate get_districts;*/
     insert into clientschema.Account_Junction_District (district_id,hospital_idHospital,district_type__c) values (v_sfdcDistrictId, v_sfdcAccountId, "Direct");

     /* Next do State House district */
     select externalsys_id from clientschema.District where district_number__c = v_SHD into v_sfdcDistrictId;
     /*if v_sfdcDistrictId = null then iterate get_districts;*/
     insert into clientschema.Account_Junction_District (district_id,hospital_idHospital,district_type__c) values (v_sfdcDistrictId, v_sfdcAccountId, "Direct");

     /* Last do State Senate district */
     select externalsys_id from clientschema.District where district_number__c = v_SSD into v_sfdcDistrictId;
     /*if v_sfdcDistrictId = null then iterate get_districts;*/
     insert into clientschema.Account_Junction_District (district_id,hospital_idHospital,district_type__c) values (v_sfdcDistrictId, v_sfdcAccountId, "Direct");

     END LOOP get_districts;
close account_cursor;

END

问题不在于光​​标。 这是一个逻辑错误。

问题是这样的三个查询:

select externalsys_id from clientschema.District where district_number__c = v_CD into v_sfdcDistrictId;

我需要在地区类型上添加一个限定词。 在多个地区类型中存在重复的地区编号。

问题解决了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM