I have an inline query, in which I have one table1 in server1 and another table2 in server2.
I need to join these two tables, and fetch data.
I can do this like connect to one server, get data and connect to next server...fetch data.
and join them.
But is there any other better way. I have heard about Linked servers. Will that help here ?
Thanks in advance !!!
Yes, set up a linked server on one server to the other. Then you can just do a normal query with a join. It would look something like this:
SELECT t1.Col1
, t2.ColA
FROM server1Table t1
INNER JOIN SERVER2.dbname.dbo.tableName t2 ON t1.TheId = t2.TheId
this assumes you're running the query on Server1. You can also have two linked servers and reference them both using [servername].[dbname].[schema].[table] and then use in SQL as normal.
Alternatively, you can use OPENROWSET (but linked server is easiest if you're able to set that up). OpenRowSets look like this:
SELECT t1.Col1
, t2.ColA
FROM server1Table t1
INNER JOIN OPENROWSET('SQLNCLI', 'Server=Server2;Trusted_Connection=yes;',
'SELECT t2.ColA, t2.TheId FROM dbname.dbo.tableName') AS t2
ON t1.TheId = t2.TheId
and then you can just join on 'a' as if it's a local table. Under the hood it's probably pulling all the data down to your local database, so you should consider adding WHERE to the inner query to restrict rows, and only get the columns you need.
Related
I have a SQL Server 2014 Express with multiple databases. One of them has general tables with information common to the remaining databases (let's call this database UniversalData).
The other databases have information that is pertinent to a specific site (let's call one of these databases Site01Data). The universal data may change and I don't want to replicate it regularly to the other site-specific databases, so I want to include the UniversalData table in some queries, some of which involve CTEs.
What I am trying to accomplish:
WITH CTE1 AS
(
SELECT *
FROM UniversalData.dbo.someTable
),
CTE2 AS
(
SELECT *
FROM Site01Data.dbo.anotherTable
),
CTE3 AS
(
SELECT CTE1.field1, CTE2.field2
FROM CTE1
JOIN CTE2 ON CTE1.idx = CTE2.idx
)
SELECT *
FROM CTE3;
This doesn't generate an error, but I seem to get no data from the CTE1 in my final query (null result set). Intuitively, does this mean it is saving a temp table in the UniversalData database that is not accessible from the Site01Data database?
How can I use a CTE with tables from different databases on the same server?
There are lots of ways to do this..
You could read the tables in one database into a temp table on the second database and then join to it.. or join both of them on the fly.
but first.. refrain from doing select *.. specify the columns
You could go
select t1.column1,t2.column2
from UniversalData.dbo.someTable t1
inner join Site01Data.dbo.anotherTable t2
on t2.ida = t2.idx
and so onn.. it depends on which way you want to specify the join and what sort of join you want to choose..
This assumes that both the data bases are on the same instance.. else you will need linked servers
Specify servername.site1data.dbo.table etc and use linked servers if appropriate across different servernames
enter image description hereI'm facing some weird problem off late, when i try to write a query on joining two different table located in two different databases on same server i get an error.
For EX: if i select Database A separately data is getting displayed and i have to manually select database from available databases dropdown on the top left.
If i select Database B then i should manually change it back to Database B on available database dropdown, due to this issue I'm not able join two tables together. Please help me in getting settings right.
You can use by adding database name before the table name.
Select * from Database1.[dbo].Table1 t1
join Database2.[dbo].Table2 t2 on t1.columnName = t2.columnName
From Your Query lets T1 from DatabaseA and Remaining From DatabaseB
select distinct [Material_Number] from DatabaseA.[dbo].[view_sku_universe_from_sku_data_quality] T1
left join DatabaseB.[dbo].[table_Mat_Deter_SWM] T2 on T1.[Material_Number]=T2.[Sub_Material_1]
left join DatabaseB.[dbo].[table_Mat_Deter_SWM] T3 on T1.[Material_Number]=T3.[Sub_Material_2]
where T2.[Sub_Material_1] is not null or T3.[Sub_Material_2] is not null
1.Create a user which have rights to access both Databases
2.Login to SSMS from that user which you created (user which have rights to access both Databases).
3.Create query like this
SELECT * FROM TestDatabase1.[dbo].Test1 t1
JOIN TestDatabase2.[dbo].Test2 t2 on t1.TestID= t2.TestID
I want to connect with relationships between 2 tables in 2 databases without link server.
NOTE: When you say two different databases i'm guessing you mean on two separate servers otherwise just look at 'The beginner''s answer on this.
You can use FROM OPENDATASOURCE but Microsoft doesn't recommended this as the security details are in the connection string, a Linked Server is the best way to go about it.
The Microsoft Docs is HERE, and a handy post is HERE.
The syntax will look something like this:
SELECT
*
FROM OPENDATASOURCE('Connection String')."DATABASE"."SCHEMA"."TABLE" T1
INNER JOIN Table2 T2 on T1.Id = T2.Id
SELECT *
FROM [FIRST_DB].[dbo].[FIRST_Table] tab1
INNER JOIN [SECOND_DB].[dbo].[SECOND_Table] tab2
ON tab1.[COMMON COLUMN]= tab2.[COMMON COLUMN]
I'm trying to create a little SQL script (in SQL Server Management Studio) to get a list of all tables in two different databases. The goal is to find out which tables exist in both databases and which ones only exist in one of them.
I have found various scripts on SO to list all the tables of one database, but so far I wasn't able to get a list of tables of multiple databases.
So: is there a way to query SQL Server for all tables in a specific database, e.g. SELECT * FROM ... WHERE databaseName='first_db' so that I can join this with the result for another database?
SELECT * FROM database1.INFORMATION_SCHEMA.TABLES
UNION ALL
SELECT * FROM database2.INFORMATION_SCHEMA.TABLES
UPDATE
In order to compare the two lists, you can use FULL OUTER JOIN, which will show you the tables that are present in both databases as well as those that are only present in one of them:
SELECT *
FROM database1.INFORMATION_SCHEMA.TABLES db1
FULL JOIN database2.INFORMATION_SCHEMA.TABLES db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY COALESCE(db1.TABLE_NAME, db2.TABLE_NAME)
You can also add WHERE db1.TABLE_NAME IS NULL OR db2.TABLE_NAME IS NULL to see only the differences between the databases.
As far as I know, you can only query tables for the active database. But you could store them in a temporary table, and join the result:
use db1
insert #TableList select (...) from sys.tables
use db2
insert #TableList2 select (...) from sys.tables
select * from #TableList tl1 join Tablelist2 tl2 on ...
Just for completeness, this is the query I finally used (based on Andriy M's answer):
SELECT * FROM DB1.INFORMATION_SCHEMA.Tables db1
LEFT OUTER JOIN DB2.INFORMATION_SCHEMA.Tables db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY db1.TABLE_NAME
To find out which tables exist in db2, but not in db1, replace the LEFT OUTER JOIN with a RIGHT OUTER JOIN.
I have a SQL Server 2005 database that is linked to an Oracle database. What I want to do is run a query to pull some ID numbers out of it, then find out which ones are in Oracle.
So I want to take the results of this query:
SELECT pidm
FROM sql_server_table
And do something like this to query the Oracle database (assuming that the results of the previous query are stored in #pidms):
OPENQUERY(oracledb,
'
SELECT pidm
FROM table
WHERE pidm IN (' +
#pidms + ')')
GO
But I'm having trouble thinking of a good way to do this. I suppose that I could do an inner join of queries similar to these two. Unfortunately, there are a lot of records to pull within a limited timeframe so I don't think that will be a very performant option to choose.
Any suggestions? I'd ideally like to do this with as little Dynamic SQL as possible.
Ahhhh, pidms. Brings back bad memories! :)
You could do the join, but you would do it like this:
select sql.pidm,sql.field2 from sqltable as sql
inner join
(select pidm,field2 from oracledb..schema.table) as orcl
on
sql.pidm = orcl.pidm
I'm not sure if you could write a PL/SQL procedure that would take a table variable from sql...but maybe.....no, I doubt it.
Store openquery results in a temp table, then do an inner join between the SQL table and the temp table.
I don't think you can do a join since OPENQUERY requires a pure string (as you wrote above).
BG: Actually JOIN IN SQLServer to Oracle by OpenQuery works, avoiding #tmp table and allowing JOIN to SQL without Param* - ex.
[SQL SP] LEFT JOIN OPENQUERY(ORADB,
'SELECT COUNT(distinct O.ORD_NUM) LCNT,
O.ORD_MAIN_NUM
FROM CUSTOMER.CUST_FILE C
JOIN CUSTOMER.ORDER_NEW O
ON C.ID = O.ORD_ID
WHERE C.CUS_ID NOT IN (''2'',''3'')
GROUP BY O.ORD_MAIN_MACNUM') LC
ON T.ID = LC.ORD_MAIN_ID*
Cheers, Bill Gibbs