A condition in a where clause should be :
insert into #spacetemp (DBname,DBcreationDate,DBStatusid,DBStatus)
select [name],crdate, status,case
when status=1 then 'autoclose'
(....)
end
from master..sysdatabases where (status & 512 = 0) and dbid >case when @sysdb=1 then 0 else 4 end
To prevent crash on a offline databases.
Regards,
Yenidai