2

大家好,我有点困惑。我只是想问一下这是否是正确的语法。我在其他帖子中尝试了 if else 存储的 proc 查询,但它不起作用。

CREATE PROCEDURE `addBarberCommission`(IN `empID` INT, IN `ioIN` VARCHAR(11))
BEGIN
INSERT INTO `attendance_log` (`emp_id`, `io`) VALUES (`empID`, `ioIN`);
IF (SELECT e.position, e.status, a.io FROM employee e INNER JOIN
attendance_log a WHERE a.emp_id = `empID`
AND e.position = 'Barber'
AND e.status = 'Active'
AND a.io = 'Time In') THEN
INSERT INTO `barber_employee` (`emp_id`, `tot_sal`) VALUES (`empID`, 0);
END IF;
END #

该过程有效,但是当我调用该存储过程时,CALL addBarberCommission(1, 'Time In');它会产生一个

错误代码:1241。操作数应包含 1 列

我只是要求澄清。谢谢

4

1 回答 1

2

您需要使用EXISTS

CREATE PROCEDURE `addBarberCommission`(IN `empID` INT, IN `ioIN` VARCHAR(11))
BEGIN
INSERT INTO `attendance_log` (`emp_id`, `io`) VALUES (`empID`, `ioIN`);
IF EXISTS (SELECT e.position, e.status, a.io    --here
           FROM employee e 
           JOIN  attendance_log a 
           WHERE a.emp_id = `empID`
             AND e.position = 'Barber'
             AND e.status = 'Active'
             AND a.io = 'Time In') THEN
  INSERT INTO `barber_employee` (`emp_id`, `tot_sal`) VALUES (`empID`, 0);
END IF;
END #

DBFiddle 演示

于 2018-07-08T15:13:17.313 回答