插入备份数据 报 IDENTITY_INSERT 为 ON 时解决方法

SQL Server 2008 R2 x64 下

事情是这样的, 使用Navcate的数据导出备份工具导出表的记录

INSERT INTO [xxx_users] VALUES (1, 'tom ', 'tom@153.com ', '13432145689 ', ' ', '浜屽疂 ', NULL);
GO
INSERT INTO [xxx_users] VALUES (2, 'zj_a ', NULL, NULL, 'e10adc3949ba59abbe56e057f20f883e', NULL, NULL);
GO

因为 [xxx_users]表有一列是自增长的标识列,所以当执行上面的SQL是报错的,提示 IDENTITY_INSERT 为 ON 之类的

解决方法一,干脆将INSERT 要插入的VALUES值去掉自增字段


INSERT INTO [xxx_users] VALUES ('tom', 'tom@153.com', '13432145689', '', '浜屽疂', NULL);
GO
INSERT INTO [xxx_users] VALUES ('zj_a', NULL, NULL, 'e10adc3949ba59abbe56e057f20f883e', NULL, NULL);
GO

解决方法二:标注每个字段名

SET IDENTITY_INSERT [dbo].[tbiz_UserInfo] ON;
INSERT INTO [dbo].[tbiz_UserInfo]
           ([UserID],[isConfirmAgreement] ,[CompanyID]
           ,[UserName],[Password],[Contract]
           ,[Mobile]
           ,[Email]
           ,[RoleID]
           ,[CreatedBy]
           ,[CreatedTime]
           ,[LastUpdatedBy]
           ,[LastUpdatedTime]
           ,[Enabled]
           ,[State]
           ,[IsMain]
           ,[UserType]
           ,[VerificationCode]
           ,[MaxPurchaseCount]
           ,[MaxUpdateCount])
     VALUES
           (10 ,NULL,0
           ,'admin','9bf6fd3a108b93e9e4e323ffc2a17929','管理员'
           ,NULL
           ,NULL
           ,1
           ,NULL
           ,GETDATE()
           ,NULL
           ,NULL
           ,1
           ,1
           ,NULL
           ,NULL
           ,NULL
           ,NULL
           ,NULL);
SET IDENTITY_INSERT [dbo].[tbiz_UserInfo] OFF

 

posted @ 2016-04-23 10:25  轴轴  阅读(3585)  评论(0编辑  收藏  举报