A
A
Alexander2017-09-24 15:03:04
MySQL
Alexander, 2017-09-24 15:03:04

How to fix "Undeclared variable: NEW" error in Trigger?

I'm trying to write a TEST4_B trigger in dbForge to work with a Nest set tree using this example . I successfully did the procedure, but it hangs on the trigger, when the compiler throws out an error: Undeclared variable: NEW .
Here is the code for this trigger :

CREATE 
  DEFINER = 'root'@'%'
TRIGGER hierarchy.test4_bi
  BEFORE INSERT
  ON test4
  FOR EACH ROW
BEGIN
IF (NEW.parent_id IS NULL) THEN
    BEGIN
      set NEW.lb = NULL;
      SELECT MAX(rb) FROM test4 INTO NEW.lb;
      SELECT NEW.lb;
      IF (NEW.lb IS NULL) THEN
        set NEW.lb = 1;
      ELSE
        set NEW.lb = NEW.lb + 1;
      END IF;
    END; 
    ELSE
      BEGIN
        CALL el_test4(NEW.parent_id, 1, -1, -1, @left);
        set NEW.lb = @left;
      END;
    END IF;
  SELECT NEW.lb;
  set NEW.rb = NEW.lb;
END

> Table code:
CREATE TABLE hierarchy.test4 (
  id int(11) NOT NULL,
  parent_id int(11) DEFAULT NULL,
  lb int(11) NOT NULL,
  rb int(11) NOT NULL,
  name varchar(150) NOT NULL,
  PRIMARY KEY (id),
  INDEX test4_x_lb (lb),
  INDEX test4_x_rb (rb),
  CONSTRAINT test4_ibfk_1 FOREIGN KEY (parent_id)
  REFERENCES hierarchy.test4 (id) ON DELETE CASCADE ON UPDATE CASCADE
)
ENGINE = INNODB
AVG_ROW_LENGTH = 1820
CHARACTER SET utf8
COLLATE utf8_general_ci;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fortop, 2017-10-01
@koshalex

Use variable in queries
AND write to NEW.field from variable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question