automate sqlcmd to detatch database

I am looking to Automate a SQL script file for detaching a database using sqlcmd console. can anyone help me out with this?

Who is Participating?
BrandonGalderisiConnect With a Mentor Commented:
sqlcmd -Syourservername -E -dmaster -Q"sp_detach_db 'YOURDATABASENAME'"

-E is trusted connection, either use that or -U and -P for password
-dmaster : you want to ensure that you don't accidentally end up in the DB to be detached due to default database settings.

There are other things to consider:
What if users are in the DB currently?
What if users have that DB set as their default DB?  Removing the Db could prevent them from logging in.
IGCNTechAuthor Commented:
this was exactly what I was looking for.
thanks for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.