将一个表内容插入一个空表

wxf8886 2005-01-06 06:45:34
各位大侠!我要将一个表中的内容插入到另一个空表,但是两张表的结构有些不同,要挑字段,不知SQL语句该怎么写,请各位帮帮忙
...全文
205 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kshape 2005-01-06
  • 打赏
  • 举报
回复
insert into newtable col1,col2,col3,....select col1,col2,col3,....from oldtable where...
Wanter1 2005-01-06
  • 打赏
  • 举报
回复
--插入一张新表
select col1,col2,col3,....into newtable from oldtable where 条件

--插入到已有的表中
insert newtable(col1,col2,col3,....)
select col1,col2,col3,....from oldtable
where 条件


xluzhong 2005-01-06
  • 打赏
  • 举报
回复
1.建立新表select col1,col2,col3,....into newtable form oldtable where ......

2.insert into newtable col1,col2,col3,....select col1,col2,col3,....from oldtable where...

hai2003xp 2005-01-06
  • 打赏
  • 举报
回复
select top 0 col1,col2,col3... into tab from table1

col1,col2,col3...為你要選擇的字段
realgz 2005-01-06
  • 打赏
  • 举报
回复
--太久不写了,玩。
declare @e varchar(8000)

select @e=''

select @e =@e+c1.name+','
from syscolumns c1,syscolumns c2
where c1.id=object_id('table1') and c2.id=object_id('table2')
and c1.name=c2.name

select @e=case when len(@e)>0 then left(@e,len(@e)-1) end
select @e='insert into table1 ('+@e+') select '+@e+' from table2'
exec (@e)

--如果还要注意什么不为空之类的区别,自己研究系统表
winternet 2005-01-06
  • 打赏
  • 举报
回复
insert into Tb1(Col1,Col2)
select ColA,ColA from Tb2

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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