怎样将多个表的合并到同一个表中?

usxue 2005-03-16 09:25:30
比如表A,表B,表C,表D,
他们有一个唯一的字段ID
select a.*,b.*,c.*,d.* into adsf where a.id=b.id and b.id=c.id and c.id=d.id
...全文
212 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xluzhong 2005-03-16
  • 打赏
  • 举报
回复
如果你先建好表,再
insert adsf select a.*,b.*,c.*,d.* from ... where a.id=b.id and b.id=c.id and c.id=d.id
usxue 2005-03-16
  • 打赏
  • 举报
回复
这个不好吧,因为有一些默认值不好设置啊!
dzhfly 2005-03-16
  • 打赏
  • 举报
回复
把三个表分别用企业管理器生成结构的sql脚本,然后编辑一下,合成一个sql脚本,在查询分析器运行一次,建成一个新表吧。
usxue 2005-03-16
  • 打赏
  • 举报
回复
返回的只有表的列名
usxue 2005-03-16
  • 打赏
  • 举报
回复
我可以通过以下的方法来得到所有的列,但怎样用这些列来建立一个表呢?
select distinct col.name from syscolumns col
where exists
(select obj.* from sysobjects obj where col.id=obj.id and obj.xtype='u' and obj.name like 'HRM%')
paoluo 2005-03-16
  • 打赏
  • 举报
回复
的确没有更好的方法了。
子陌红尘 2005-03-16
  • 打赏
  • 举报
回复
select
a.*,
b表字段列表,--除ID字段
c表字段列表,--除ID字段
d表字段列表 --除ID字段
into adsf
where
a.id=b.id and b.id=c.id and c.id=d.id
usxue 2005-03-16
  • 打赏
  • 举报
回复
正因为字段太多了,所以我不想再定义每一个字段,这太麻烦了,呵呵……
wyb0026 2005-03-16
  • 打赏
  • 举报
回复
问题没看明白是不是这个意思
select * into adsf from (select * from a
union
select * from b
union
select * from c
union
select * from d
union) as t
zjcxc 元老 2005-03-16
  • 打赏
  • 举报
回复
如果你先建好表,再
insert adsf select a.*,b.*,c.*,d.* from ... where a.id=b.id and b.id=c.id and c.id=d.id
则没问题
zjcxc 元老 2005-03-16
  • 打赏
  • 举报
回复
因为你用的是into生成表,必须保证列名唯一,所以对于select中再现的同名的列,必须指定另外的名称
jinjazz 2005-03-16
  • 打赏
  • 举报
回复
只能用as指定一下了
Yang_ 2005-03-16
  • 打赏
  • 举报
回复
select a.*,b.b1,b.b2,...,c.c1,...,d.d1,... into adsf where a.id=b.id and b.id=c.id and c.id=d.id
usxue 2005-03-16
  • 打赏
  • 举报
回复
这会出现错误,提示有相同的ID字段

34,871

社区成员

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

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