DECLARE
@i integer = 1,
@e integer =
(
SELECT
MAX(d.database_id)
FROM sys.databases AS d
),
@s nvarchar(MAX) = N'';
SET @s = N'
SELECT TOP (1)
@i = d.database_id
FROM sys.databases AS d
WHERE d.database_id >= @i
ORDER BY
d.database_id;';
WHILE
@i <= @e
BEGIN
EXEC sys.sp_executesql
@s,
N'@i integer OUTPUT',
@i OUTPUT;
RAISERROR(N'%i of %i', 0, 1, @i, @e) WITH NOWAIT;
SELECT
@i += 1
END;