使用分區來將非自增表改為自增表 ------------------------------------------------- --創建測試表TestTable001和TestTable002 CREATE TABLE TestTable001 ( ID INT PRIMARY KEY, C1
使用分區來將非自增表改為自增表
------------------------------------------------- --創建測試表TestTable001和TestTable002 CREATE TABLE TestTable001 ( ID INT PRIMARY KEY, C1 VARCHAR(200) ) GO CREATE TABLE TestTable002 ( ID INT IDENTITY PRIMARY KEY, C1 VARCHAR(200) ) GO --向測試表TestTable001插入數據 INSERT INTO TestTable001 SELECT ROW_NUMBER() OVER (ORDER BY object_id) AS RID, name FROM SYS.all_objects GO ------------------------------------------------- --將分區表切換來將TestTable001換入到TestTable002中 ALTER TABLE TestTable001 SWITCH PARTITION 1 TO TestTable002 --使用RESEED重置自增值 DBCC CHECKIDENT('TestTable002',RESEED) --查看當前表中自增值 SELECT IDENT_CURRENT('TestTable002') ------------------------------------------------- --插入測試 INSERT INTO TestTable002(C1) SELECT 'Test' --查看上一次插入的自增ID SELECT @@IDENTITY
--=========================================
妹子來啦