You should make a couple ofsmall changes in your script
First change the type of @SQL to nvarchar and replaceReplace EXEC @SQL with EXEC sp_executesql @SQL or EXEC (@SQL) This works fine
DECLARE @DBName VARCHAR (64)
DECLARE @SQL NVARCHARVARCHAR (256)
DECLARE DROPDB CURSOR FOR
SELECT name FROM sys.databases
WHERE create_date <= '20190726'
AND name NOT IN ('master','model','msdb','tempdb','distribution','ReportServer','ReportServerTempDB')
OPEN DROPDB
FETCH next FROM DROPDB INTO @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'DROP DATABASE ' + @DBName
PRINT @SQL
EXEC sp_executesql @SQL --OR EXEC (@SQL)
FETCH next FROM DROPDB INTO @DBName
END
CLOSE DROPDB
DEALLOCATE DROPDB