Can I catch error? Server: Msg 3701, Level 16,Cannot detach the database ''marktest'' because it is currently in use

  • Part of my script runs:

    sp_detach_db @dbname, 'true'

    When the db is in use the message 'Cannot detach the database 'marktest' because it is currently in use' is displayed in sql server.

    How can I catch this error?  I already have the ability to log other errors to a text file but I dont know how to capture this type of message!?

     

    Can anyone help?

  • you might want to run sp_who2  and store the output into the #table. Then query this table for that database usage and if necessary issue kill users...Then use your statement

  • Agreed you should check for users first and then deal with them. You may be able to catch the error by evaluating @@error, but some things can not be caught

    Terry

  •  

    hi,

    You  can detach your database only when if it is not used by someone

    or by some application.

     

    Regards

    Amit Gupta.

Viewing 4 posts - 1 through 3 (of 3 total)

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