SQL实现数据库导表的问题请高人指点下

嗳嘉 2009-05-14 01:53:55
我有两个完全相同的数据库 DATEBASE1 和 DATEBASE2 其中数DATEBASE1中的表没有数据,DATEBASE2中表有数据若干
(表名为 TABLE1)

我想要将DATEBASE2中的数据导入到DATEBASE1中,写

insert into DATEBASE1.dbo.TABLE1 select * from DATEBASE2.dbo.TABLE1

就可以了,但是如果TABLE1中的主键ID有标识列,标识列不能插值。

我使用以下方法后标识列可以插值了
set identity_insert TABLE1 on
insert TABLE1 (id,name) values(2,'abc')
set identity_insert TABLE1 off
但是虽然标识列可以插值了但是我这样写
set identity_insert TABLE1 on
insert into DATEBASE1.dbo.TABLE1 select * from DATEBASE2.dbo.TABLE1
set identity_insert TABLE1 off
提示我
“仅当使用了列的列表,并且identity_insert为on的时候才能在表‘DATEBASE1.dbo.TABLE1’中标识列指定显示值”
这是为什么谁能告诉我




...全文
136 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
嗳嘉 2009-05-14
  • 打赏
  • 举报
回复
各位大大,弱弱的问一下,有没有方法可以不写列名啊?,小弟我要弄的数据库200多表,平均每个表都有十多列的说 T_T
幸运的意外 2009-05-14
  • 打赏
  • 举报
回复
use DATEBASE1
go
set identity_insert TABLE1 on
dbcc checkident(TABLE1,reseed,1)--这里的1应该是表中自增列的起始值
insert into DATEBASE1.dbo.TABLE1(ID,COl1,Col2...) select ID,Col1,Col2... from DATEBASE2.dbo.TABLE1 --必须人为指明列名
set identity_insert TABLE1 off
刚才的语句格式有点小问题,改了一下。楼主见谅了。
幸运的意外 2009-05-14
  • 打赏
  • 举报
回复
use DATEBASE1
go
set identity_insert TABLE1 on
dbcc checkident(TABLE1,reseed,1)--这里的1应该是表中自增列的起始值
insert into DATEBASE1.dbo.TABLE1(ID,COl1,Col2...) select ID,Col1,Col2... from DATEBASE2.dbo.TABLE1 --必须指明列名
set identity_insert TABLE1 off

楼主朋友,每个具有自增列的表都需要用到红色的语句,并且以上四行语句顺序不能错。
第一个语句是允许表在插入时认为提供自增列标识,第二句标识把原表自增列标识回复位初始值
第三句是插入语句,第四句恢复原表的自增列。此时表中的当前标识ID为表中自增列的最大值。
如果数据没有间断,也就是表中记录数。
csdyyr 2009-05-14
  • 打赏
  • 举报
回复
1,仅当使用了列的列表
2, 并且identity_insert为on的时候
  • 打赏
  • 举报
回复
set identity_insert TABLE1 on
insert into TABLE1 select * from DATEBASE2.dbo.TABLE1
set identity_insert TABLE1 off
csdyyr 2009-05-14
  • 打赏
  • 举报
回复
--try:
set identity_insert DATEBASE1.dbo.TABLE1 on
insert into DATEBASE1.dbo.TABLE1 select * from DATEBASE2.dbo.TABLE1
set identity_insert DATEBASE1.dbo.TABLE1 off
lg3605119 2009-05-14
  • 打赏
  • 举报
回复
如果表里有自增列,要显示地把列都指出来,不能使用*来代表全部


set identity_insert TABLE1 on
insert into DATEBASE1.dbo.TABLE1(co1,co2,col3,....) select * from DATEBASE2.dbo.TABLE1
set identity_insert TABLE1 off
贾桂权 2009-05-14
  • 打赏
  • 举报
回复
set identity_insert TABLE1 on 
insert into DATEBASE1.dbo.TABLE1(ID,COl1,Col2...) select ID,Col1,Col2... from DATEBASE2.dbo.TABLE1 --必须指明列名
set identity_insert TABLE1 off

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧