| Author |
Topic |
|
atmonline
Starting Member
14 Posts |
Posted - 2008-06-20 : 18:40:58
|
| Hi i have a trigger in a table ResumesReceived that fires a trigger for insert.Now that i doing data migration and i am doing batch insert to ResumesReceived.My problem is the trigger fires only once and it affects only one record. i am inserting 35,000 records as one batch.i am planning to stop the trigger and run a stored procedure for that. i have read that i can set a value in one of the columns and verify that it is batch update then don't execute the trigger. but how do i do that |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-21 : 01:28:02
|
| Didnt understand what you mean by it affects only one record. In trigger you can access all records that were inserted in that batch. Can you post your current code and also what you want out of them? |
 |
|
|
atmonline
Starting Member
14 Posts |
Posted - 2008-06-23 : 19:33:30
|
| INSERT INTO ResumesReceived ( JS_UserID, JobID, ProfileID, DateReceived, PaperApplied, Evaluationstepid ) SELECT JobSeekerID, JobNO, ProfileID, ReqDate , 1, es.EvaluationStepid FROM #t2 a left join #t1 b ON (a.seq=b.seq) left join ExamPlanJobMat em ON (em.jobid = b.JobNO) left join Evaluationsteps es ON (es.examplanid = em.examplanid) where es.EvaluationStepTypeID = 1 i am using the above query to insert records into the ResumesReceived table.This table has a trigger that insert new record to another table called Candidatesteps.My problem is if i insert for example 10 records only the first record is being added to the CandidateSteps.so now i am disabling the trigger and running a query to add records to the candidatesteps. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-23 : 23:35:53
|
quote: Originally posted by atmonline INSERT INTO ResumesReceived ( JS_UserID, JobID, ProfileID, DateReceived, PaperApplied, Evaluationstepid ) SELECT JobSeekerID, JobNO, ProfileID, ReqDate , 1, es.EvaluationStepid FROM #t2 a left join #t1 b ON (a.seq=b.seq) left join ExamPlanJobMat em ON (em.jobid = b.JobNO) left join Evaluationsteps es ON (es.examplanid = em.examplanid) where es.EvaluationStepTypeID = 1 i am using the above query to insert records into the ResumesReceived table.This table has a trigger that insert new record to another table called Candidatesteps.My problem is if i insert for example 10 records only the first record is being added to the CandidateSteps.so now i am disabling the trigger and running a query to add records to the candidatesteps.
Then there's certainly some problem with your trigger code. Can you please post it? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-06-23 : 23:39:37
|
quote: My problem is if i insert for example 10 records only the first record is being added to the CandidateSteps
Oh it is very clear that your trigger didn't handle the records in set based. You are assuming trigger fired for every record, which is not necessary true.basically you need to change to something likeinsert into Candidatesteps ( . . . )select . . .from inserted i KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
atmonline
Starting Member
14 Posts |
Posted - 2008-06-24 : 16:55:12
|
| The following is the trigger in resumesreceived tableset ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER TRIGGER [ins_ResumesReceived] ON [dbo].[ResumesReceived] FOR INSERTAS -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;/* damir - 09/01/02 - this TRIGGER inserts a row into CandidateSteps table to step the applicant to 'Application Received' step.*/DECLARE @ResumeReceivedID INTDECLARE @JobID INTDECLARE @EvaluationStepID INTSELECT @JobID= ins.JobID, @ResumeReceivedID= ins.ResumeReceivedID FROM inserted ins/* if Exam Plan is set up for this job perform an insert */SELECT @EvaluationStepID = es.EvaluationStepIDFROM EvaluationSteps es, ExamPlanJobMat ejmatWHERE es.EvaluationStepTypeID = 1AND es.ExamPlanID = ejmat.ExamPlanIDAND ejmat.JobID = @JobIDIF @EvaluationStepID > 0BEGIN INSERT INTO CandidateSteps ( ResumeReceivedID ,EvaluationStepID ,CreatedDate ,LastUpdatedDate ) VALUES ( @ResumeReceivedID ,@EvaluationStepID ,GetDate() ,GetDate() )END |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-25 : 01:14:43
|
quote: Originally posted by atmonline The following is the trigger in resumesreceived tableset ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER TRIGGER [ins_ResumesReceived] ON [dbo].[ResumesReceived] FOR INSERTAS -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;/* damir - 09/01/02 - this TRIGGER inserts a row into CandidateSteps table to step the applicant to 'Application Received' step.*/DECLARE @ResumeReceivedID INTDECLARE @JobID INTDECLARE @EvaluationStepID INTSELECT @JobID= ins.JobID, @ResumeReceivedID= ins.ResumeReceivedID FROM inserted ins/* if Exam Plan is set up for this job perform an insert */SELECT @EvaluationStepID = es.EvaluationStepIDFROM EvaluationSteps es, ExamPlanJobMat ejmatWHERE es.EvaluationStepTypeID = 1AND es.ExamPlanID = ejmat.ExamPlanIDAND ejmat.JobID = @JobIDIF @EvaluationStepID > 0BEGIN INSERT INTO CandidateSteps ( ResumeReceivedID ,EvaluationStepID ,CreatedDate ,LastUpdatedDate ) VALUES ( @ResumeReceivedID ,@EvaluationStepID ,GetDate() ,GetDate() )END
You're assuming that inserted will contain only one value and trying to assign them to variables @JobID & @ResumeReceivedID. But based on how many records you insert, inserted can contain all the inserted records. So instead of storing values to varaibles from inserted you need to join with inserted table in next SELECT and insert it directly to final table Candidatesteps. This is clearly explained in link Tara posted. |
 |
|
|
|