Tuesday, May 11, 2021

To kill a stubborn

In MS-SQL, on an attempt to drop a DB getting an 'in use' error, even after it switched to the singly user mode by executing

USE master
GO
ALTER DATABASE [DB]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [DB]
SET READ_ONLY;
GO


As suggested in an answer here, there is a way to find who is actually using the DB:

 

  select  d.name , convert (smallint, req_spid) As spid
      from master.dbo.syslockinfo l, 
           master.dbo.spt_values v,
           master.dbo.spt_values x, 
           master.dbo.spt_values u, 
           master.dbo.sysdatabases d
      where   l.rsc_type = v.number 
      and v.type = 'LR' 
      and l.req_status = x.number 
      and x.type = 'LS' 
      and l.req_mode + 1 = u.number
      and u.type = 'L' 
      and l.rsc_dbid = d.dbid 
      and rsc_dbid = (select top 1 dbid from 
                      master..sysdatabases 
                      where name like 'DB')


Tuesday, April 20, 2021

Ghost Files

In  Windows a space is not allowed in the end of a file name. When such a file is somehow created, it cannot be removed by regular means. As a workaround, try to specify the file location with the following notation:

"\\?\D:\path\filename "