SQL Server表中的AutoID号 [英] AutoID Number in a SQL Server table

查看:171
本文介绍了SQL Server表中的AutoID号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Sql server 2008自动生成ID号,



前样本ID号

A100

B100



这个我的桌子



  CREATE   TABLE 用户
(UserID INT < span class =code-keyword> IDENTITY ( 1 1 NOT NULL ,//仅genarate自动编号
用户名 VARCHAR 50 NOT NULL
密码 VARCHAR 50 NOT NULL

解决方案

您最好在表示层中格式化输出。

您可以使用T-SQL内置工具格式化选择:

  SELECT  '  A' +  RIGHT '  000' +  CONVERT  VARCHAR ,UserID), 3 ),UserName,[密码] 来自用户



但您也可以使用CLR集成功能,因此您可以使用复杂的格式例如,c#,让整个框架落后于你。请参阅: http://www.fotia.co.uk/ fotia / Blog / 2009/05 / indispensable-sql-clr-functions.html [ ^ ]



[更新]

我不确定我有你的想法。特别是序列如何工作。你应该考虑如何从前一个获得一个id,从当前一个获得下一个id。更准确地说,您需要一个将1,2,3 ...映射到您的id格式的函数。如果您将此作为算法,则可以决定要实现它的级别。请注意,您根本不需要身份,您可能只需要一个主键,并且该主键只能是唯一的。


  IDENTITY (数据类型,种子,增量)  -   功能 
IDENTITY (种子,增量) - 属性



数据类型 - >除了位和小数之外,transact-sql中使用的数据类型。

seed - >用于加载到表中的第一行的值。

increment - >是添加到上一行标识值的增量值已加载。



在这里,看看这个链接

IDENTITY(Function) [ ^ ]

IDENTITY(Property) [ ^ ]



现在您可以轻松了解使用Identity的基础。如果你想使用A100,B100,等等,你必须创建自己的逻辑。


搜索(在谷歌上用SQL Server自定义自动生成的序列)。 ......我想你想要那个...同样的问题是听到...希望它能帮助你

http://forums.asp.net/t/1572015.aspx/1 [ ^ ]

http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server [ ^ ]


I want to generate ID number automatically using Sql server 2008,

Ex-sample Id Number
A100
B100

This my table

CREATE TABLE Users
         (UserID INT IDENTITY(1,1) NOT NULL,//Only genarate Auto NUMBER
          UserName VARCHAR(50) NOT NULL,
          Password VARCHAR(50) NOT NULL)

解决方案

You better format your output in the presentation layer.
You can format on selection with T-SQL built-in tools:

SELECT 'A' + RIGHT('000'+ CONVERT(VARCHAR, UserID), 3), UserName, [Password] from Users


But you can also use CLR integrated function also, thus you can make complex formatting using c# for example, having the whole framework behind you. See: http://www.fotia.co.uk/fotia/Blog/2009/05/indispensable-sql-clr-functions.html[^]

[update]
I am not sure I got your idea. Especially how the sequence will work. You should think about how you will get the one id from the previous, and the next from the current one. More precisely you need a function that maps 1,2,3... to your id format. If you have this as an algorithm, you can decide on what level you want to implement it. Be aware, that you don''t need an identity at all, you will probably need only a primary key, and that one has to be unique only.


IDENTITY(datatype,seed,increment)--Function
IDENTITY(seed,increment)--Property


datatype-->Datatypes used in the transact-sql except for bit and decimal.
seed-->Is the value that is used for the very first row loaded into the table.
increment-->Is the incremental value that is added to the identity value of the previous row that was loaded.

Here, have a look at this links
IDENTITY(Function)[^]
IDENTITY(Property)[^]

Now you easily understood the basis of of using Identity. And If you want to use A100,B100,...so on, you have to create your own logic.


search for (Custom Auto-Generated Sequences with SQL Server on google) ....i think u want that...and same question is hear...hope it will help you
http://forums.asp.net/t/1572015.aspx/1[^]
http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server[^]


这篇关于SQL Server表中的AutoID号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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