seaBass
seaBass

Reputation: 597

MySQL Trigger won't Save

I'm trying to create a function inside a trigger but for some reason I can't get this to save. MySQL keeps giving me Error 1064.

DELIMITER //

CREATE FUNCTION whichTable(vanID VARCHAR(10))
RETURNS VARCHAR(10)

BEGIN
  DECLARE tableName VARCHAR(10);

  IF vanID = 'JAX 04' THEN SET tableName = 'JAX_01';
  ELSEIF vanID = 'ORL 01' THEN SET tableName = 'ORL_01';
  END IF;

  RETURN tableName;
END //

DELIMITER;

INSERT INTO whichTable(NEW.b) (`indx`, `b`, `c`, `d`) VALUES (NULL, NEW.b, NEW.c, NEW.d);

Upvotes: 0

Views: 111

Answers (1)

Ivan Cachicatari
Ivan Cachicatari

Reputation: 4284

You can't evaluate the function as table name for INSERT statement.

But in your trigger you can use the same condition:

IF NEW.b = 'JAX 04' THEN 
    INSERT INTO JAX_01 (`indx`, `b`, `c`, `d`) VALUES (NULL, NEW.b, NEW.c, NEW.d);
ELSEIF NEW.b = 'ORL 01' THEN 
    INSERT INTO ORL_01 (`indx`, `b`, `c`, `d`) VALUES (NULL, NEW.b, NEW.c, NEW.d);
END IF;

Upvotes: 1

Related Questions