Click here to Skip to main content
12,509,612 members (52,741 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL SCOPE_IDENTITY , +
I would be grateful if you could help me to solve the problem.
I have scope_identity() that implemented in TRIGGER.

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 18-Jan-12 7:46am
Mich_901.8K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

but you can try this

ALTER TRIGGER TempProcTrig
 ON Table_temp2 
  AFTER insert 
AS 
BEGIN  
      insert Table_Temp(TempID)
      SELECT TempID FROM inserted
END 
  Permalink  
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.
Rahul Dhoble 19-Jan-12 5:19am
   
Thanks Amir .
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 19 Jan 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100