tsql: select view from different database - sql-server

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]

Related

Multi database query

The code example given in Query across multiple databases on same server will work for what I want to do. My only question is whether it's possible to include the name of the database in the SELECT statement for the records that are retrieved. In other words, I'd like to know if a record is from DB1 or DB2. Is this possible?
This answer is based on the code from the accepted answer in the linked question (link).
Since you know while creating the View from which database you are pulling the data, you can add this information as a static column:
CREATE VIEW vCombinedRecords AS
SELECT 'DB1' as Database, * FROM DB1.dbo.MyTable
UNION ALL
SELECT 'DB2', * FROM DB2.dbo.MyTable
This will tag every record from DB1 with the value "DB1" in the Database column
Sure, try:
CREATE VIEW vCombinedRecords AS
SELECT 'DB1' as Database_Name,* FROM DB1.dbo.MyTable
UNION ALL
SELECT 'DB2',* FROM DB2.dbo.MyTable

in SQL Server 2014 why SELECT * FROM System_User doesn't work but SELECT * FROM [ProjectManage].[dbo].[System_User] work

I am using SQL Server 2014 management studio, I found a strange situation.
in a SQL query dialog, I key in the below SQL:
SELECT * FROM System_User
I can see the table name in resource management dialog is: dbo.System_User
Then when I try to execute it, it said there is some grammar error.
If I change the SQL as:
SELECT * FROM [ProjectManage].[dbo].[System_User]
it works well.
But acutally in same SQL query dialog, I have another SQL:
SELECT * FROM FlowTemplateTransition
and its table name is dbo.FlowTemplateTransition in resource management dialog.
My question is:
what is the difference between dbo.FlowTemplateTransition and FlowTemplateTransition?
when do I need to add dbo and when it is unnecessary?
do I need to using square brackets always?
Thanks
dbo is the default schema in SQL Server. You can create your own schemas to allow you to better manage your object namespace.
In SQL Server Management Studio, you can create your own schema by browsing to Databases - Your Database - Security - Schemas. Or you can execute below query to create a schema.
CREATE SCHEMA [SchemaName] AUTHORIZATION [dbo]
You can use them to logically group your tables, for example by creating a schama for "vendors" information and another for "customers" data. Your tables would then display as:
vendors.BankAccounts
vendors.Transactions
customers.Address
Instead of using the default schema of dbo.
No need to use the square brackets[] always. But if your database or table name contains something like a dot(.) then you must use [].
As you have 3 questions. I have list it down separately.
Why SELECT * FROM System_User not working?
what is the difference between dbo.FlowTemplateTransition and FlowTemplateTransition
when do I need to add dbo and when it is unnecessary? do I need to using square brackets always?
Answer 1 : System_User is sql server built in function. Your table name conflict with this function that is why it gives an error.
Answer 2 : dbo is default database owner. If you don't write owner of table than Sql Server automatically consider dbo as an owner for that table.
Answer 3 : Generally dbo is not necessary to add.
The brackets are required if you use keywords or special chars in the table name.

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 to make a select query for sql and access databases?

Using SQL server 2000 and Access 2003
Access Database Name - History.mdb
Access Table Name - Events
SQL Database Name - Star.mdf
SQL Table Name - Person
I want to take the field from person table, and include in Events table by using inner join
Tried Query
Select * from Events inner join person where events.id = person.id
So How to make a query for access and sql databases.
I want to make a Select query in access only. Not an sql Database.
Need Query Help?
While you can (possible, should -- why?) use a linked table, there are as ever more than one way to skin a cat. Here's another approach: put the connection details into the query test e.g. something like
SELECT *
FROM [ODBC;Driver={SQL Server};SERVER=MyServer;DATABASE=Star;UID=MyUsername;Pwd=MyPassword;].Person AS P1
INNER JOIN
[MS Access;DATABASE=C:\History;].[Events] AS E1
ON S1.seq = S2.seq
WHERE E1.id = P1.id;
You can set up a linked table in Access to your SQL Server, and the instructions on how to do so vary slightly in Access versions. Look in the help file for "Linked Table", or go here if you have Access 2007.
Once you have a linked table set up, you'll be able to access the SQL Server table in your query. Note that optimizing a linked table join takes some work.
You can create a linked table in Access, that points to the table in SQL. The rest you can achieve in the query designer.
You should add the msaccess db as a remote server.
then you can do that join

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