 .NOTE
 Author: Ned Otter
 Original link: http://nedotter.com/archive/2017/10/in-memory-oltp-diagnostic-script/
+Version: 1.0
+
+Modified: 2017-12-06
+Author: Aleksey Nagorskiy
+Version: 1.1
+
+Modified: 2017-12-13
+Author: Konstantin Taranov
+Version: 1.2
+
+Modified: 2017-12-14
+Author: Konstantin Taranov
+Version: 1.3
 */
 AS BEGIN TRY
 

                SELECT @sql += ';WITH InMemDatabases AS (';
            END

            IF @counter <> @MaxRows OR (@counter = 1 AND @counter = @MaxRows)
                SELECT @sql += 
                    CONCAT
                    (
                         @crlf
                        ,'SELECT DISTINCT '
                        , ''''
                        ,  name
                        , ''' AS databaseName,'
                        , database_id
                        , ' AS database_id FROM '
                        , name
                        , '.sys.database_files INNER JOIN '
                        , name
                        , '.sys.filegroups ON database_files.data_space_id = filegroups.data_space_id WHERE filegroups.type = ''FX'''
                    )
            FROM #inmemDatabases
            WHERE rowNumber = @counter;

            IF @debug = 1 PRINT(@sql);
            
            
            SELECT @sql +=
            CASE 
                WHEN @counter = 1 AND @MaxRows = 1 THEN ''  -- there is exactly 1 database for the entire instance
                WHEN @counter = @MaxRows  THEN ''
                WHEN @counter <> 1 AND @counter = @MaxRows -1 THEN ''
                ELSE @crlf + ' UNION ALL ' + @crlf
            END;

            SELECT @counter += 1;
        END;

        AND (name = @dbName OR @dbName = 'ALL')
        AND state_desc = 'ONLINE';

    IF @debug =1 SELECT 'All not system and ONLINE databases' AS AllDatabases, * FROM #inmemDatabases;

    IF @dbName IS NULL AND @instanceLevelOnly = 0
    BEGIN
        SET @errorMessage = '@dbName IS NULL, please specify database name or ALL';
