#2014 - 命令不同步;你现在不能运行这个命令;在函数中使用

时间:2017-08-31 13:41:01

标签: mysql phpmyadmin

我在执行此查询时遇到问题:

SELECT func_total_facturas_cliente(cli_codigo,2,"no") AS totalCreditos
FROM cliente

功能如下:

BEGIN
    declare totalAbonos double; 

    declare total cursor for
        SELECT SUM(faca_abono) FROM abono_facturacredito_ventas 
         WHERE cli_codigo = cliCodigo COLLATE latin1_general_ci;

     OPEN total;
        FETCH total INTO totalAbonos;
    CLOSE total;

    RETURN totalAbonos;
END

具体问题是它只在服务器的phpmyadmin中运行时才跳过该错误,我怎样才能改进查询或函数来纠正错误?

提前致谢。

1 个答案:

答案 0 :(得分:0)

这是一个可以帮助你的SQL-Fiddle:

https://www.db-fiddle.com/f/jrwvJHt2PYLsXbhqWVW9FC/1

我也会在这里复制它:

create table abono_facturacredito_ventas 
(
  faca_abono double,
  cli_codigo nvarchar(50)
);

create table clientes 
(
cli_codigo nvarchar(30),
 nombre nvarchar(20)
);

现在插入一些测试数据:

insert into abono_facturacredito_ventas (faca_abono, cli_codigo) values (31,'abc');
insert into abono_facturacredito_ventas(faca_abono, cli_codigo) values (3,'abc');
insert into abono_facturacredito_ventas(faca_abono, cli_codigo) values (32,'abc');
insert into abono_facturacredito_ventas(faca_abono, cli_codigo) values (34,'xyxz');


insert into clientes (cli_codigo, nombre) values ('abc','Ricardo');
insert into clientes (cli_codigo, nombre) values ('xyxz','Caro');

现在声明这个函数(注意:我的语法在我留给你的评论上是错误的):

CREATE FUNCTION func_total_facturas_cliente(codigo nvarchar(50)) RETURNS double
    DETERMINISTIC 
BEGIN

    select SUM(faca_abono) into @totalAbonos from abono_facturacredito_ventas WHERE cli_codigo = codigo;

    return @totalAbonos;
END

现在使用它:

select func_total_facturas_cliente(cli_codigo) as total_abonos, nombre from clientes ;