获取SQL Server 2008中最后插入的ID [英] get last inserted id in sql server 2008

查看:163
本文介绍了获取SQL Server 2008中最后插入的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取特定表的最后插入的ID.谁能告诉我我怎么能做到? 我们在mysql中有mysql_insert_id().在sql server 2008中,我们是否具有类似的功能?

I want to get last inserted id of particular table. can any one tell me how i can get that? we have mysql_insert_id() in mysql. Do we have similar kind of function in sql server 2008?

推荐答案

我使此函数变得容易:)...如果您有多个db资源,只需将其传递给mssql_query请求即可.这将检索在该资源连接上生成的最后一个唯一ID.

I made this function to make it easy :)... if you have more then one db resource just pass that into the mssql_query request. This will retrieve the last unique id generated on that resource connection.

mssql_query("insert into data(name) values('bob')");
$id = getLastId();

function getLastId() {
    $result = mssql_fetch_assoc(mssql_query("select @@IDENTITY as id"));
    return $result['id'];
}

这篇关于获取SQL Server 2008中最后插入的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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