如何从sqlserver2008中的表中选择最后插入的id [英] how to select the last inserted id from table in sqlserver2008
问题描述
我有一个称为Persons的表,它是(IdCode:pk,不是null,Identity).现在我想拥有(选择)表中最后插入的IdCode,我该怎么做?当然,我必须将另一个表加入该表,然后从两个表中提取IdCode
最后,我想在Asp.net GridView中显示结果,所以我可以在Function中应用此查询,然后通过烂程序调用此函数,然后在Asp.net连接中使用它吗?
我也在使用SqlServer2008r2,
谢谢您的忠告.
i have a table known as Persons which is (IdCode :pk, not null ,Identity). now i want to have(select) the last inserted IdCode in the table, how can i do that? ofcourse i have to join one another table to this table and then extract IdCode from both of them
at the end i want to show results in Asp.net GridView , so can i apply this query in Function and then call this function by sroted procedure and then use it in Asp.net connection?
i am also using SqlServer2008r2,
thank''s in your advise
推荐答案
U应该这样尝试:
创建过程SPName
@CandidateID VARCHAR(20)输出
始于
-在表格中插入数据后
CandidateID = @@ IDENTITY-它将返回最后插入的ID
结束
那么在前端,您可以使用OutPut参数获取此值.
U should try like that :
CREATE PROCEDURE SPName
@CandidateID VARCHAR(20) OUTPUT
AS BEGIN
-- After inserting data in your table
CandidateID =@@IDENTITY -- it will return last inserted id
END
then in front end u can use OutPut Parameter for getting this value.
您必须使用@@ identity.
SELECT @@ IDENTITY将为您提供最后一条记录
检查此
http://msdn.microsoft.com/en-us/library/ms187342.aspx [ ^ ]
You have to use @@identity.
SELECT @@IDENTITY will give you the last record
Check this
http://msdn.microsoft.com/en-us/library/ms187342.aspx[^]
这篇关于如何从sqlserver2008中的表中选择最后插入的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!