Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would be grateful if you could help me to solve the problem.
I have scope_identity() that implemented in TRIGGER.

SQL
ALTER TRIGGER TempProcTrig
 ON Table_temp2 
  AFTER insert 
AS 
BEGIN  
      declare @TempIdentity int      
      set @TempIdentity =scope_identity()
      insert Table_Temp(TempID)
      values(@TempIdentity) 
END 


When TRIGGER is fired @TempIdentity gets Identety column field, and set this value into another table.

But,allways after TRIGGER is fired @TempIdentity gets NULL.

Why TempIdentity dosen't get Identity field?What should i change in my code?

Thank you in advance.
Posted

scope_identity() function hold the recently added identity value.
You can not use it

but you can try this

SQL
ALTER TRIGGER TempProcTrig
 ON Table_temp2 
  AFTER insert 
AS 
BEGIN  
      insert Table_Temp(TempID)
      SELECT TempID FROM inserted
END 
 
Share this answer
 
Comments
Amir Mahfoozi 19-Jan-12 3:47am    
+5 This is my opinion too. And he should replace TempID with the identity field name of Table_temp2.
RDBurmon 19-Jan-12 5:19am    
Thanks Amir .
first Add Identity Column at your table
Alter Table Table_temp2 Add TempId Int IDENTITY(1,1) Not null
then you can use the code that has already showed you Rahul Dhoble.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900