views:

17

answers:

1

I'm looking for the proper syntax (if this is possible in MySQL stored procedures) for using logical operators in an IF THEN statement. Here's something along the lines of what I would like to do, but I'm not certain if I should type "OR" or "||" in the IF ... THEN clause:

DELIMITER $$

CREATE PROCEDURE `MyStoredProc` (_id INT)
BEGIN

  DECLARE testVal1 INT DEFAULT 0;
  DECLARE testVal2 INT DEFAULT 0;

  SELECT value1, value2 INTO testVal1, testVal2 
    FROM ValueTable 
   WHERE id = _id;

 IF testVal1 > 0 OR testVal2 > 0 THEN

   UPDATE ValueTable 
      SET value1 = (value1+1) 
    WHERE id=_id;

 END IF;

END$$
A: 

I haven't tried it with stored procedures, but "or" works for triggers. Here's a working snippet out of some current production code:

CREATE TRIGGER `update_inventory` AFTER INSERT ON `order_updates` FOR EACH ROW 
begin
   if new.type = 'received' or new.type = 'used' or new.type = 'returned' then 
...
Greg Harman