It's fetching the dbname peroperly from the cursor and printing the same, but not able to execute the drop command . can someone please help me on this.
---------------------------------------------
declare db_loop cursor for
select name from sysdatabases where dbid in(7,10,11,12)
go
declare @dbname char(35)
open db_loop
fetch db_loop into @dbname
while (@@sqlstatus = 0)
begin
select 'dbname is ' + @dbname
if exists (select 1 from sysprocesses where dbid = db_id(@dbname))
begin
select " Killing users in the database " + rtrim(@dbname)
declare @sql varchar(50)
while exists (select 1 from sysprocesses where dbid = db_id(@dbname))
begin
select @sql = 'kill ' + convert(varchar,min(spid)) from sysprocesses where dbid = db_id(@dbname)
print '%1!', @sql
exec (@sql)
waitfor delay '00:00:02'
end
drop database @dbname
end
print 'Droping the database ...'
fetch db_loop into @dbname
end
close db_loop
deallocate cursor db_loop
go