If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > Unable to execute drop database from the script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-01-11, 15:05
regtha regtha is offline
Registered User
 
Join Date: Jan 2010
Posts: 72
Unable to execute drop database from the script

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
Reply With Quote
  #2 (permalink)  
Old 02-02-11, 03:47
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
Are you getting an error? If so, which error?
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #3 (permalink)  
Old 02-02-11, 11:14
regtha regtha is offline
Registered User
 
Join Date: Jan 2010
Posts: 72
Here is the output :
Here it is fetching the row from cursor correctly, i.e database name is test.
but when it comes to drop, it's not doing.
---------------------------
dbname is test
Droping the database ...
Msg 2520, Level 16, State 5:
Server 'INDIA', Line 24:
Database named test not found; check sysdatabases
-----------------------------------------

Thank you.
Reply With Quote
  #4 (permalink)  
Old 02-03-11, 03:48
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
try
exec ('drop database '+@dbname)
Reply With Quote
  #5 (permalink)  
Old 02-03-11, 11:46
regtha regtha is offline
Registered User
 
Join Date: Jan 2010
Posts: 72
Thank you. It's working now.

Though I posted the script which has

"drop database @db_name"

I also tried "exec drop database @db_name" but this also didn't work.

Can you also sugget me one thing like how to know that this command has to be executed like " exec ('drop database' + @db_name)

Once again thank you.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On