简体   繁体   English

MSSQL Server 2008 R2中的表串联?

[英]Table concatenation in MSSQL Server 2008 R2?

In my case, I need to put some data into tables by month: 就我而言,我需要按月将一些数据放入表中:

some_table_04
some_table_05
some_table_06

Is there a way to create a view like GBQ: 有没有一种方法可以创建类似GBQ的视图:

SELECT * FROM (TABLE_QUERY([some_table], "table_id like 'some_table_%'";

But in MSSQL Server 2008 R2? 但是在MSSQL Server 2008 R2中?

Or, maybe, table inheritance like Postgres's 或者,也许像Postgres的表继承

CREATE TABLE some_table(...);
CREATE TABLE some_table_04 (...) inherits (some_table);
CREATE TABLE some_table_05 (...) inherits (some_table);
CREATE TABLE some_table_06 (...) inherits (some_table);

And then work with base table? 然后使用基表?

Table concatenation is achieved by UNION ALL : 表连接是通过UNION ALL实现的:

select * from some_table_04
union all
select * from some_table_05
union all
select * from some_table_06
etc.

To do it dynamically, use this code: 要动态地执行此操作,请使用以下代码:

declare @sql varchar(1000)
-- it will select all tables names some_table_**two digits** from your table,
-- I also assumed that these tables are in dbo schema
select @sql = @sql + 'select * from dbname.dbo.' + name + ' union all ' from [dbname].sys.tables
where name like 'some_table_[0-9][0-9]'

exec (@sql)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM