失踪'结束'创建临时表

时间:2018-04-24 05:15:38

标签: mysql stored-procedures cursor temp-tables

我正在创建一个存储过程,我应该创建一个临时表来插入数据并在结尾显示(这是一个赋值,所以我必须根据指令创建临时表)。

我应该在这个存储过程中使用游标。我正在创建临时表,然后我正在创建一个游标。但是在创建表格后我收到错误missing 'end'

CREATE DEFINER=`root`@`localhost` PROCEDURE `walkerMasterSummary`()

BEGIN

DECLARE rowCount INT Default 0;
DECLARE counter INT Default 0;
DECLARE WID char(6);
DECLARE WType_WTID varchar(15);
DECLARE WUID char(6);
DECLARE BattleGroup char(5);
DECLARE CubicAreaWeight decimal(10,2);
DECLARE FuelCapacity decimal(10,2);
DECLARE FuelExpenditure_Mile decimal(10,2);
DECLARE ArmorWeight decimal(10,2);
DECLARE StructureWeight decimal(10,2);
DECLARE Status VARCHAR(20);
DECLARE CostToRepair int(10);


DROP temporary table walker_master;

CREATE temporary table walker_master(
    WID char(6),
WType_WTID varchar(15),
WUID char(6),
BattleGroup char(5),
CubicAreaWeight decimal(10,2),
FuelCapacity decimal(10,2),
FuelExpenditure_Mile decimal(10,2),
ArmorWeight decimal(10,2),
StructureWeight decimal(10,2),
Status VARCHAR(20),
CostToRepair int(10)
);

 DECLARE walkerMaster CURSOR FOR
 SELECT IWA.WID, concat(IWA.WalkerType, " - ", IWT.WTypeID) AS WType_WTID
 , IWA.WUID, WU.BattleGroup, walkerCubicAreaWeight(IWA.WalkerType) AS 
 CubicAreaWeight, walkerFuelCapacity(IWA.WalkerType) AS FuelCapacity
 ,walkerFuelExpenditure(IWA.WalkerType) AS FuelExpenditure_Mile, 
  walkerArmorWeight(IWA.WalkerType) AS ArmorWeight
  ,walkerStructureWeight(IWA.WalkerType) AS StructureWeight, IWA.Status
  , CASE  WHEN IWA.Status = 'Damaged' AND IWA.WalkerType = 'AT-AT' THEN 122 
  * IWT.Weight 
    WHEN IWA.Status = 'Damaged' AND IWA.WalkerType = 'AT-ST' THEN 89 * 
  IWT.Weight
        ELSE 0 END AS CostToRepair
 FROM imperial_walkers_assign IWA
 LEFT JOIN imperial_walker_type IWT ON IWA.WalkerType = IWT.WType
 LEFT JOIN walker_units WU ON IWA.WUID=WU.WUID
ORDER BY IWA.WID;

OPEN walkerMaster;
  BEGIN
SELECT Found_Rows() INTO rowCount;
process_loop : loop
 IF counter < rowCount THEN
 FETCH walkerMaster INTO WID ,WType_WTID, WUID, BattleGroup, 
 CubicAreaWeight, FuelCapacity,
 FuelExpenditure_Mile, ArmorWeight, StructureWeight,Status, CostToRepair;
/* DROP temporary table walker_summary;
CREATE temporary table walker_summary AS SELECT WID ,WType_WTID,WUID, 
 BattleGroup, CubicAreaWeight, FuelCapacity,
 FuelExpenditure_Mile, ArmorWeight, StructureWeight,Status, CostToRepair;*/
 SELECT WID ,WType_WTID, WUID, BattleGroup, CubicAreaWeight, FuelCapacity,
 FuelExpenditure_Mile, ArmorWeight, StructureWeight,Status, CostToRepair;
 SET counter = counter +1;
 ELSE
 leave process_loop;
END IF;
END loop process_loop;
END;
CLOSE walkerMaster;

END

我没有选择消除临时表或游标。我该如何解决这个错误?

1 个答案:

答案 0 :(得分:0)

光标声明应在创建临时表之前

此外,我还没有注释插入临时表

的代码
DECLARE rowCount INT Default 0;
    DECLARE counter INT Default 0;
    DECLARE WID char(6);
    DECLARE WType_WTID varchar(15);
    DECLARE WUID char(6);
    DECLARE BattleGroup char(5);
    DECLARE CubicAreaWeight decimal(10,2);
    DECLARE FuelCapacity decimal(10,2);
    DECLARE FuelExpenditure_Mile decimal(10,2);
    DECLARE ArmorWeight decimal(10,2);
    DECLARE StructureWeight decimal(10,2);
    DECLARE Status VARCHAR(20);
    DECLARE CostToRepair int(10);

    DECLARE walkerMaster CURSOR FOR
     SELECT IWA.WID, concat(IWA.WalkerType, " - ", IWT.WTypeID) AS WType_WTID
     , IWA.WUID, WU.BattleGroup, walkerCubicAreaWeight(IWA.WalkerType) AS 
     CubicAreaWeight, walkerFuelCapacity(IWA.WalkerType) AS FuelCapacity
     ,walkerFuelExpenditure(IWA.WalkerType) AS FuelExpenditure_Mile, 
      walkerArmorWeight(IWA.WalkerType) AS ArmorWeight
      ,walkerStructureWeight(IWA.WalkerType) AS StructureWeight, IWA.Status
      , CASE  WHEN IWA.Status = 'Damaged' AND IWA.WalkerType = 'AT-AT' THEN 122 
      * IWT.Weight 
        WHEN IWA.Status = 'Damaged' AND IWA.WalkerType = 'AT-ST' THEN 89 * 
      IWT.Weight
            ELSE 0 END AS CostToRepair
     FROM imperial_walkers_assign IWA
     LEFT JOIN imperial_walker_type IWT ON IWA.WalkerType = IWT.WType
     LEFT JOIN walker_units WU ON IWA.WUID=WU.WUID
    ORDER BY IWA.WID;

    DROP temporary table walker_master;

    CREATE temporary table walker_master(
        WID char(6),
    WType_WTID varchar(15),
    WUID char(6),
    BattleGroup char(5),
    CubicAreaWeight decimal(10,2),
    FuelCapacity decimal(10,2),
    FuelExpenditure_Mile decimal(10,2),
    ArmorWeight decimal(10,2),
    StructureWeight decimal(10,2),
    Status VARCHAR(20),
    CostToRepair int(10)
    );



    OPEN walkerMaster;
      BEGIN
    SELECT Found_Rows() INTO rowCount;
    process_loop : loop
     IF counter < rowCount THEN
     FETCH walkerMaster INTO WID ,WType_WTID, WUID, BattleGroup, 
     CubicAreaWeight, FuelCapacity,
     FuelExpenditure_Mile, ArmorWeight, StructureWeight,Status, CostToRepair;

    insert into walker_summary (WID,WType_WTID,WUID,BattleGroup,CubicAreaWeight,FuelCapacity,FuelExpenditure_Mile,ArmorWeight,StructureWeight,
    StructureWeight,Status,CostToRepair)
    SELECT WID ,WType_WTID,WUID, 
     BattleGroup, CubicAreaWeight, FuelCapacity,
     FuelExpenditure_Mile, ArmorWeight, StructureWeight,Status, CostToRepair;
     SELECT WID ,WType_WTID, WUID, BattleGroup, CubicAreaWeight, FuelCapacity,
     FuelExpenditure_Mile, ArmorWeight, StructureWeight,Status, CostToRepair;
     SET counter = counter +1;
     ELSE
     leave process_loop;
    END IF;
    END loop process_loop;
    END;
    CLOSE walkerMaster;

    END