当前位置: 代码迷 >> Sql Server >> 关于查询出来的表,建立视图的有关问题
  详细解决方案

关于查询出来的表,建立视图的有关问题

热度:223   发布时间:2016-04-24 08:45:02.0
关于查询出来的表,建立视图的问题
根据查询结果,例如:
select name from sysobjects where left(name,1)=c and xtype='u'

结果为:
c2014
c2015
这样的
想建立视图
大概就是
select *
from c2014
union all
select *
from c2015
这样的,,,
等于说查询结果是可变的,所以也就是不一定是几个表的union all,因为查询结果,也可能是c2014,c2015,c2016这样的,,这样的视图怎么建立,,,,,
------解决思路----------------------

select LEFT(name,LEN(name)-9) from
(
select 
(
select 'select * from ' +  name + ' union all '
from sysobjects where left(name,1)='c' and xtype='u' 
FOR XML PATH('')
) as name
) a

  相关解决方案