对我很重要的一个菜鸟问题!

jhlgxyxl 2004-03-16 10:11:14
从数据库A中将所有数据导入到B数据库中,由于B数据库与A数据库的表结构不是完全一样的.所以存在A中的两张表的内容对应于B中一张表的内容,
即;将数据库A中的表1和表2中的数据联起来导入到数据库B中的表1中,这样的做法可以吗?
...全文
21 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhlgxyxl 2004-03-19
  • 打赏
  • 举报
回复
痛苦呀!!
jhlgxyxl 2004-03-19
  • 打赏
  • 举报
回复
三个表是通过m_id来关联的
jhlgxyxl 2004-03-18
  • 打赏
  • 举报
回复
我的意思并不是以将三张表接接为主,主要是想将三张表的部门数据一起导入到另一数据库的某一表中,
具体怎么写union?
batt 2004-03-18
  • 打赏
  • 举报
回复
不是用union而是用join,看是left outer join 还是right outer join或是inner join。

楼主的三个表中的数据是通过哪个字段关联的?
whhdgy 2004-03-18
  • 打赏
  • 举报
回复
如果是有关联的话,你可以这样写:select a.col1,a.col2,....,b.col1,b.col2 from table1 as a,table2 as b into [server2]..table3 where a.colp=b.colp
其中select 中的字段顺序可打乱,只要符合table3的要求
whhdgy 2004-03-18
  • 打赏
  • 举报
回复
你不用着急,我想知道a中的表1和表2有什么关联地方?
rouqu 2004-03-18
  • 打赏
  • 举报
回复
insert into [data1].dbo.model_table (id,intro,zheng,tech,rel,e_url,mu_id,mu_url,msg_id,msg_name,msg_cont,msg_time)
----------------------
select m_id,intro,zheng,tech,rel,e_url from [data2].dbo.model_table union (select mu_id,mu_url from [data2].dbo.my_upload union (select msg_id,msg_name,msg_cont,msg_time from [data2].dbo.my_msg))

后面的union语句错掉了
Eg:
select m_id,intro,zheng,tech,rel,e_url from [data2].dbo.model_table_1
union all
select m_id,intro,zheng,tech,rel,e_url from [data2].dbo.model_table_2
union all
select m_id,intro,zheng,tech,rel,e_url from [data2].dbo.model_table_3
union all 不去处重复行 union则去处
如果是把几张表的所有列拼接起来 可以这样得到
select identity(int,1,1) id,* into #tem1 from tab1
select identity(int,1,1) id,* into #tem2 from tab2
select identity(int,1,1) id,* into #tem3 from tab3
select [字段列表] from #tem1,#tem2,#tem3 where #tem1.id = #tem2.id and
#tem1.id = #tem3.id
字段列表 = 表名.字段名,...

再有,数据库里面的用户表很多吗?需要批处理?


jhlgxyxl 2004-03-18
  • 打赏
  • 举报
回复
继续支持我呀?我还没解决呢?
lmcsdn 2004-03-18
  • 打赏
  • 举报
回复
怎么不缺少了,自己再数一下
union 并不是说三个union 和起来的数量,而是select m_id,intro,zheng,tech,rel,e_url
的数量
jhlgxyxl 2004-03-17
  • 打赏
  • 举报
回复
insert into [data1].dbo.model_table (id,intro,zheng,tech,rel,e_url,mu_id,mu_url,msg_id,msg_name,msg_cont,msg_time)
select m_id,intro,zheng,tech,rel,e_url from [data2].dbo.model_table union (select mu_id,mu_url from [data2].dbo.my_upload union (select msg_id,msg_name,msg_cont,msg_time from [data2].dbo.my_msg))

将data2数据库中的三个表中的内容合并导入到数据库data1中,用上面的语句它提示一些奇怪的问题:
服务器: 消息 120,级别 15,状态 1,行 2
INSERT 语句的选择列表包含的项少于插入列表中的项。SELECT 语句中值的数目必须与 INSERT 语句中列的数目匹配。
我数了一下又不缺的,请问代码有没有语法错误呀?
taoxianxue 2004-03-17
  • 打赏
  • 举报
回复
up
zjs5287703 2004-03-17
  • 打赏
  • 举报
回复
是不是SQLServer2000,有问题啊,重装
jhlgxyxl 2004-03-17
  • 打赏
  • 举报
回复
怎么人才这么淡的,还是没人会呀!
花鱼片 2004-03-16
  • 打赏
  • 举报
回复
如2楼所诉,在from里加
select a,b,c,d from table(X)
union
就可以了
jhlgxyxl 2004-03-16
  • 打赏
  • 举报
回复
如果有四张表的内容凑起来导入到一张表中,那又该怎么写呢?在线等!!分不够再加!!!
只要能解决就行!!!
jhlgxyxl 2004-03-16
  • 打赏
  • 举报
回复
哦,那么取table1和table2中的某几项应该也是可以的吧
如果取table1和table2中的几项,是不是这样写的

insert into B..table1
select *
from ( select a,b,c,d from table1
union
select A,B,C,D from table2
) a

a,b,c,d,A,B,C,D分别代表table1,table2中的项
victorycyz 2004-03-16
  • 打赏
  • 举报
回复

insert into B..table1
select *
from ( select * from table1
union all
select * from table2
) a

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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