1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
對(duì)已經(jīng)存在的一張表,要把該表的表結(jié)構(gòu)和數(shù)據(jù)復(fù)制到另一張新表中,可以采用的方法有兩種。
方法一
select * into test01_02 from test01_01;
把test01_01的表結(jié)構(gòu)和數(shù)據(jù)(如果有數(shù)據(jù))導(dǎo)入到test01_02表中。
注:使用這種方法的前提是test01_02表是不存在的,如果存在執(zhí)行SQL語句時(shí)會(huì)報(bào)錯(cuò)。
方法二
set identity_insert test01_03 on
insert into test01_03(id,p_name,p_age,p_address) select * from test01_01
set identity_insert test01_03 off
把test01_01的數(shù)據(jù)導(dǎo)入到test01_03表中,其中id為主鍵,整型,自動(dòng)增長(zhǎng)。
注:
a.方法二要求test01_03表存在,不存在會(huì)報(bào)錯(cuò)。
b.如果把方法二改寫為:insert into test01_03 select * from test01_01,會(huì)報(bào)“僅當(dāng)使用了列列表并且 IDENTITY_INSERT 為 ON 時(shí),才能為表'test01_03'中的標(biāo)識(shí)列指定顯式值”錯(cuò)誤。
c.如果把方法二改寫為:
set identity_insert test01_03 on
insert into test01_03 select * from test01_01
set identity_insert test01_03 off
會(huì)報(bào)“僅當(dāng)使用了列列表并且 IDENTITY_INSERT 為 ON 時(shí),才能為表'test01_03'中的標(biāo)識(shí)列指定顯式值”錯(cuò)誤。
這樣把表的表結(jié)構(gòu)和數(shù)據(jù)復(fù)制到另一張新表中的,表中的主鍵也就復(fù)制過去了。
- 1 回答
- 0 關(guān)注
- 897 瀏覽
添加回答
舉報(bào)