如果在mysql的存储过程中

时间:2018-07-08 15:10:33

标签: mysql mysql-workbench mysql-error-1241

大家好,我有点困惑。我只是想问一下这是否是正确的语法。我尝试将if proc存储在其他帖子中的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列

我只是想澄清一下。谢谢

1 个答案:

答案 0 :(得分: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 Demo