i am trying to create a table with a row where id=0 but the identity insert column starts at 1,1 so all following use the identity insert

我正在尝试创建一个具有id = 0的行的表,但标识插入列从1,1开始,因此以下所有使用标识插入

i have to execute

我必须执行

DBCC CHECKIDENT ('Foo', RESEED, 0);

after i manually insert (using a script) to get the next row to have id=1 is there another way to get id=0 for my first row which is from the insert script and all others that follow start at 1 so I dont have to call the dbcc checkident

在我手动插入(使用脚本)以使下一行具有id = 1之后,还有另一种方法来获取我的第一行的id = 0,这是来自插入脚本,其他所有其他行都是从1开始所以我没有调用dbcc checkident

create the following table

创建下表

CREATE TABLE [dbo].[Foo](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CreatedAt] [datetime] NOT NULL CONSTRAINT DF_Foo_CreatedAt DEFAULT getdate(),
    [ModifiedAt] [timestamp] NOT NULL,
    [Code] [nvarchar](50) NOT NULL,
    [Name] [nvarchar](50) NULL,
 CONSTRAINT [PK_Foo] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
) ON [PRIMARY]
) ON [PRIMARY]

i want a row which has an id=0 so i run the following next

我想要一个id为0的行,所以我接下来运行以下内容

SET IDENTITY_INSERT [dbo].[Foo] ON;
INSERT INTO [dbo].[Foo] ([Id],[Code],[Name]) VALUES (0, 'Default', 'Regular');
SET IDENTITY_INSERT [dbo].[Foo] OFF;

then i have to execute

然后我必须执行

DBCC CHECKIDENT ('Foo', RESEED, 0);

DBCC CHECKIDENT('Foo',RESEED,0);

to get all following inserts to start from 1

让所有后续插入从1开始

2 个解决方案

#1


Ben,

From what I understand you want the identity column to start with 0 instead of 1?

根据我的理解,您希望标识列以0而不是1开头?

If that's what you want, you must change the initial seed IDENTITY(seed, increment)

如果这是你想要的,你必须改变初始种子IDENTITY(种子,增量)

change the create table to:

将create table更改为:

[Id] [int] IDENTITY(0,1) NOT NULL,

[Id] [int] IDENTITY(0,1)NOT NULL,

更多相关文章

  1. 脚本与SQL Server中的表并发运行
  2. 个人网站对xss跨站脚本攻击(重点是富文本编辑器情况)和sql注入攻击
  3. 利用Shell脚本将MySQL表中的数据转化为json格式
  4. Scripts:创建手工的SQL PROFILE的脚本,老外写的很好用coe_xfr_sql
  5. mysql转存sql脚本的时候怎么取消外键
  6. PostgreSQL自动备份批处理脚本
  7. 查看 SQL Server 作业(job)运行结果状态脚本
  8. 为什么数据库设计人员不让标识列从最小值开始而不是从1开始?
  9. SQL 日志太大,脚本清除

随机推荐

  1. Android设置android:clipChildren达到的
  2. android的大好时光结束进行时
  3. 王家林的81门一站式云计算分布式大数据&
  4. Android串口Serial服务解析
  5. Android技术专家 高焕堂 推荐这本书
  6. Android音乐播放器系列讲解之一
  7. Android的ps命令介绍和技巧
  8. Android:关于声明文件中android:process
  9. Android 6.0权限机制
  10. Android五大布局详解