如何在不使用IDENTITY的情况下自动增加列 [英] How can I auto-increment a column without using IDENTITY

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

问题描述

我正在创建一个包含两列的表,我希望自动增加。一列是主键,因此我在其上使用IDENTITY关键字。另一列将用于跟踪表中用户定义的项目排序顺序。每当用户移动项目时,其排序顺序将交换值与另一个元素的值。但是,当项目插入表格时,应始终自动为插入的项目分配高于表格中任何其他值的排序顺序值。这是表创建脚本的简化版本:



这是我的创建表脚本,



< pre lang =c#> CREATE TABLE [dbo]。[tbl_category] ​​

[id] [INT] IDENTITY( 1 ,< span class =code-digit> 1 )NOT NULL,
[category] ​​[VARCHAR]( 50 )NULL,
[last_updated] [DATETIME] NULL,
[priority] [NVARCHAR](max)NOT NULL

ON [PRIMARY]





这里,我还提到了我已经完成的存储过程脚本,

这里你可以看到我的最后一个字段优先级,我想要自动增量优先级字段,

以上你可以看到我的创建表脚本在这里我已经使用了id的身份关键字,

所以,我怎样才能增加优先级字段?



 INSERT INTO dbo .tbl_categorynmpics 
(类别,
la st_updated,
priority)
VALUES(@category,
@imageurl,
Getdate(),
@priority)









谢谢,

解决方案

1.使用如果您正在使用SQL Server 2012,则为序列。



2.使用GUID和NEWID()函数。



3.创建该表的触发器以增加值。





谢谢


Hai

在数据库设计时,你不能在没有主键的情况下自动增加coloum,但在编码时你可以这样做。

首先使它成为整数,读取id coloumn如果没有值赋值1,我有值然后以1递增,这些东西你在插入值时这样做。



以及插入查询中的ur代码u使用3个字段并尝试添加4个版本

  INSERT   INTO  dbo .tbl_categorynmpics 
(类别,
last_updated,
优先级)
VALUES @ category
@ imageurl
Getdate() ,
@ priority





其中imageurl的coloumn ?



你问这样吗?如果你插入值它自动增加值,如果你插入和删除或删除值总是它出现下一个增量值,即使表中没有最后一个值,所以删除自动增量并尝试通过编码给出id



最佳

问候


I'm creating a table with two columns that I want to auto-increment. One column is a primary key, so I'm using the IDENTITY keyword on it. The other column will be used to track the user-defined "sort order" of items in the table. Any time the user moves an item, its "sort order" will swap values with that of another element. However, when an item is inserted into the table, the inserted item should always be auto-assigned a sort-order value higher than any other value in the table. Here's a simplified version of the table creation script:

This is my create table script,

CREATE TABLE [dbo].[tbl_category] 
  ( 
     [id]           [INT] IDENTITY(1, 1) NOT NULL, 
     [category]     [VARCHAR](50) NULL, 
     [last_updated] [DATETIME] NULL, 
     [priority]     [NVARCHAR](max) NOT NULL 
  ) 
ON [PRIMARY] 



Here, i have also mention my stored procedure script which i have done,
here you can see my last field priority, I want auto increment priority field,
above you can see my create table script in this i have already use identity keyword with id,
so, how can i increment with priority field ?

 INSERT INTO dbo .tbl_categorynmpics 
            (category, 
             last_updated, 
             priority) 
VALUES      ( @category, 
              @imageurl, 
              Getdate(), 
              @priority ) 





Thanks,

解决方案

1.Use the "sequences" if you're using SQL Server 2012.

2.Use GUID's and NEWID() function.

3.Create the trigger for that table to increment the values.


Thanks


Hai
You cant make auto increment coloum without primary key,in database design time,but in coding u can do.
first make it id as integer,read id coloumn if dont have value assign 1,i have value then increment with 1,these thing u do when insert value.

And above ur code in insert query u use 3 fields and try to add 4 vlaues

INSERT INTO dbo .tbl_categorynmpics 
            (category, 
             last_updated, 
             priority) 
VALUES      ( @category, 
              @imageurl, 
              Getdate(), 
              @priority ) 



where the coloumn for imageurl ?

And u ask like this ? if u insert value it automatic increment value,if u insert and delete or remove value always it appear next increment value even thoung last value is not availabe in table,so remove auto increment and try to give id via codingd

Best
Regards


这篇关于如何在不使用IDENTITY的情况下自动增加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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