获取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'];
}