GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('sp_ElozoTanevesViewokTorlese') IS NOT NULL 
BEGIN
  DROP PROCEDURE sp_ElozoTanevesViewokTorlese
END
GO

CREATE PROCEDURE sp_ElozoTanevesViewokTorlese
   @pintezmenyId int
	,@pintezmenyAzonosito nvarchar(max)
  ,@pkovTanevId INT
AS
BEGIN
	SET NOCOUNT ON;

  DECLARE @schemaName nvarchar(max) = CONCAT(N'KR_', @pintezmenyAzonosito, N'_Schema')

  DECLARE @viewsXml xml =
    (SELECT v.name AS ViewName
    FROM sys.views v
      INNER JOIN sys.schemas s ON s.schema_id = v.schema_id
        AND s.name = @schemaName
    --WHERE v.name not like N'%_OSSZES'
    FOR XML PATH (N'ViewNames'))
    
  EXEC sp_Global_DeleteViews @schemaName, @viewsXml

  EXEC sp_Global_CreateSchemaViews @pintezmenyId, @pintezmenyAzonosito, @pkovTanevId
END
GO
