我们想使用
设置身份种子DBCC CHECKIDENT('myTable',RESEED,0)
但我们的印象是,必须将运行该代码的服务帐户分配给db_ddladmin角色才能执行此操作。这是真的?如果是这样,由于政策原因,这对我们来说是不可取的。
如果是这样,这是我正在考虑的另一种选择:
--Create a test table
/****** Object: Table [dbo].[People2] Script Date: 03/22/2012 19:07:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[People2](
[PersonId] [int] IDENTITY(1,1) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
CONSTRAINT [PK_People2] PRIMARY KEY CLUSTERED
(
[PersonId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
---Proof of concept
declare @seed int = 1000 --Set the seed
SET IDENTITY_INSERT People2 ON
--Insert a dummy record to increment the identity counter to 1 less than the value that we really want
Insert into people2
(PersonId, LastName,FirstName)
values
(@seed-1, 'temp','dummy')
SET IDENTITY_INSERT People2 OFF
--Delete the record that we just inserted (for no otehr purpose than setting the seed)
delete from People2 where personid = @seed-1
--Do your normal code to populate the People2 dest table
Insert into people2
(LastName,FirstName)
values
('Jones','John')
--Verify that the seed was set to the value we wanted
select * from People2
其他注意事项是插入我们使用动态SQL创建的临时表,该动态SQL包含从变量中获取的标识种子值。
不太有吸引力的方法是手动浏览记录并手动增加ID,而不是自动列。
Tally表是可能的,但让我感到困惑。
建议?
答案 0 :(得分:2)
如果您允许在桌面上拥有ALTER
权限,则可以使用SET IDENTITY_INSERT作为第一个值。这会为你更新种子。
从上面关于使用SET IDENTITY_INSERT的链接:
If the value inserted is larger than the current identity value for the table,
SQLServer automatically uses the new inserted value as the current identity value
答案 1 :(得分:0)
为什么不
CREATE TABLE [dbo].[People2](
[PersonId] [int] IDENTITY(1000,1) NOT NULL, ...