MySQL存储过程语法错误

标签 mysql stored-procedures

我尝试为我的存储过程执行以下代码,它在第 14 行抛出错误。我无法找到错误所在,请帮助我解决。

DELIMITER $$
   CREATE PROCEDURE usp_SetGems (
    -- Add the parameters for the stored procedure here
    p_requestid int/* =null */,
    p_akcija int/* =null */)
 BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    -- Insert statements for procedure here
    if p_akcija = 0 then

    declare @v_userId char(36);
    declare @v_vingems int;

    select @v_userId:=r.user_id, @v_vingems := r.value from Requests r
    where r.Id=p_requestid; 

    update Users 
    set balance=balance+@v_vingems
    where id=v_userId;

    else

    declare @v_userrId longtext;
    declare @v_vingemss int;

    select @v_userrId.user_id:=r.user_id, @v_vingemss := r.value from Requests r
    where r.Id=p_requestid;

    update Users 
    set balance=balance-@v_vingems
    where id=v_userrId;
    end if;
 end;  $$    
 DELIMITER ;

最佳答案

您的 Declare 语句是错误的(declare @v_userId char(36),declare @v_vingems int; .....)。以下是您的程序的正确代码:-

DELIMITER $$
   CREATE PROCEDURE usp_SetGems (
    -- Add the parameters for the stored procedure here
    p_requestid int/* =null */,
    p_akcija int/* =null */)
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    -- Insert statements for procedure here
    if p_akcija = 0 then
        BEGIN 
            select r.user_id, r.value INTO @v_userId, @v_vingems from Requests r
            where r.Id=p_requestid; 
            update Users 
            set balance=balance+@v_vingems
            where id=v_userId;
        END;
    else
        BEGIN 
            select r.user_id, r.value INTO @v_userId, @v_vingems from Requests r
            where r.Id=p_requestid; 

            update Users 
            set balance=balance-@v_vingems
            where id=v_userrId;
        END;
    end if;
 end $$ 
 DELIMITER ;

关于MySQL存储过程语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551456/

相关文章:

sql - Sql 触发器是同步的还是异步的?

java - 收到错误 SQLServerException : The result set is closed

Python MySQL 连接器 : caching_sha2_password plugin

mysql - Sonarqube - 使用 MySQL NDB Cluster

oracle - 无法使用 BULK COLLECT 和 FORALL 编译 PL/SQL

sql - 在 IN 子句中使用逗号分隔的参数

php - Laravel 查询,显示一个表中的结果,其 id 不在另一个表中

mysql - mysql索引中的顺序的目的是什么

mysql - 选择 while 而不是 While select 导致问题

mysql - 如何遍历查询结果然后调用MySQL存储过程中的其他存储过程?