获取Sql Server 2008中列的最后身份 [英] Get Last Identity of column in Sql Server 2008

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

问题描述

大家好,

我在应用程序中将Sql Server用作数据库.我在其中使用库伦名称-表中的ID(主键以及IsIdentity = Yes).

因此,我需要在保存之前在表单中显示ID.

怎么可能?

问候!
Aman

Hi All,

I am using Sql Server as database in my application. In which i am using coulmn name - ID (primary key and also IsIdentity=Yes) in table.

So I need to show the ID in form before saving it.

How is it possible?

Regards!
Aman

推荐答案

SELECT IDENT_CURRENT(''Yourtable_name'')将为您提供由标识列生成的最后一个值.
请参见此处 [
SELECT IDENT_CURRENT(''Yourtable_name'') will give you last value generated by identity column.
See Here[^]


我不确定您要做什么,但我认为您有问题,因为这是一个标识列,并且在保存记录之前需要表单中的标识值?

简而言之,你不能.

保存记录时,数据库系统会分配一个Identity值,以防止以任何方式重复该值:只有数据库才可以分配该值,并且只有在第一次保存记录时才可以.尝试自行分配它会导致错误.

您所能做的就是保存记录,将其读回以获取ID,然后显示它.

如果您确实需要ID值,那么我强烈建议您改用GUID值:这些值是由代码而不是数据库分配的,并且可以预先生成,并且如果不保存数据则永远不要使用.
I''m not sure exactly what you are trying to do, but I assume you have a problem because this is an identity column and you need the identity value in your form before you save the record?

In short, you can''t.

An Identity value is assigned by the database system when the record is saved, to prevent it being duplicated in any way: only the database can assign the value, and only when teh record is first saved. Attempts to assign it yourself, will cause an error.

All you can do is save the record, read it back to get the ID and then display it.

If you actually need the ID value, then I would strongly suggest you use GUID values instead: these are assigned by your code rather than the database and can be pre-generated and never used if the data is not saved.


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

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