简体   繁体   中英

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)

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.

You can't set a variable to the result of SELECT * ...

Nor can you access fields of a variable like @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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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