This will help I guess
DROP TABLE ##TEMPDBID
GO
CREATE TABLE ##TEMPDBID
(
Name sysname,
Db_id_no int,
sidid varchar(5000),
mode smallint,
status int,
status2 bigint,
crdate datetime,
reserved varchar(1000),
categry smallint,
cmptlevel int,
Filename varchar(5000),
Version int,
)
INSERT INTO ##TEMPDBID SELECT * FROM SYS.SYSDATABASES
DECLARE
@query varchar(max),
@Db_id_no int,
@Db_name varchar(1000)
DECLARE CUR_DBID CURSOR FOR
SELECT Db_id_no FROM ##TEMPDBID
OPEN CUR_DBID
FETCH NEXT FROM CUR_DBID INTO @Db_id_no
WHILE @@FETCH_STATUS=0
BEGIN
IF ((@Db_id_no > 4) ) ---- number after sysdatabases
/* The System Databases can be eliminated from the drop Task. Depending upon the requirement,
the @Db_id_no can be given as >4~5 excluding the Sys DBs.*/
BEGIN
set @Db_name = (select DB_NAME(@Db_id_no))
SELECT @query ='drop DATABASE ' + @Db_name
--set @query = 'Drop database @Db_name;'
exec (@query)
END
FETCH NEXT FROM CUR_DBID INTO @Db_id_no
END
CLOSE CUR_DBID
DEALLOCATE CUR_DBID