如何在scope_identity的帮助下获取身份值 [英] How to get Identity value whith help of scope_identity

查看:83
本文介绍了如何在scope_identity的帮助下获取身份值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能帮助我解决问题,我将不胜感激.
我有在TRIGGER中实现的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 



触发TRIGGER后,@ TempIdentity会获取Identety列字段,并将此值设置到另一个表中.

但是,在触发TRIGGER之后,@ TempIdentity始终为NULL.

为什么TempIdentity无法获得Identity字段?我应该在代码中更改哪些内容?

预先谢谢您.



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.

推荐答案

scope_identity()函数保留最近添加的标识值.
您不能使用它

但是你可以试试这个

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 


首先在表中添加标识列
first Add Identity Column at your table
Alter Table Table_temp2 Add TempId Int IDENTITY(1,1) Not null

然后,您可以使用已经向您展示Rahul Dhoble的代码.

then you can use the code that has already showed you Rahul Dhoble.


这篇关于如何在scope_identity的帮助下获取身份值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆