I have a program need to create and drop a database multiple time, but sometime when dropping a database I get a exception here:
ALTER DATABASE failed because a lock could not be placed on database ...
And the command is like this:
USE master;
IF EXISTS(select * from sys.databases where name='{0}')
BEGIN
ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [{0}]
END
Why it happens?
What is the better way to do this (drop database)?