Few lines about SQL Use statement
We see this statement in Management Studio now and then and it is used quite extensively. For Example,
USE MyDatabaseName GO
MyDatabaseName : MyDatabaseName is the name of the database that we want to connect with.
How it works?
When we login to SQL Server, we automatically gets connected to the default database. It runs under the security context of current database user. In condition of no default database user for the SQL Server login, the login connects as guest.
We sometimes receive an error that ‘…login does not have sufficient permissions..’. Here,If the current user does not have CONNECT permission on the database, the USE statement will fail.
So, It is required to have CONNECT permission for the target database.
- If the target database is not defined, then it is by default set to master.
- USE is executed at both compile and execution time and takes effect immediately.