-- Drop Procedure View_Creation_Procedure
DECLARE @procedureExists BIT
EXEC Procedure_Exists 'View_Creation_Procedure', @exists=@procedureExists OUTPUT
IF (@procedureExists = 0)
BEGIN
-- Create a basic procedure
EXEC ('CREATE PROCEDURE View_Creation_Procedure as SELECT 1')
END
EXEC dbo.sp_executesql @statement = N'
ALTER PROCEDURE View_Creation_Procedure
@scopeList VARCHAR(MAX),
@entityName VARCHAR(50)
AS
DECLARE @viewExists BIT
DECLARE @viewName varchar(50)
SET @viewName = @entityName + ''Summary''
EXEC View_Exists @viewName= @viewName, @exists=@viewExists OUTPUT
-- Check if view exists or not
IF (@viewExists = 0)
-- View does not exists
BEGIN
DECLARE @viewCreationStatement nvarchar(MAX)
-- Create a basic view
SET @viewCreationStatement = ''CREATE VIEW '' + @viewName + '' as SELECT 1 as DUMMY'';
EXEC dbo.sp_executesql @statement =@viewCreationStatement
END
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
DECLARE @ClusterName varchar(30), @Pos int
--Format the spaces
SET @scopeList = LTRIM(RTRIM(@scopeList))+ '',''
-- Find position of first occurance of comma
SET @Pos = CHARINDEX('','', @scopeList, 1)
IF (REPLACE(@scopeList, '','', '''') <> '''')
BEGIN
SET @viewCreationStatement = ''ALTER VIEW '' + @viewName + '' as ''
WHILE @Pos >0
BEGIN
-- find first scope
SET @ClusterName = LTRIM(RTRIM(LEFT(@scopeList, @Pos - 1)))
IF (@ClusterName <> '''')
BEGIN
SET @viewCreationStatement = @viewCreationStatement + '' select * from '' + @entityName + ''@'' + @ClusterName + '' where SnapshotVersion =( select max(SnapshotVersion) from '' + @entityName + ''@'' + @ClusterName + '')''
END
SET @scopeList = RIGHT(@scopeList, LEN(@scopeList) - @Pos)
-- Find position of next comma
SET @Pos = CHARINDEX('','', @scopeList, 1)
IF (@Pos > 0)
BEGIN
-- There are more scopse, so append union
SET @viewCreationStatement = @viewCreationStatement + '' union ''
END
END
END
EXEC dbo.sp_executesql @statement =@viewCreationStatement
COMMIT
'
--EXEC View_Creation_Procedure 'CH1StageApp01, BN1StageApp01, AM3PrdApp01', 'Node'
DECLARE @procedureExists BIT
EXEC Procedure_Exists 'View_Creation_Procedure', @exists=@procedureExists OUTPUT
IF (@procedureExists = 0)
BEGIN
-- Create a basic procedure
EXEC ('CREATE PROCEDURE View_Creation_Procedure as SELECT 1')
END
EXEC dbo.sp_executesql @statement = N'
ALTER PROCEDURE View_Creation_Procedure
@scopeList VARCHAR(MAX),
@entityName VARCHAR(50)
AS
DECLARE @viewExists BIT
DECLARE @viewName varchar(50)
SET @viewName = @entityName + ''Summary''
EXEC View_Exists @viewName= @viewName, @exists=@viewExists OUTPUT
-- Check if view exists or not
IF (@viewExists = 0)
-- View does not exists
BEGIN
DECLARE @viewCreationStatement nvarchar(MAX)
-- Create a basic view
SET @viewCreationStatement = ''CREATE VIEW '' + @viewName + '' as SELECT 1 as DUMMY'';
EXEC dbo.sp_executesql @statement =@viewCreationStatement
END
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
DECLARE @ClusterName varchar(30), @Pos int
--Format the spaces
SET @scopeList = LTRIM(RTRIM(@scopeList))+ '',''
-- Find position of first occurance of comma
SET @Pos = CHARINDEX('','', @scopeList, 1)
IF (REPLACE(@scopeList, '','', '''') <> '''')
BEGIN
SET @viewCreationStatement = ''ALTER VIEW '' + @viewName + '' as ''
WHILE @Pos >0
BEGIN
-- find first scope
SET @ClusterName = LTRIM(RTRIM(LEFT(@scopeList, @Pos - 1)))
IF (@ClusterName <> '''')
BEGIN
SET @viewCreationStatement = @viewCreationStatement + '' select * from '' + @entityName + ''@'' + @ClusterName + '' where SnapshotVersion =( select max(SnapshotVersion) from '' + @entityName + ''@'' + @ClusterName + '')''
END
SET @scopeList = RIGHT(@scopeList, LEN(@scopeList) - @Pos)
-- Find position of next comma
SET @Pos = CHARINDEX('','', @scopeList, 1)
IF (@Pos > 0)
BEGIN
-- There are more scopse, so append union
SET @viewCreationStatement = @viewCreationStatement + '' union ''
END
END
END
EXEC dbo.sp_executesql @statement =@viewCreationStatement
COMMIT
'
--EXEC View_Creation_Procedure 'CH1StageApp01, BN1StageApp01, AM3PrdApp01', 'Node'