```
ALTER PROCEDURE [dbo].[ProcessListSQL]  
    ( --@CommaDelimitedList AS NVARCHAR(MAX),
     @SQLtoExecute AS NVARCHAR(MAX),
     @Result VARCHAR(MAX) OUTPUT)
AS 
BEGIN
    DECLARE @Statements TABLE
                        (PK INT IDENTITY(1,1) PRIMARY KEY,
                         SQLObject NVARCHAR (MAX)
                        )

    SET @SQLtoExecute = REPLACE (@SQLtoExecute, '"', '''')
    /*================*/
    DECLARE @CommaDelimitedList  AS NVARCHAR(MAX)
    DECLARE @ddd NVARCHAR(MAX)
    DECLARE @R1 NVARCHAR(MAX)

    SET @ddd=''

    SELECT @ddd = @ddd + organisation  + ',' 
    FROM dbo.Orgs;

    SET @CommaDelimitedList = @ddd   /*select all tables that need to be interrogated*/

    /*================*/
   INSERT INTO @Statements
       SELECT PARAM 
       FROM [dbo].[fn_MVParam](@CommaDelimitedList,',')

    DECLARE @i INT
    SELECT @i = MIN(PK) FROM @Statements

    DECLARE @max INT
    SELECT @max = MAX(PK) FROM @Statements

    DECLARE @SQL AS NVARCHAR(MAX) = NULL
    DECLARE @Object AS NVARCHAR(MAX) = NULL

    WHILE @i <= @max 
    BEGIN       
        SELECT @Object = SQLObject FROM @Statements WHERE PK = @i        /*gets the table from the array table*/
        SET @SQL = REPLACE(@SQLtoExecute, '{RP}', @Object)               /*replaces RP with tablename */
        -- Uncommend below to check the SQL
        -- PRINT @SQL
        DECLARE @returnstatus nvarchar(15);     /*mh did this */
        SET @returnstatus = NULL;               /*mh did this */

        EXECUTE sp_executesql  @SQL,@R1=@R1
        SET @Result = @Result + @R1
        SELECT @Object = NULL
        SELECT @SQL = NULL
        SET @i = @i + 1
    END
END  

```

This works okay but only return the first table the tables collection come from function.