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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert Statement in Trigger

Author  Topic 

invisible777
Starting Member

10 Posts

Posted - 2007-04-30 : 16:07:06
I have a trigger that is supposed to compare inserted data against another table (basically it's supposed to automatically grade an exam or quiz). Below is the trigger I have setup to make this comparison, but when I run it, it inserts a brand new row of data with all Null's except for the 'rightwrong' column. So basically I have two rows, one with the inserted data, one with the trigger data that I just need to combine.

I think it has to do with that last line (the insert line) but I'm not sure how to specify which precise row to update...


Thanks

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-04-30 : 17:20:49
I would probably do this as a update to all records, and not as a triger on the insert.

I think this is what you were looking for



Update a
set a.rightWrong = case i.TestID
when null then 0
else 1
end
from TestAnswers a
Left Join Inserted i
on I.TestID = a.TestID
and i.questionNbr = a.QuestionNbr
and a.answer = i.answer

Go to Top of Page

invisible777
Starting Member

10 Posts

Posted - 2007-04-30 : 17:44:07
Hi Vinnie... everything is being marked with a '1' (correct answer) with that update...

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-01 : 00:16:44
quote:
I have a trigger that is supposed to compare inserted data against another table

Create Trigger insertTestAnswers
on TestAnswers
for insert


You want record to be inserted back to TestAnswers ?
From what i understand it should be an UPDATE trigger. When you insert records into the TestAnswers table, the update trigger will update the column rightWrong. Is it ?




KH

Go to Top of Page
   

- Advertisement -