I Have problem in creating after insert trigger.
Actually my requirement is when i try to insert a record in transactions,
trigger should get td_value from control table and update the same value in the transactions tablee,
then increment the id_value by 1.
I wrote trigger as below, but it is giving error in update transaction set..... statement
can some on help in this regards.
create trigger update_transID
on transactions
after insert
as
declare @id_value nvarchar(25)
set @id_value = (select id_value from controls where id='trans_no')
update transactions set trans_id= @id_value where (select trans_id from inserted)
update control set id_value = (@id_value+1)
Tanking You
RamakrishnaPrasanna

Trigger Problem
tom stucki
on transactions
after insert
as
declare @id_value nvarchar(25)
set @id_value = (select id_value from controls with(xlock) where id='trans_no')
update transactions set trans_id= @id_value where trans_id in (select trans_id from inserted)
update control set id_value = (@id_value+1) where id = 'trans_no'
sekhar_nitt
Pluey
Hi,
you should consider a general design mistake that you have. Triggers are fired per statement NOT per row, so using your trigger (although you don’t use valid syntax in your update statement and you use different names for the same table control/controls)
You should also do this the other way, first update the control table, then use the value for the update to the transaction table.
HTH, jens Suessmeyer.
---
http://www.sqlserver2005.de
---
DMX
If your original INSERT statement ALWAYS assigns the same trans_id to everything that is inserted (that is, trans_id is NOT the IDENTITY column, cause identity would assign 2 different values to 2 rows inserted at once), you can try to rewrite your update statement as follows:
UPDATE transactions SET transactions.trans_id = @id_value WHERE transactions.trans_id IN (SELECT trans_id FROM inserted)
empire29
Thanks for the replay, there is a small problem when i execute trigger following statement is not updating the value to the trans_id in the transaction.
update transactions set trans_id= @id_value where trans_id in (select trans_id from inserted)
My requirement is, i want to update the @id_value in transactions table which is inserted recently.
Can you help me in this regards.
Thanking you