Du kan bruge denne forespørgselsbatch til at oprette visningen. Men du skal blive ved med at opdatere det.
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);
Dette er en lagret proc, der tager basistabelnavnet og opretter en visning for den (jeg har pakket ovenstående kode ind i en proc, der tager en 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