如何根據最後一次計數選擇種類。我已經寫了一個查詢以根據最後一次計數重新排序。但是,如何一次執行10個表格。重新定位選定表格的標識值
declare @last int
select @last=max(empid) from Table_1
DBCC CHECKIDENT (Table_1, RESEED, @last)
,但如何爲超過10桌以上的桌......一氣呵成立足於過去數補種
如何根據最後一次計數選擇種類。我已經寫了一個查詢以根據最後一次計數重新排序。但是,如何一次執行10個表格。重新定位選定表格的標識值
declare @last int
select @last=max(empid) from Table_1
DBCC CHECKIDENT (Table_1, RESEED, @last)
,但如何爲超過10桌以上的桌......一氣呵成立足於過去數補種
迭代通過所需的表格做的,然後運行每桌上面的命令。
您必須動態構建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
你不需要選擇@最後= MAX(EMPID )Table_1
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
感謝這讓我擺脫困境的一個偉大的答案。這裏是我的更全面的版本
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
我只好一個'+ 1'添加到您獲得最後的ID位'SELECT @lastid = MAX(「+ @idcol +」)'所以它被設置爲下一個可用的一個,不過它像夢一樣工作。歡呼聲 – Ben
添加+1假設(當然)您的身份正在遞增1.要確切知道它的遞增量,請使用內置的IDENT_INCR('tableName')SQL函數。 [參考](http://msdn.microsoft.com/en-us/library/ms189795.aspx) –
您可能還想爲MAX添加ISNULL檢查(以防您的表爲空)。 – Ashish