Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I want to insert last insert query to the another column.For that i wish to use TRIGGERS

--
-- Triggers `sync_log`
--
DROP TRIGGER IF EXISTS `tg_sync_id_insertbgnn`;
DELIMITER //
CREATE TRIGGER `tg_sync_id_insertbgnn` BEFORE INSERT ON `student_master`
FOR EACH ROW BEGIN
INSERT INTO `sync_log`(`Sync_query`) SELECT * FROM student_master WHERE `SM_NO` = NEW.`SM_NO`;
END
DELIMITER ;

This doesn't seems to work.Any idea why ?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You need three changes

CHANGE #1

Add // after end

CHANGE #2

Just insert NEW.SM_NO directly into sync_log

CHANGE #3

Make it an AFTER INSERT trigger, to be sure the new SM_MO lands in student_master

--
-- Triggers `sync_log`
--
DROP TRIGGER IF EXISTS `tg_sync_id_insertbgnn`;
DELIMITER //
CREATE TRIGGER `tg_sync_id_insertbgnn` AFTER INSERT ON `student_master`
FOR EACH ROW BEGIN
INSERT INTO `sync_log`(`Sync_query`) VALUES (NEW.`SM_NO`);
END //
DELIMITER ;

Give it a Try !!!

share|improve this answer
    
What is the different between // and $ –  samitha Mar 10 at 3:28
    
I want to save the newLy inserted query like this INSERT INTO student_master () VALUES (); –  samitha Mar 10 at 3:38
add comment

You are trying to select a record in student_master which is currently inserted. Because you use before insert it is very likely that the result of your select is empty (assuming that SM_NO is your PK). Change it to after insert or just use the values from NEW to create a log entry. It isn't very useful to let the server first insert a record and after that reread it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.