如何在sql2008中为ID自动增加? [英] how to make an auto increment for ID in sql2008 ?

查看:91
本文介绍了如何在sql2008中为ID自动增加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对我桌子的陈述



  SELECT  id ,名称,部门
FROM auto





我想要让我的id列从001开始自动递增。

我已经尝试过这个>>



ALTER TABLE dbo.auto AUTO_INCREMENT = 001



但它不起作用



谢谢

解决方案

请参阅 - 带有前导填充零的SQL标识 [ ^ ]。

Quote:

  create   Identitytest(
number int identity 1 001 null
value varchar 500



但它会将它们存储为1,2,3 ...



如果你想用前导零显示它们,那么按照答案...

引用:

如果要显示数字带有前导零的列,只需在 SELECT 语句中填充它。它是一个数字,它不会以前导零作为整数存储。



  SELECT   RIGHT '  00000' + CAST([number]  AS   varchar  5 )), 3 
FROM IdentityTest

< br $>
3 是输出显示中你想要总数的字符数。


This is my statement for my table

SELECT       id, name, department
FROM         auto



I want to make my id column is auto increment start from 001.
I already try this >>

ALTER TABLE dbo.auto AUTO_INCREMENT=001

but its doesn't work

Thanks

解决方案

Refer - SQL Identity with leading padded zeros[^].

Quote:

create table Identitytest(
    number int  identity(1,001) not null,
    value varchar(500)
)


But it will store them as 1, 2, 3 ...

If you want to show them by leading zeros, then follow the answer...

Quote:

If you want to display your number column with leading zeros, just pad it in your SELECT statement. It's a number, it will NOT store with leading zeros as an integer.

SELECT RIGHT('00000' + CAST([number] AS varchar(5)) , 3)
FROM IdentityTest


The 3 is the number of characters you want total in the output display.


这篇关于如何在sql2008中为ID自动增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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