"DECLARE" 在此位置对于此服务器版本无效,应为:错误

"DECLARE" is not valid at this position for this server version, expecting: ERROR

我尽一切可能将 "DECLARE" 放入我的程序 SQL 查询中(使用 MySQL Workbench)及其显示:

"DECLARE" is not valid at this position for this server version, expecting: ERROR.

现在我需要帮助。

DELIMITER //

CREATE PROCEDURE getdetails()
BEGIN 
    DECLARE 
        vin table.vin%type; 
        responsetime table.responseTimeStamp%type;
        odometer table.odometer%type;
        chargePercentage table.soc%type;
        CURSOR sequential_vehicle_status is
                SELECT vin, responseTimeStamp, odometer, soc FROM table ORDER BY vin, responseTimeStamp;
    OPEN sequential_vehicle_status;
    LOOP
    FETCH sequential_vehicle_status into vin, responseTimeStamp, odometer, chargePercentage;
        EXIT WHEN sequential_vehicle_status%notfound;
        dbms_output.put_line(vin || " * " || responseTimeStamp || " * " || odometer || " * " || chargePercentage || "% " ||);
    END LOOP;
    CLOSE sequential_vehicle_status;
END //

DELIMITER ;

您正在组合 mysql 和 oracle 语法。 Dbms_output是oracle中的包,在oracle中使用IS子句创建游标。

    CREATE PROCEDURE getdetails()
        BEGIN 
            DECLARE vin table.vin%type; 
            DECLARE responsetime table.responseTimeStamp%type;
            DECLARE odometer table.odometer%type;
            DECLARE chargePercentage table.soc%type;
            DECLARE CURSOR sequential_vehicle_status for 
                        SELECT vin, responseTimeStamp, odometer, soc FROM table ORDER BY vin, responseTimeStamp;

此外,如果你想打印一些东西到控制台,请使用 select 而不是 dbms_output,这是 oracle 包。

即。 SELECT concat('VIN is ', vin);

这段代码可以工作。 Here is a demo。请将声明的变量调整为您的 table 列类型。

CREATE PROCEDURE getdetails()
BEGIN

DECLARE finished INTEGER DEFAULT 0; --this is added so the exit from loop can be made

DECLARE vin int; 
DECLARE responseTimeStamp int;
DECLARE odometer int;
DECLARE chargePercentage int;
DECLARE sequential_vehicle_status 
cursor for
    SELECT vin
           , responseTimeStamp
           , odometer
           , soc 
    FROM `table` 
    ORDER BY vin, responseTimeStamp;

DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET finished = 1;--this is added so the exit from loop can be made

OPEN sequential_vehicle_status;
start_loop: LOOP

IF finished = 1 THEN 
        LEAVE start_loop;
    END IF;

FETCH sequential_vehicle_status into vin
                                     , responseTimeStamp
                                     , odometer
                                     , chargePercentage;

select concat(vin, '*', responseTimeStamp, '*', odometer, '*', chargePercentage, '% ');                                         

END LOOP;
CLOSE sequential_vehicle_status;

结束;

请阅读对您的问题的评论。 DBMS_OUTPUT 是 oracle 包。 您可以添加以下 dbms_output 行代码来代替:

select concat(vin, '*', responseTimeStamp, '*', odometer, '*', chargePercentage, '% ');