Recently I wanted to create a StoredProcedure to convert all the views which read millions of records to tables in a database.
The Reason being when we process a cube its always better if it reads from a simple view or a table.
So the ETL(SSIS) job will execute the view which is complicated and has many joins and look ups to a table which can be easily converted to a table and it can be read into the cube.
Here is code to convert all views ending with "_ToTable" to table with same name.
Example : dbo.SalesOrder_ToTable view will be converted to dbo.SalesOrder so having the same schema name (.dbo) as the view .
GO
DECLARE @View_Name varchar(255)
DECLARE MY_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT SCHEMA_NAME(schema_id) +'.'+name AS view_name
FROM sys.views where name like '%_ToTable%'
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @View_Name
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @Tablename_Name varchar(255)
Set @Tablename_Name = Replace(@View_Name,'_ToTable','')
IF OBJECT_ID(@Tablename_Name, 'U') IS NOT NULL Exec('DROP TABLE '+@Tablename_Name)
Exec('Select * into '+@Tablename_Name+' from '+@View_Name )
FETCH NEXT FROM MY_CURSOR INTO @View_Name
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
The Reason being when we process a cube its always better if it reads from a simple view or a table.
So the ETL(SSIS) job will execute the view which is complicated and has many joins and look ups to a table which can be easily converted to a table and it can be read into the cube.
Here is code to convert all views ending with "_ToTable" to table with same name.
Example : dbo.SalesOrder_ToTable view will be converted to dbo.SalesOrder so having the same schema name (.dbo) as the view .
GO
DECLARE @View_Name varchar(255)
DECLARE MY_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT SCHEMA_NAME(schema_id) +'.'+name AS view_name
FROM sys.views where name like '%_ToTable%'
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @View_Name
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @Tablename_Name varchar(255)
Set @Tablename_Name = Replace(@View_Name,'_ToTable','')
IF OBJECT_ID(@Tablename_Name, 'U') IS NOT NULL Exec('DROP TABLE '+@Tablename_Name)
Exec('Select * into '+@Tablename_Name+' from '+@View_Name )
FETCH NEXT FROM MY_CURSOR INTO @View_Name
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
No comments:
Post a Comment