statement "USE @dbname" doesn't work, why? How to do that?

sql-server-2005, t-sql

Solution

You cannot provide the name of the database for `USE` statement in a variable.

Problem

I've got this t-sql snippet: ``` DECLARE @db_name varchar(255); SET @db_name = 'MY_DATABASE'; -- assuming there is database called 'my_database' USE @db_name -- this line ends with error "Incorrect syntax near '@db'." ``` But USE with variable (third line of snippet) doesn't work. Why it doesn't work?

Original source