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