Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using "USE" keyword Vs. full table name in T-SQL

When I want to select from table Y in database X I can use

select * from [X].[dbo].[Y]

or

USE X
select * from [Y]

Is there any reason to prefer one over the other?

like image 997
choppy Avatar asked Feb 18 '14 05:02

choppy


People also ask

What does the use keyword do in SQL?

The SQL USE statement is used to select any existing database in the SQL schema.

Which SQL keyword is used to add one or more rows to a table?

The INSERT Keyword is used to insert the rows of data to a table.

Why should you not use select * in SQL?

When you use select * you're make it impossible to profile, therefore you're not writing clear & straightforward code and you are going against the spirit of the quote. select * is an anti-pattern. So selecting columns is not a premature optimization.

Can we use SQL keywords as column name?

In SQL, certain words are reserved. These are called Keywords or Reserved Words. These words cannot be used as identifiers i.e. as column names in SQL.


1 Answers

dbo
Using dbo as the owner of all the database objects can simplify managing the objects. You will always have a dbo user in the database. Users in the database will be able to access any object owned by dbo without specifying the owner as long as the user has appropriate permission.

USE X
When a SQL Server login connects to SQL Server, the login is automatically connected to its default database and acquires the security context of a database user. If no database user has been created for the SQL Server login, the login connects as guest. If the database user does not have CONNECT permission on the database, the USE statement will fail. If no default database has been assigned to the login, its default database will be set to master.

Understanding the Difference between Owners and Schemas in SQL Server

USE (Transact-SQL)

like image 118
Nagaraj S Avatar answered Sep 24 '22 03:09

Nagaraj S