2

如何根据他们的最后一次计数为选定的种子重新播种。我已经写了一个查询来根据最后一次计数重新播种。但是如何一次做 10 个表。

declare @last int
select @last=max(empid) from Table_1
DBCC CHECKIDENT (Table_1, RESEED, @last)

但是如何处理超过 10 桌或更多桌...根据上次计数一次重新播种

4

3 回答 3

6

遍历您想要的表,然后为每个表运行上述命令。

您必须动态构建 SQL 语句。

例如,对于 SQL Server 2008 R2(您没有指定您使用的是什么),这可以解决问题:

DECLARE @tname SYSNAME, -- Will hold each table name
    @sname SYSNAME,     -- Will hold each table's schema name
    @idcol SYSNAME,     -- Will hold the name of identity column of each table
    @sql NVARCHAR(4000) -- To build each dynamic SQL statement

-- Declare a cursor to iterate through all table and schema names
-- of current database.
-- Add a WHERE clause here if needed.
DECLARE idtables CURSOR FOR
    SELECT name, SCHEMA_NAME(schema_id)
        FROM sys.tables

OPEN idtables

-- Fetch first table and schema name into the corresponding variables
FETCH NEXT FROM idtables INTO @tname, @sname
WHILE @@FETCH_STATUS = 0
BEGIN
    -- Ensure no dirty values if table has no identity column
    SET @idcol = NULL
    -- Build 1st statement.
    -- Objective: get identity column name, if any.
    SET @sql = 'SELECT @idcolname = name
        FROM sys.columns
        WHERE object_id = OBJECT_ID(''' + @sname + '.' + @tname + ''')
            AND is_identity = 1'
    -- Run the statement and store the result into @idcol
    EXEC sp_executesql @sql,
                       N'@idcolname sysname OUTPUT',
                       @idcolname = @idcol OUTPUT
    IF @idcol IS NOT NULL
    BEGIN
        -- Time for the 2nd statement.
        -- Objective: find the maximum identity value and reseed the table.
        SET @sql = 'DECLARE @lastid int;
            SELECT @lastid = MAX(' + @idcol + ')
                FROM [' + @sname + '].[' + @tname + '];
            IF @lastid IS NOT NULL
                DBCC CHECKIDENT (''' + @sname + '.' + @tname + ''', RESEED, @lastid)'
        EXEC SP_EXECUTESQL @sql
    END
    FETCH NEXT FROM idtables INTO @tname, @sname
END

CLOSE idtables
DEALLOCATE idtables
于 2013-07-11T14:26:02.987 回答
0

您不需要从 Table_1 中选择 @last=max(empid)

DBCC CHECKIDENT (Table_1, RESEED) 将获取最后一个 id(如果 empid 是一个标识列)。

此代码将为所有表重新设置种子:

SET NOCOUNT ON
DECLARE @lcl_name VARCHAR(100)
DECLARE cur_name CURSOR FOR

SELECT TABLE_NAME 
FROM information_schema.tables
WHERE TABLE_TYPE = 'BASE TABLE' 

OPEN cur_name
FETCH NEXT FROM cur_name INTO @lcl_name
WHILE @@Fetch_status = 0
BEGIN

DBCC CHECKIDENT (@lcl_name, RESEED);

PRINT @lcl_name
FETCH NEXT FROM cur_name INTO @lcl_name
END
CLOSE cur_name
DEALLOCATE cur_name
SET NOCOUNT OFF
于 2013-09-20T18:51:20.577 回答
0

感谢您的出色回答,使我摆脱了困境。这是我的完整版本

DECLARE @idcol nvarchar(max)
DECLARE @sql nvarchar(max)
DECLARE @sname nvarchar(max)
DECLARE @tname nvarchar(max)

DECLARE idtables CURSOR FOR 
SELECT t.name, s.name 
FROM sys.columns C
    INNER JOIN sys.tables T ON C.object_id = T.object_id
    INNER JOIN sys.schemas s ON S.schema_id = T.schema_id
WHERE is_identity = 1;

OPEN idtables

-- Fetch first table and schema name into the corresponding variables
FETCH NEXT FROM idtables INTO @tname, @sname
WHILE @@FETCH_STATUS = 0
BEGIN
    -- Ensure no dirty values if table has no identity column
    SET @idcol = NULL
    -- Build 1st statement.
    -- Objective: get identity column name, if any.
    SET @sql = 'SELECT @idcolname = name
        FROM sys.columns
        WHERE object_id = OBJECT_ID(''' + @sname + '.' + @tname + ''')
            AND is_identity = 1'
    -- Run the statement and store the result into @idcol
    EXEC sp_executesql @sql,
                       N'@idcolname sysname OUTPUT',
                       @idcolname = @idcol OUTPUT
    IF @idcol IS NOT NULL
    BEGIN
        -- Time for the 2nd statement.
        -- Objective: find the maximum identity value and reseed the table.
        SET @sql = 'DECLARE @lastid int;
            SELECT @lastid = MAX(' + @idcol + ')
                FROM [' + @sname + '].[' + @tname + '];
            IF @lastid IS NOT NULL
                DBCC CHECKIDENT (''' + @sname + '.' + @tname + ''', RESEED, @lastid)'
        EXEC SP_EXECUTESQL @sql
    END
    FETCH NEXT FROM idtables INTO @tname, @sname
END

CLOSE idtables
DEALLOCATE idtables
于 2014-05-17T16:07:53.363 回答