注册 登录
编程论坛 SQL Server论坛

求助,如何将若干数据库同一数据表查询结果合并

nature_730 发布于 2012-03-01 11:35, 609 次点击
几个数据库的名称分别为aa,aa_2010,aa_2011,aa_2009,表为mxz
我试过
use aa
select * from mxz where 条件
union
use aa_2010
select * from mxz where 条件
union
use aa_2011
select * from mxz where 条件
union
use aa_2009
select * from mxz where 条件
会报错



2 回复
#2
cnfarer2012-03-01 12:51
1.在SQL语句中肯定不能有use...
2.不同数据库表的命名:数据库名..表名  也就是在from子句区别数据库
#3
panxingren2012-03-02 10:19
同意2楼。
select * from aa.mxz where 条件
union all
select * from aa_2010.mxz where 条件
union all
select * from aa_2011.mxz where 条件
union all
select * from aa_2009.mxz where 条件
1