重新选择所选表的标识值

时间:2013-07-11 12:53:38

标签: sql-server tsql

如何根据他们的最后一次计数重新选择。我已根据最后一次计数编写了一个查询重新开始。但是如何一次完成10个表。

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

但如何处理超过10个表或更多表...根据最后一次计数一次性重新种植

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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