Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trigger,

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?
Go to Top of Page

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.
Go to Top of Page

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?
Go to Top of Page

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 like

insert into Candidatesteps ( . . . )
select . . .
from inserted i



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

atmonline
Starting Member

14 Posts

Posted - 2008-06-24 : 16:55:12
The following is the trigger in resumesreceived table


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER TRIGGER [ins_ResumesReceived] ON [dbo].[ResumesReceived]
FOR INSERT
AS
-- 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 INT
DECLARE @JobID INT
DECLARE @EvaluationStepID INT

SELECT @JobID= ins.JobID,
@ResumeReceivedID= ins.ResumeReceivedID
FROM inserted ins

/* if Exam Plan is set up for this job perform an insert */
SELECT @EvaluationStepID = es.EvaluationStepID
FROM EvaluationSteps es, ExamPlanJobMat ejmat
WHERE es.EvaluationStepTypeID = 1
AND es.ExamPlanID = ejmat.ExamPlanID
AND ejmat.JobID = @JobID


IF @EvaluationStepID > 0
BEGIN
INSERT INTO CandidateSteps
( ResumeReceivedID
,EvaluationStepID
,CreatedDate
,LastUpdatedDate )

VALUES ( @ResumeReceivedID
,@EvaluationStepID
,GetDate()
,GetDate() )


END





Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-24 : 17:11:41
http://weblogs.sqlteam.com/tarad/archive/2004/09/14/2077.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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 table


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER TRIGGER [ins_ResumesReceived] ON [dbo].[ResumesReceived]
FOR INSERT
AS
-- 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 INT
DECLARE @JobID INT
DECLARE @EvaluationStepID INT

SELECT @JobID= ins.JobID,
@ResumeReceivedID= ins.ResumeReceivedID
FROM inserted ins

/* if Exam Plan is set up for this job perform an insert */
SELECT @EvaluationStepID = es.EvaluationStepID
FROM EvaluationSteps es, ExamPlanJobMat ejmat
WHERE es.EvaluationStepTypeID = 1
AND es.ExamPlanID = ejmat.ExamPlanID
AND ejmat.JobID = @JobID


IF @EvaluationStepID > 0
BEGIN
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.
Go to Top of Page
   

- Advertisement -