Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL Stored 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 8:46am
Mich_901.6K
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 at 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 at 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
0 OriginalGriff 476
1 Sergey Alexandrovich Kryukov 200
2 CHill60 195
3 TheRealSteveJudge 120
4 F-ES Sitecore 120
0 Sergey Alexandrovich Kryukov 9,061
1 OriginalGriff 7,920
2 Peter Leow 4,774
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,176


Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 19 Jan 2012
Copyright © CodeProject, 1999-2015
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