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

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?

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)

I'd tend to use [server].[database].[schema].[table] in instances where a script may query mutliple tables from multiple databases.
The USE [database] would typically be used in scenarios where all statements were to apply to the same database and you needed to make sure they were applied to the correct database. Have you ever connected to a server and run a script only to find you ran it on the master database?

USE X will change the context to X and all the following statements will execute under the context X.
But X.dbo.Y will access the object Y without changing the current context.
Eg: Let us consider there is two databases DB1 and DB2. DB1 contains table T1 & T2 and DB2 contains tables U1 & U2.
Now,
USE DB1 -- here context set to DB1
select * from T1 -- works fine
select * from U1 -- gives error, because U1 is not in current context
select * from DB2.dbo.U1 -- works fine, because it access the context DB2 from current content context DB1
select * from T2 -- works fine
USE DB2 -- here context changed to DB2
select * from U2 -- works fine
select * from T1 -- gives error, because T1 is not in current context
select * from DB1.dbo.T1 -- works fine, because it access the context DB1 from current content context DB2

by using first Query you can perform that selection from other databases.In the same window you can have the selection for other data beses also.
But By using second selection, From the same window you can have selection for that(USE X) databse only.

Sometimes you want the schema and database to be dictated by the login, and in this case you should simply use the object name. That's one reason to not fully qualify them.

Related

tsql: select view from different database

Is it possible to select view defined in different database in MS SQL Server?
All my searching results point to defining view to use data from different database, but haven't found if it possible to select view from another database yet.
suppose you want to do a select on database DBOther than it would be :
select * from DBOther..TableName
Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name
select * from DBOther.dbo.ViewName
Make sure the Database is in the Linked Server if they are not on the same server.
Then you can access the table or view on that database via:
SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]
If on same server:
SELECT * FROM [DB].[dbo].[Table]

prevent some user from seeing db2 tables structure

How can I restrict some users in DB2, not to see the table structure. I set the user privilege and restrict user from table access. so that user can not select data or change table but still can see the table structure or describe it.
This problem refers to row access in tables which is added in db2 version 10.
I had this problem too.
you can use this version - if applicable- and restrict user access from specific table structures.
You need to remove the select grant on catalog tables. For example, the following query should return 0 rows when executing with q restricted user.
db2 "select tabschema, tabname from syscat.tables"
All tables and views in the following schemas should not have select on public, nor in any group the restrictive user is in.
sysibm
syscat
db2 revoke select on SYSIBM.SYSTABLES from username

Query more than one database?

What's the way to query more than one database using SQL*Plus?
In MySQL it's possible to do something like this :
create table WK_LINK_JOINT_IDEOREQ AS
select k.constraint_name cn, k.table_name tl, l.column_name lc
, k.referenced_table_name tg, k.column_name cg, l.referenced_table_name td
, l.referenced_column_name cd
from information_schema.KEY_COLUMN_USAGE k
It's just an example, it's not complete : but as you can see, we are working on two databases, INFORMATION_SCHEMA and another one.
I want to do somthing like this using SQL*Plus, but the problem is that when we connect using SQL*Plus, we specify the database (SID) which means that the others are not accessible.
Is there a way to do it?
Oracle has a different interpretation of DATABASE from MySQL. In Oracle we have multiple users or schemas in the same database.
So, if what you really want is to access objects from a different schema all that has to happen is for that schema to grant you privileges. You can then reference the tables (or whatever) in your SQL.
User JOE grants you select on his table
SQL> conn JOE/SOAP
SQL> grant select on my_table to ABC;
You can then run queries on it:
SQL> conn ABC/DEF
SQL> select * from joe.my_table;
In your example you used INFORMATION_SCHEMA. The Oracle equivalent of this is the data dictionary, an enormous library of views. Find out more.
Public access is granted by default on most of them. So you can select from USER_TABLES, USER_CONSTRAINTS and USER_CONS_COLUMNS to re-create that query (assuming I have understood it correctly).

How to SELECT in Oracle using a DBLINK located in a different schema?

We have an Oracle DBMS (11g) and the following configuration:
A DB user "MYUSER"
Two schemas "MYUSER" and "SCHEMA_B"
User "MYUSER" can access "SCHEMA_B" and has READ permissions on its tables
A public DB link "DB_LINK" located in "SCHEMA_B"
The DB_LINK is working when using the DB user "SCHEMA_B" directly
Question: When logged on as "MYUSER", what is the correct syntax to access tables using the DB link of "SCHEMA_B"? Is it possible to do so at all?
I already tried several constellations, which all did not work:
select * from dual#"DB_LINK"
select * from dual#"SCHEMA_B"."DB_LINK"
select * from dual#SCHEMA_B."DB_LINK"
select * from dual#SCHEMA_B.DB_LINK
select * from SCHEMA_B.dual#DB_LINK
select * from "SCHEMA_B".dual#DB_LINK
The error message I receive is:
ORA-02019. 00000 - "connection description for remote database not found"
Thanks for any suggestion!
I don't think it is possible to share a database link between more than one user but not all. They are either private (for one user only) or public (for all users).
A good way around this is to create a view in SCHEMA_B that exposes the table you want to access through the database link. This will also give you good control over who is allowed to select from the database link, as you can control the access to the view.
Do like this:
create database link db_link... as before;
create view mytable_view as select * from mytable#db_link;
grant select on mytable_view to myuser;
I had the same problem
I used the solution offered above -
I dropped the SYNONYM, created a VIEW with the same name as the synonym.
it had a select using the dblink ,
and gave GRANT SELECT to the other schema
It worked great.

How do you show a list of tables in another database?

I can use:
select * from sys.tables
in mssql to show a list of all tables in the current database. Is there anyways I can use similar syntax to show list of tables in another database?
Say I am using A with:
use A
statement, can I show tables in database B?
This does it for me (MS SQL 2005 and newer):
select * from your_database_name.sys.tables
Keep in mind that you (or whatever authentication context you're using) will still need read permission on that database.
To use your example:
use a;
go
select * from sys.tables; -- selects table info from a
select * from b.sys.tables; -- selects table info from b
Another possibility is to use:
select * from your_database_name.information_schema.tables

Resources