I need to create a view that unions all tables based on their prefix (new tables added monthly)

▼魔方 西西 提交于 2019-12-12 04:43:10

问题


The integration software that my client is using is pulling over ISAM files from a RMS system and creating lots of tables that all have identical schemas, but nonetheless the software they use doesn't support merging them together.

Here's some example table names:

  • SOME_TABLE_XYZ_2012OCT
  • SOME_TABLE_ABC_2012OCT
  • SOME_TABLE_LMN_2012OCT
  • SOME_TABLE_XYZ_2012NOV
  • SOME_TABLE_ABC_2012NOV
  • SOME_TABLE_LMN_2012NOV

I need to roll these all up into a single view named SOME_TABLE, with the data union all'd together. It must be dynamic because there can be new 3 char codes and obviously, monthly new tables being created.

Ideally I'd like a SP that creates a view named SOME_TABLE, then I can query off of SOME_TABLE to do the work that I need on it (scheduled ETL into a sql server 2012 data warehouse). I know that I could just have the SP return a cursor, but creating the view gives me flexibility to make it an indexed view if I need for performance, and ease of use from other ETL tools.

I figure on querying sys.tables (or whatever it is), and working from there to build up a sql statement for a create view.

Any other bright ideas on how to approach this?


回答1:


You can use this query batch to create the view. But you need to keep updating it.

declare @v nvarchar(max) =
    (
        select stuff((
        select cast(' union all select * from ' as nvarchar(max)) + quotename(name)
          from sys.tables
         where name like 'SOME\_TABLE\____\_[0-9][0-9][0-9][0-9][a-Z][a-Z][a-Z]' escape '\'
           for xml path('a'), type
        ).value('.','nvarchar(max)'),1,11,'')
    );
set @v = 'CREATE VIEW SOME_TABLE AS ' + @v;
exec (@v);

This is a stored proc that takes the base table name, and creates a view for it (I wrapped the above code into a proc that takes a parameter)

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE spCreateUnionedView
        @BaseTableName varchar(100)
    AS
    BEGIN
        SET NOCOUNT ON;

        declare @v nvarchar(max) =
        (
            select stuff((
            select cast(' union all select * from ' as nvarchar(max)) + quotename(name)
              from sys.tables
             where name like replace(@BaseTableName, '_', '\_') + '\____\_[0-9][0-9][0-9][0-9][a-Z][a-Z][a-Z]' escape '\'
               for xml path('a'), type
            ).value('.','nvarchar(max)'),1,11,'')
        );

        declare @s nvarchar(max) = 'DROP VIEW ' + @BaseTableName;
        exec (@s);

        set @v = 'CREATE VIEW ' + @BaseTableName + ' AS ' + @v;
        exec (@v);

    END
    GO


来源:https://stackoverflow.com/questions/13635295/i-need-to-create-a-view-that-unions-all-tables-based-on-their-prefix-new-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!