Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 310
Michal Bernhard Avatar asked Dec 05 '22 03:12

Michal Bernhard


1 Answers

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

like image 152
Quassnoi Avatar answered Jun 06 '23 11:06

Quassnoi