我认为游标的意思是遍历多行,这些行与用于定义游标的SQL中的条件相匹配。
所以我感到困惑的是,我在执行此代码时遇到此错误,因为我的目标是操作并从与#34;防御承包商类型相匹配的多行中提取关键数据:":
错误是"代码: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
答案 0 :(得分:0)
问题不在于光标。这是一个逻辑错误。
问题是这样的三个查询:
select externalsys_id from clientschema.District where district_number__c = v_CD into v_sfdcDistrictId;
我需要在区域类型上放置限定符。多个地区类型都有重复的地区号码。
问题解决了!