﻿DROP PROCEDURE IF EXISTS dev.uspCreateSchemaSPFN
GO

CREATE PROCEDURE dev.uspCreateSchemaSPFN
   @pRoutinList nvarchar(max) -- Vesszővel elválasztott SP és FN lista (akár vegyesen is), NULL esetén az összeset frissíti
  ,@pSchemaName nvarchar(max) = NULL -- Schema neve, amit frissíteni kell, NULL esetén az összes schemán végigmegy
AS
BEGIN
  SET NOCOUNT ON
  
  DECLARE 
     @spList nvarchar(max)
    ,@fnList nvarchar(max)
  
  IF @pRoutinList IS NULL BEGIN
    EXEC dbo.sp_Global_CreateSchemaStoredProcedures @pSchemaName, NULL
    EXEC dbo.sp_Global_CreateSchemaFunctions @pSchemaName, NULL
    RETURN
  END
  
  SET @spList = (      
    SELECT r.ROUTINE_NAME AS "StoredProcedureName" FROM INFORMATION_SCHEMA.ROUTINES r
    INNER JOIN STRING_SPLIT (@pRoutinList, N',') s ON LTRIM(RTRIM(s.value)) = r.ROUTINE_NAME
    WHERE r.ROUTINE_SCHEMA = 'dbo'
      AND r.ROUTINE_TYPE = 'PROCEDURE'
    FOR XML PATH(''), ROOT('StoredProcedureNames')
  )
  
  SET @fnList = (
    SELECT r.ROUTINE_NAME AS "FunctionName" FROM INFORMATION_SCHEMA.ROUTINES r
    INNER JOIN STRING_SPLIT (@pRoutinList, N',') s ON LTRIM(RTRIM(s.value)) = r.ROUTINE_NAME
    WHERE r.ROUTINE_SCHEMA = 'dbo'
      AND r.ROUTINE_TYPE = 'FUNCTION'
    FOR XML PATH(''), ROOT('FunctionNames')
  )
  
  IF @spList IS NOT NULL BEGIN
    EXEC dbo.sp_Global_CreateSchemaStoredProcedures @pSchemaName, @spList
  END
  IF @fnList IS NOT NULL BEGIN  
    EXEC dbo.sp_Global_CreateSchemaFunctions @pSchemaName, @fnList
  END
END
GO

