1

Anybody know what's wrong with my trigger declaration?

CREATE TRIGGER `sch_trigger_curriculum_subject_before_insert` BEFORE INSERT ON  
`sch_curriculum_subject` FOR EACH ROW 
BEGIN
   DECLARE curriculum_code VARCHAR(50);        
   SET curriculum_code = SELECT code FROM sch_curriculum WHERE id=NEW.id;
   SET NEW.`code_name` = CONCAT(curriculum_code,' - ', NEW.code,' - (',NEW.name,')');
END;

The error: SQL Error(1064): You have error in you SQL syntax ..... near '' at line 4.

According to error message, there is something wrong with my curriculum_code declaration. But I cant find out what's wrong.

Thanks in advance

UPDATE: already solved, the problem is on delimiter, here is the working one

DELIMITER $$
CREATE TRIGGER `sch_trigger_curriculum_subject_before_insert` BEFORE INSERT ON  
`sch_curriculum_subject` FOR EACH ROW 
BEGIN
  DECLARE curriculum_code VARCHAR(50);        
  SET curriculum_code = (SELECT code FROM sch_curriculum WHERE id=NEW.id);
  SET NEW.`code_name` = CONCAT(curriculum_code,' - ', NEW.code,' - (',NEW.name,')');
END$$
2
  • 1
    Is curriculum_code returns multiple rows ? Commented Aug 21, 2012 at 15:51
  • @Tornike, no it is not. The problem is on delimiter. Thanks Commented Aug 21, 2012 at 15:57

3 Answers 3

1

You can't assign a SELECT statement to a variable in that fashion. At very least you should place the SELECT statement within parentheses to execute it as a subquery, but better yet:

SELECT code FROM sch_curriculum WHERE id = NEW.id INTO curriculum_code;

Be careful that the query does not return multiple records (one assumes that there is a UNIQUE constraint on the id column, so this probably isn't an issue).

Sign up to request clarification or add additional context in comments.

1 Comment

thx for your suggestion. I can make sure that the query is unique.
0

You need to make sure multiple values are not returned from the SELECT.

Try:

SET curriculum_code = (SELECT code FROM sch_curriculum WHERE id=NEW.id LIMIT 1); 

1 Comment

Yeah. This is one of the error reason. I'll accept your answer later. thx
0

You can't simply SET curriculum_code = some_SQL. I suspect you mean SELECT ... INTO curriculum_code

1 Comment

Thanks for your suggestion. But I think I've found the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.