	IF (LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX('%Server %', @@VERSION)+7, 5))) > '2008')
	EXEC('DECLARE @AG as NVARCHAR(1000) = '''', @AGH as NVARCHAR(1000) = ''''; IF(IS_SRVROLEMEMBER(''sysadmin'') = 1) SELECT @AG = @AG + IIF(ROW_NUMBER() OVER (ORDER BY ag_name) > 1, '', '', '''') + UPPER(ag_name) FROM sys.dm_hadr_name_id_map JOIN sys.dm_hadr_availability_group_states ON ag_id = group_id; IF(IS_SRVROLEMEMBER(''sysadmin'') = 1) SELECT @AGH = @AGH + IIF(ROW_NUMBER() OVER (ORDER BY agh) > 1, '', '', '''') + agh FROM (SELECT DISTINCT synchronization_health_desc as agh FROM sys.dm_hadr_name_id_map JOIN sys.dm_hadr_availability_group_states ON ag_id = group_id) t; SELECT UPPER(CAST(SERVERPROPERTY(''MachineName'') as VARCHAR)) as MachineName, ISNULL(SERVERPROPERTY(''InstanceName''), '''') as InstanceName, IIF(IS_SRVROLEMEMBER(''sysadmin'') = 1, @AG, '''') as AG, IIF(IS_SRVROLEMEMBER(''sysadmin'') = 1, @AGH, '''') as AGHealth, LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX(''%Server %'', @@VERSION)+7, 5))) as Version, SERVERPROPERTY(''ProductVersion'') as ProductVersion, SUBSTRING(CAST(SERVERPROPERTY(''Edition'') as NVARCHAR), 1, PATINDEX(''% %'', CAST(SERVERPROPERTY(''Edition'') as NVARCHAR)) - 1) Edition, SUBSTRING(@@VERSION, 11, PATINDEX(''% - %'', @@VERSION) - 11) as MSSQLVer, SERVERPROPERTY(''Collation'') as Collation, IS_SRVROLEMEMBER(''sysadmin'') as IsSysadmin, IIF((select count(distinct(collation_name)) from sys.databases where database_id < 5) = 1 and CAST(SERVERPROPERTY(''collation'') as NVARCHAR(50)) = (select collation_name from sys.databases where database_id = 1), 1, 0) AS [CollationIsCorrect];')
ELSE
    EXEC('SELECT UPPER(CAST(SERVERPROPERTY(''MachineName'') as VARCHAR)) as MachineName, ISNULL(SERVERPROPERTY(''InstanceName''), '''') as InstanceName, LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX(''%Server %'', @@VERSION)+7, 5))) as Version, SERVERPROPERTY(''ProductVersion'') as ProductVersion, SUBSTRING(CAST(SERVERPROPERTY(''Edition'') as NVARCHAR), 1, PATINDEX(''% %'', CAST(SERVERPROPERTY(''Edition'') as NVARCHAR)) - 1) Edition, SUBSTRING(@@VERSION, 11, PATINDEX(''% - %'', @@VERSION) - 11) as MSSQLVer, SERVERPROPERTY(''Collation'') as Collation, IS_SRVROLEMEMBER(''sysadmin'') as IsSysadmin;')
go



IF (LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX('%Server %', @@VERSION)+7, 5))) > '2008')
EXEC('
WITH
mf AS (
	SELECT database_id, type, SUM(size) as SumSize
	FROM sys.master_files
	GROUP BY database_id, type
),
bs AS (
	SELECT database_name, type, is_copy_only, max(backup_start_date) as LastBackup
	FROM msdb.dbo.backupset
	GROUP BY database_name, type, is_copy_only
)
SELECT
	@@SERVERNAME as [Server],
	db.database_id as [DBID],
	DB_NAME(db.database_id) as [DBName],
	ROUND(CAST(mfrow.SumSize as float)/128/1024, 1) as RowsGB,
	ROUND(CAST(mflog.SumSize as float)/128/1024, 1) as LogsGB,
	db.state_desc,
	db.recovery_model_desc as RecModel,
	db.log_reuse_wait_desc as LogReuseWait,
	format(bsf.LastBackup, ''yyyy-MM-dd HH:mm:ss'') as LastFullBackup,
	format(bsd.LastBackup, ''yyyy-MM-dd HH:mm:ss'') as LastDiffBackup,
	iif(db.recovery_model = 1, format(bsl.LastBackup, ''yyyy-MM-dd HH:mm:ss''), ''SIMPLE'') as LastLogBackup,
	format(bsc.LastBackup, ''yyyy-MM-dd HH:mm:ss'') as LastFullCopyOnlyBackup
FROM
	sys.databases db
	LEFT JOIN mf as mfrow ON mfrow.database_id = db.database_id AND mfrow.type = 0
	LEFT JOIN mf as mflog ON mflog.database_id = db.database_id AND mflog.type = 1
	LEFT JOIN bs as bsf ON bsf.database_name = DB_NAME(db.database_id) AND bsf.type = ''D'' AND bsf.is_copy_only = 0
	LEFT JOIN bs as bsc ON bsc.database_name = DB_NAME(db.database_id) AND bsc.type = ''D'' AND bsc.is_copy_only = 1
	LEFT JOIN bs as bsd ON bsd.database_name = DB_NAME(db.database_id) AND bsd.type = ''I''
	LEFT JOIN bs as bsl ON bsl.database_name = DB_NAME(db.database_id) AND bsl.type = ''L'' AND bsl.is_copy_only = 0
WHERE db.database_id > 4
ORDER BY 3')
ELSE IF (LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX('%Server %', @@VERSION)+7, 5))) > '2000') EXEC('
WITH
mf AS (
	SELECT database_id, type, SUM(size) as SumSize
	FROM sys.master_files
	GROUP BY database_id, type
),
bs AS (
	SELECT database_name, type, is_copy_only, max(backup_start_date) as LastBackup
	FROM msdb.dbo.backupset
	GROUP BY database_name, type, is_copy_only
)
SELECT
	@@SERVERNAME as [Server],
	db.database_id as [DBID],
	DB_NAME(db.database_id) as [DBName],
	ROUND(CAST(mfrow.SumSize as float)/128/1024, 1) as RowsGB,
	ROUND(CAST(mflog.SumSize as float)/128/1024, 1) as LogsGB,
	db.state_desc,
	db.recovery_model_desc as RecModel,
	db.log_reuse_wait_desc as LogReuseWait,
	bsf.LastBackup as LastFullBackup,
	bsd.LastBackup as LastDiffBackup,
	bsl.LastBackup as LastLogBackup,
	bsc.LastBackup as LastFullCopyOnlyBackup
FROM
	sys.databases db
	LEFT JOIN mf as mfrow ON mfrow.database_id = db.database_id AND mfrow.type = 0
	LEFT JOIN mf as mflog ON mflog.database_id = db.database_id AND mflog.type = 1
	LEFT JOIN bs as bsf ON bsf.database_name = DB_NAME(db.database_id) AND bsf.type = ''D'' AND bsf.is_copy_only = 0
	LEFT JOIN bs as bsc ON bsc.database_name = DB_NAME(db.database_id) AND bsc.type = ''D'' AND bsc.is_copy_only = 1
	LEFT JOIN bs as bsd ON bsd.database_name = DB_NAME(db.database_id) AND bsd.type = ''I''
	LEFT JOIN bs as bsl ON bsl.database_name = DB_NAME(db.database_id) AND bsl.type = ''L'' AND bsl.is_copy_only = 0
WHERE db.database_id > 4
ORDER BY 3')
ELSE EXEC('
SELECT
	@@SERVERNAME as [Server],
	db.dbid as [DBID],
	DB_NAME(db.dbid) as [DBName],
	ROUND(CAST(mfrow.SumSize as float)/128/1024, 1) as RowsGB,
	ROUND(CAST(mflog.SumSize as float)/128/1024, 1) as LogsGB,
	bsf.LastBackup as LastFullBackup,
	bsd.LastBackup as LastDiffBackup,
	bsl.LastBackup as LastLogBackup
FROM
	sysdatabases db
	LEFT JOIN (
		SELECT dbid, groupid, SUM(size) as SumSize
		FROM sysaltfiles
		GROUP BY dbid, groupid
	) as mfrow ON mfrow.dbid = db.dbid AND mfrow.groupid = 1
	LEFT JOIN (
		SELECT dbid, groupid, SUM(size) as SumSize
		FROM sysaltfiles
		GROUP BY dbid, groupid
	) as mflog ON mflog.dbid = db.dbid AND mflog.groupid = 0
	LEFT JOIN (
		SELECT database_name, type, max(backup_start_date) as LastBackup
		FROM msdb.dbo.backupset
		GROUP BY database_name, type
	) as bsf ON bsf.database_name = DB_NAME(db.dbid) AND bsf.type = ''D''
	LEFT JOIN (
		SELECT database_name, type, max(backup_start_date) as LastBackup
		FROM msdb.dbo.backupset
		GROUP BY database_name, type
	) as bsd ON bsd.database_name = DB_NAME(db.dbid) AND bsd.type = ''I''
	LEFT JOIN (
		SELECT database_name, type, max(backup_start_date) as LastBackup
		FROM msdb.dbo.backupset
		GROUP BY database_name, type
	) as bsl ON bsl.database_name = DB_NAME(db.dbid) AND bsl.type = ''L''
WHERE db.dbid > 4
ORDER BY 3')
--
go



IF (LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX('%Server %', @@VERSION)+7, 5))) > '2008')
EXEC('
SELECT
	database_name as [DB],
	IIF(type = ''L'', ''LOG'','''')+IIF(type = ''I'', ''DIF'','''')+IIF(type = ''D'', ''FULL'','''')+IIF(is_copy_only = 1, ''_CO'','''')+IIF(type not in (''L'', ''I'', ''D''), ''type=''+type+'' - ERROR?'','''') as BackupType,
	user_name,
	cast(1.0*backup_size/1024.0/1024.0/1024.0 as numeric(15,2)) as BkpSizeGB,
	cast(1.0*compressed_backup_size/1024.0/1024.0/1024.0 as numeric(15,2)) as CompressedSizeGB,
	format(backup_start_date, ''yyyy-MM-dd HH:mm:ss'') as [Start],
	format(backup_finish_date, ''yyyy-MM-dd HH:mm:ss'') as [End],
	format((backup_finish_date-backup_start_date), ''HH:mm:ss'') as [Duration]
FROM msdb.dbo.backupset
WHERE DB_ID(database_name) > 4 and backup_start_date > GETUTCDATE() - 14
ORDER BY database_name, backup_start_date, type')
ELSE IF (LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX('%Server %', @@VERSION)+7, 5))) > '2000') EXEC('
SELECT
	database_name as [DB],
	type,
	is_copy_only,
	user_name,
	cast(1.0*backup_size/1024.0/1024.0/1024.0 as numeric(15,2)) as BkpSizeGB,
	backup_start_date as [Start],
	backup_finish_date as [End],
	(backup_finish_date-backup_start_date) as [Duration]
FROM msdb.dbo.backupset
WHERE DB_ID(database_name) > 4 and backup_start_date > GETUTCDATE() - 14
ORDER BY database_name, backup_start_date, type')
ELSE EXEC('
SELECT
	database_name as [DB],
	type,
	user_name,
	cast(1.0*backup_size/1024.0/1024.0/1024.0 as numeric(15,2)) as BkpSizeGB,
	backup_start_date as [Start],
	backup_finish_date as [End],
	(backup_finish_date-backup_start_date) as [Duration]
FROM msdb.dbo.backupset
WHERE DB_ID(database_name) > 4 and backup_start_date > GETUTCDATE() - 14
ORDER BY database_name, backup_start_date, type')
go


IF(IS_SRVROLEMEMBER('sysadmin') = 1)
BEGIN
	CREATE TABLE #SQLErrorLog (LogDate DATETIME, ProcessInfo NVARCHAR(12), LogText NVARCHAR(4000));

	IF (LTRIM(RTRIM(SUBSTRING(@@VERSION, PATINDEX('%Server %', @@VERSION)+7, 5))) <> '2000')
	BEGIN
		INSERT INTO #SQLErrorLog (LogDate, ProcessInfo, LogText) EXEC sp_readerrorlog 0;
	END
	ELSE BEGIN
		SET DATEFORMAT ymd;
		INSERT INTO #SQLErrorLog (LogText, ProcessInfo) EXEC sp_readerrorlog 0;

		UPDATE #SQLErrorLog
		SET LogDate = CAST(SUBSTRING(LogText, 1, 22) as DATETIME),
		ProcessInfo = LTRIM(RTRIM(SUBSTRING(LogText, 23, 9))),
			LogText = LTRIM(RTRIM(SUBSTRING(LogText, 33, 4000)))
		WHERE LogText like '20%'
	END

	DELETE FROM #SQLErrorLog
	WHERE  LogDate is NULL or LogDate < GETUTCDATE() - 14
		or LogText like 'Login succeeded for user %'
		or LogText like 'Log was backed up. Database%'
		or LogText like 'BACKUP DATABASE successfully processed %'
		or LogText like 'Database backed up. Database: %'
		or LogText like 'BACKUP DATABASE WITH DIFFERENTIAL successfully processed %'
		or LogText like 'Database differential changes were backed up. Database: %'
		or LogText like 'I/O was resumed on database %'
		or LogText like 'I/O is frozen on database %'
		or LogText like 'This instance of SQL Server has been using a process ID of %'
		or LogText like 'Error: %, Severity: %, State: %.'
		or LogText like 'Ошибка: %, серьезность: %, состояние: %.'
		or LogText like 'Setting database option %'
		or LogText like 'Using ''xpstar.dll'' version %'
		or LogText like 'Attempting to load library %'
		or LogText like 'Parallel redo is s%'
		or LogText like 'AppDomain % (%'
		or LogText like 'The Service Broker endpoint is in disabled or stopped state.'
		or LogText like 'CHECKDB for database % finished without errors %'
		or LogText like 'Always On: DebugTraceVarArgs AR %'
		or LogText like 'Login failed for user %'
		or LogText like 'SQL Server has encountered _ occurrence(s) of cachestore flush for the %'
		or LogText like 'The activated proc % running on queue %'
--		or LogText like '%'
		;

	DELETE FROM #SQLErrorLog
	WHERE  LogText like '%This is an informational message only. No user action is required.'
		--or LogText like '%: 18456, %: 14, %: 8.'
		--or LogText like 'SQL Server Audit failed to create the audit file %'
		--or LogText like 'Could not allocate space for object %'

	SELECT * FROM #SQLErrorLog ORDER BY LogDate DESC;
	DROP TABLE #SQLErrorLog;
END;
GO
