如何自动递增值 [英] How to auto increment a value

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

问题描述





我有一个要求,如果用户创建新表单,ID值应自动增加。我用SQL做到了。下面是我的代码:



string strcomm = string.format(@IF EXISTS(select * from [DATABASE]。[TABLE]其中ID为NULL和[ArtifactID] ] = @ArtifactID)

开始

更新[数据库]。[TABLE] SET [ID] =(选择CAST(MAX(ISNULL(ID,0))为int )来自[DATABASE]的+1。[TABLE])其中[ArtifactID] = @ArtifactID

END);



问题是,每当用户创建一个任务时,它就会递增值,但是它只递增到10个。无论10之后创建的任务是什么,它只取值为10。任何人都可以帮我吗?



我尝试过:



我是SQL新手,尝试过更改,但没有得到。任何帮助将不胜感激。

解决方案

此解决方案可能会帮助您



创建时表



创建表tablename



id int not null identity(1,1)primary key


  CREATE   TABLE 
ID_column INT NOT NULL IDENTITY 1 1 PRIMARY KEY
...
);





IDENTITY属性会自动从第1列开始增加列。


检查:



SQL AUTO INCREMEN字段 [ ^ ]

Hi,

I have a requirement where if a user creates a new form, the ID value should increment autoamtically. I used SQL to do it. Below is my code:

string strcomm = string.format(@"IF EXISTS(select * from [DATABASE].[TABLE] where ID is NULL and [ArtifactID] = @ArtifactID)
Begin
update [DATABASE].[TABLE] SET [ID]= (Select CAST(MAX(ISNULL(ID,0)) as int)+1 from [DATABASE].[TABLE]) where [ArtifactID] = @ArtifactID
END");

The problem is, whenever user creates a task, it is incrementing the value but, it is incrementing only till 10. Whatever the task created after 10, it is taking the value as 10 only. Can anyone help me over this?

What I have tried:

I am new to SQL and tried changing but didn't get. Any help would be appreciated.

解决方案

This solution may be help you

At the time of create table

create table tablename
(
id int not null identity(1,1) primary key
)


CREATE TABLE (
  ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
  ...
);



IDENTITY property will auto-increment the column up from number 1.


check this:

SQL AUTO INCREMENT a Field[^]


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

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