触发器更新错误 - 使用的SELECT语句具有不同的列数

时间:2017-05-29 11:16:15

标签: mysql

我的库存变动表上有一个触发器,我正在尝试使用它来更新库存表。但是,一旦第一条记录被写入移动,任何其他记录都会抛出上述错误。

不确定如何解决它。

见下文SQL

DROP TRIGGER IF EXISTS `trigUpdateStock`;

DELIMITER $$

CREATE TRIGGER `trigUpdateStock` AFTER INSERT ON `material_document`
    FOR EACH ROW BEGIN
        SELECT COUNT(MATERIAL) INTO @count FROM `stock` WHERE MATERIAL = NEW.MATERIAL
                                                   AND BATCH = NEW.BATCH;
        IF @count > 0 THEN
            SELECT `MATERIAL`, `BATCH`, `ST_STATUS`, `CONSIGN`, `LOCATION`, `STOCKIST`, `CUSTOMER`, `VENDOR`, `QUANTITY`, `UOM`, `COST`, `VALUE`, `CURRENCY`, `ENTRY_DATE`, `ENTRY_TIME`
                INTO @mat, @batch, @st_status, @cons, @loc, @stkist, @cust, @vend, @qty,
                     @qty, @uom, @cost, @val, @curr, @date, @time
                FROM `stock`
                WHERE `MATERIAL` = NEW.MATERIAL
                  AND `BATCH` = NEW.BATCH;
            SET @qty = @qty + NEW.QUANTITY;
            SET @val = @val + NEW.VALUE;
            SET @cost = @cost + NEW.COST;
            UPDATE `stock` SET `QUANTITY` = @qty,
                               `VALUE` = @val,
                               `COST` = @cost
                         WHERE `MATERIAL` = NEW.MATERIAL
                           AND `BATCH` = NEW.BATCH;
        ELSE
            SELECT ST_STATUS INTO @st_status 
                FROM document_type
                WHERE DOC_TYPE = NEW.DOC_TYPE;
            INSERT INTO `stock` (`MATERIAL`, `BATCH`, `ST_STATUS`, `CONSIGN`, `LOCATION`, `STOCKIST`,                                                  `CUSTOMER`, `VENDOR`, `QUANTITY`, `UOM`, `COST`, `VALUE`, `CURRENCY`,                                            `ENTRY_DATE`, `ENTRY_TIME`)
                        VALUES (NEW.MATERIAL, NEW.BATCH, @st_status, NEW.CONSIGN, NEW.LOCATION,                                                     NEW.STOCKIST, NEW.CUSTOMER, NEW.VENDOR, NEW.QUANTITY, NEW.UOM,                                                  NEW.COST, NEW.VALUE, NEW.CURRENCY, NEW.ENTRY_DATE, NEW.ENTRY_TIME);
        END IF;


    END;$$

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

您在SELECT声明

中选择了少一列

就像你选择了15列

  

SELECT MATERIALBATCHST_STATUSCONSIGNLOCATION,   STOCKISTCUSTOMERVENDORQUANTITYUOMCOST
  VALUECURRENCYENTRY_DATEENTRY_TIME

但是您已指定16个变量来保存

  

INTO @mat,@ batch,@ ststatus,@ @,@ loc,@stkist,@ cust,@ vend,   @qty,                        @qty,@ uom,@ cost,@ val,@curr,@ date,@ time

这就是为什么它会给出错误。