sp_detach

  • I need to do sp_detach but I can`t because says me that the database is currently in use.

    I closed all the applications that use the database.

    How can I know what process is still using the database?

    Thanks

    Viky.

  • Are you connected to the database that you're trying to detach? If so change your context to master.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Before I try to detach I connected to the master.

  • sp_who

    sp_who2

    kill

    alter database set single_user

    use master

    exec dbo.sp_detach

  • I have found this quite effective for kicking out users.

    ALTER DATABASE [DATABASE NAME]

    SET OFFLINE WITH ROLLBACK IMMEDIATE

    Go

    ALTER DATABASE [DATABASE NAME]

    SET ONLINE WITH ROLLBACK IMMEDIATE

    Chris

    Learning something new on every visit to SSC. Hoping to pass it on to someone else.

  • You can also use this,

    select DB_ID ('Your DatabaseName')

    select spid from sys.sysprocesses where dbid = YourDatabaseId

    kill

  • alter database set restricted_user

    I always wished for:

    alter database set single_restricted_user

  • USE yourDb

    ALTER yourDb

    SET SINGLE_USER WITH ROLLBACK IMMEDIATE

    USE master

    EXEC sp_detach 'yourDb'

    Scott Pletcher, SQL Server MVP 2008-2010

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply