简体   繁体   English

错误1241(21000):触发器中的操作数应包含1列

[英]ERROR 1241 (21000): Operand should contain 1 column(s) in a trigger

MariaDB [final]> delimiter //
MariaDB [final]> create or replace trigger llena_factura
-> after insert on D_FACTURA
-> FOR EACH ROW
-> BEGIN
-> set @precio=(SELECT * from ARTICULOS where Clave_A=NEW.Clave_A);
-> set @sub=NEW.Cantidad*@precio.Precio_V;
-> set @tot=@sub+NEW.Importe;
-> insert into FACTURA values(NULL,CURDATE(), @sub, @tot,1);
-> END;
-> //

Query OK, 0 rows affected (0.12 sec) 查询正常,受影响的0行(0.12秒)

MariaDB [final]> delimiter ;
MariaDB [final]> insert into D_FACTURA values(5.00,10.00,1);
ERROR 1241 (21000): Operand should contain 1 column(s)

why? 为什么? Could you explain to me what is the error in the trigger? 您能告诉我触发器的错误是什么?

MySQL variables are scalars , which means they can contain only one value, not a row. MySQL变量是标量 ,这意味着它们只能包含一个值,不能包含一行。

You can't set a variable to the result of SELECT * ... 您不能将变量设置为SELECT * ...的结果

Nor can you access fields of a variable like @precio.Precio_V . 您也无法访问@precio.Precio_V之类的变量的字段。 There are no fields. 没有字段。 The variable has only one value. 该变量只有一个值。

It looks like you're only using one field from that subquery anyway, so you could set the variable to one column, one row this way: 看来您只不过使用了该子查询中的一个字段,因此可以将变量设置为一列,以这种方式一行:

SELECT Precio_V INTO @precio FROM ARTICULOS WHERE Clave_A = NEW.Clave_A LIMIT 1;
SET @sub = NEW.Cantidad * @precio;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM