0

I have an issue with my mysql database trigger. I have a table in my database which has two field 'created date' and 'last updated date'. I am using 'TimeStamp' datatype for 'created date' and 'date' datatype for 'last updated date'.

I made a trigger which is used for updating date in 'last updated date' field. My trigger code is here

    CREATE TRIGGER `trigger_name` AFTER Insert ON `table1`
      FOR EACH ROW
    BEGIN
         update table1 set last_updated_date = NOW()
          where id = (select top id from table1)
END;

I have no idea where I am wrong. please suggest me to resolve this problem.

Please frenkly ask if any issue to understand.

thanks

2 Answers 2

2

you have an invalid sql syntax in sub query. use LIMIT instead TOP

The TOP clause works on MSSQL server.

Execute the below query it will work.

DELIMITER $$
CREATE TRIGGER `trigger_name` BEFORE INSERT ON `table1`
FOR EACH ROW
BEGIN
  SET NEW.last_updated_date = NOW(); 
END;$$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

3 Comments

You exucute this query now, I have executed myself its working fine. here I thought you want to update last_updated_date is current time either before or after insert table1 particular record so I have modified like that .
Execute the above query ,just you change table name and field name . It will work. I have executede the above again ,its working fine. Still you are facing issue just send me the error
it gives the error for 'new' keyword for field name.
0
CREATE TRIGGER `trigger_name`
AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN

update table1 set last_updated_date = NOW()
      where id = (select top id from table1)


END;//

Comments

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.