sql server中表ID如果設置爲自動遞增如何實現表與表拷貝

--SQL表中兩個表數據互傳表對錶(用於系統遷移及升級)其中涉及到
--主鍵系統ID自動增長直接對接數據自動增長數據編號同步不了問題處理方式


例如:新增表test2,test3 表


CREATE TABLE [dbo].[test2](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](250) NULL
)


CREATE TABLE [dbo].[test3](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](250) NULL
)
--插入表test2表數據後刪除一些測試用
delete from test2 where ID in(1,2,5,7)


--表test2數據插入test3表中
set identity_insert test3 on
insert into test3(ID,Name)
select ID,Name from  test2
set identity_insert test2 off


--查詢效果OK
select * from test3


最後需要打開自動增長(如果不打開則後續插入test3表後會報錯當 IDENTITY_INSERT 設置爲 ON 或某個複製用戶向 NOT FOR REPLICATION 標識列中插入內容時)
set identity_insert test3 off 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章