缺少标识符

[b][/b](—) 

GO
/****** 对象:  Table [dbo].[BS_functions_menu]    脚本日期: 11/16/2010 16:17:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BS_functions_menu](
[function_id] [bigint] IDENTITY(1,1) NOT NULL,
[function_name] [varchar](64) COLLATE Chinese_PRC_CI_AS NOT NULL,
[function_code] [varchar](64) COLLATE Chinese_PRC_CI_AS NOT NULL,
[function_level] [int] NULL,
[assembly_name] [varchar](576) COLLATE Chinese_PRC_CI_AS NULL,
[function_type] [varchar](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ordering] [int] NOT NULL,
[state] [varchar](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[remark] [varchar](128) COLLATE Chinese_PRC_CI_AS NULL,
[parent_func_id] [bigint] NULL,
CONSTRAINT [PK_BS_FUNCTIONS_MENU] PRIMARY KEY NONCLUSTERED
(
[function_id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

------------------
(二)
GO
/****** 对象:  Table [dbo].[BS_functions_menu]    脚本日期: 11/16/2010 16:40:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BS_functions_menu](
[function_id] [bigint] NOT NULL,
[function_name] [varchar](64) COLLATE Chinese_PRC_CI_AS NOT NULL,
[function_code] [varchar](64) COLLATE Chinese_PRC_CI_AS NOT NULL,
[function_level] [int] NULL,
[assembly_name] [varchar](576) COLLATE Chinese_PRC_CI_AS NULL,
[function_type] [varchar](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ordering] [int] NOT NULL,
[state] [varchar](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[remark] [varchar](128) COLLATE Chinese_PRC_CI_AS NULL,
[parent_func_id] [bigint] NULL
) ON [PRIMARY]

比较建立两个表的sql语句可以看出,(二)中没有标识,所以才会出错

SET IDENTITY_INSERT BS_functions_menu  ON
    begin tran
     插入的sql语句...
       IF @@ERROR = 0
          COMMIT TRAN
       ELSE
      ROLLBACK TRAN
SET IDENTITY_INSERT BS_functions_menu  Off
是在插入前把主键自增的约束去掉,插入完成后,在更改为原来的主键自增。如果在插入一条记录别最大的id大一的话
是没有问题了。
-------------------------------------------------------------------------------------
  还有一种方法设置插入数据后重新设置标识列自增;
    --重新设置当前标识值,新标识值为当前最大标识
    declare @max_id int
    SET @max_id = (SELECT MAX(function_id) FROM BS_command_functions)
    DBCC CHECKIDENT(BS_command_functions, RESEED, @max_id)

你可能感兴趣的:(sql,脚本,Go)