I need to drop a perticular database in SQL Server 2005 using T-SQL. I know droping a databse can not be done in a database trnsaction. So what if while deleting a real huge database some unexpected exception occurs E.g. Time out exception. Will it leave my database in unstable state ? If yes, how do i avoid such situation ?
Share
depending on how you’re running the t-sql, you can change the timeout before running the commands. SSMS defaults to no timeout, but you can change SqlCommand’s CommandTimeout to either 0 (no timeout) or something large. In my personal experience, failed runs of “drop database” haven’t prevented later runs of it from succeeding, so while you may get the database into a bad state, I don’t believe it’d be one where it’s still there but not drop-able (but that’s just my experience, admittedly)
Depending on whether you want to delete the data files, I’d recommend setting the database into either single_user mode (if you want to delete the files) or offline (don’t delete the files) with an immediate rollback to help prevent other connections from blocking the drop or causing it to fail.
So:
Make sure to check some of the various behaviors of drop database (like the above) @ http://msdn.microsoft.com/en-us/library/ms178613.aspx