else

hgb9j2n6  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(236)

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

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列
我只是想澄清一下。谢谢您

5lwkijsr

5lwkijsr1#

你需要使用 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演示

相关问题