 CREATE TRIGGER before_insert_pendingTask BEFORE INSERT ON pendingTask
 FOR EACH ROW
 BEGIN
-  SELECT projectID INTO @pID FROM task WHERE taskID = NEW.taskID;
-  call check_edit_task(@pID);
+  DECLARE pID INTEGER;
+  SELECT projectID INTO pID FROM task WHERE taskID = NEW.taskID;
+  call check_edit_task(pID);
   IF (NEW.taskID = NEW.pendingTaskID) THEN
     call alloc_error('Task cannot be pending itself.');
   END IF;

CREATE TRIGGER before_delete_pendingTask BEFORE DELETE ON pendingTask
FOR EACH ROW
BEGIN
  SELECT projectID INTO @pID FROM task WHERE taskID = OLD.taskID;
  call check_edit_task(@pID);
END
$$


CREATE TRIGGER before_update_timeSheetItem BEFORE UPDATE ON timeSheetItem
FOR EACH ROW
BEGIN
  DECLARE validDate DATE;
  DECLARE pID INTEGER;
  DECLARE rate BIGINT(20);
  DECLARE rateUnitID INTEGER;
  call check_edit_timeSheet(OLD.timeSheetID);

  SET NEW.timeSheetItemModifiedUser = personID();
  SET NEW.timeSheetItemModifiedTime = current_timestamp();

  SELECT DATE(NEW.dateTimeSheetItem) INTO validDate;
  IF (validDate = '0000-00-00') THEN
    call alloc_error("Invalid date.");
  END IF;

  SET NEW.timeSheetItemID = OLD.timeSheetItemID;
  SET NEW.personID = OLD.personID;
  SELECT projectID INTO pID FROM timeSheet WHERE timeSheet.timeSheetID = NEW.timeSheetID;
  SELECT rate,rateUnitID INTO rate,rateUnitID FROM projectPerson WHERE projectID = pID AND personID = OLD.personID;

  -- if rate is neq project-person rate AND they're don't have perm halt with error about rate
  IF (neq(NEW.rate,rate) AND NOT can_edit_rate(personID(),pID)) THEN
    call alloc_error("Time sheet's rate is not editable.");
  END IF;

  IF (NEW.rate IS NULL AND rate) THEN
    SET NEW.rate = rate;
    SET NEW.timeSheetItemDurationUnitID = rateUnitID;
  END IF;
END
$$
