如何计算尚未插入的行的(自定义ID)? [英] How to calculate the (custom id) for a row which has not been inserted yet?

查看:209
本文介绍了如何计算尚未插入的行的(自定义ID)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此问题的答案如何可以在sql server中自动生成唯一ID 来为表创建自定义ID.它工作得很好.
现在我有一列,其中包含诸如 UID00000001 > UID00000002 等.假设此列中的最后一个值是 UID00000003 .现在,我要计算尚未在我的 .aspx 页之一中通过C#插入的行的值.在这种情况下为 UID00000004 .我怎样才能获得这个价值?任何帮助,将不胜感激.谢谢.

I am using the answer of this question How to automatically generate unique id in sql server to create a custom id for a table.It worked perfectly.
Now I have a column which holds the values such as UID00000001 UID00000002 and so on. Suppose the last value in this column is UID00000003.Now I want to calculate the value for the row which hasn't been inserted yet via C# in one of my .aspx pages.In this case UID00000004. How can I achieve this value? Any help would be appreciated. Thank you.

推荐答案

您需要执行以下查询以获取将为该表生成的下一个标识:

You need to execute this query to get the next identity which will be generated for the table:

SELECT IDENT_CURRENT('table_name')+1; 

对于您的情况,它将有一些其他信息与下一个身份相关联,因此查询将如下所示:

For your case, it will have some other info concatenated with the next identity so the query will be like this:

SELECT 'UID' + RIGHT('00000000' + CAST(IDENT_CURRENT('table_name')+1 AS VARCHAR(8)), 8)

当然,您需要编写C#代码才能将该查询发送到SQL Server.

Of course you will need to write the C# code to send that query to the SQL Server.

话虽如此,请记住这一点:当您从该调用中获取值并将其保留时,如果在保持该值的过程中在该表中插入了一条记录,则该值为否下一个值.

Having said that, keep this in mind: When you get the value from that call and hold onto it, if during the time you are holding the value a record is inserted into that table, then the value is no longer the next value.

如果在您的应用程序中插入记录后需要标识值,请参考

If you need the identiy value after a record is inserted in your application, please refer this answer.

这篇关于如何计算尚未插入的行的(自定义ID)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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