Note:  Best viewed in Internet Explorer 6 and above

Implementation: How to use a variable in an ALTER database command.

Scenario: Lets say,you want to use a variable in an ALTER DATABASE for putting all or selected DBs offline/online/attach/detach purpose.

Infrastructure:The below query/procedure has been run/tested successfully on sql server 2005,2008,2008 R2 and 2012.

Kindly find the below link for the same.