I have four SQL Servers that are named in the following way:
dbs
dbs2
dbs3
dbs4
I have a table that is on dbs3 called table1 in database1. This table does not exist on the other servers. However when I run the query:
select *
from dbs.database1.dbo.table1 (or any of the database servers)
it returns the results as if I queried the existing table on dbs3. It is like the DBMS is ignoring the 4 part nameing in the query and returning the results from table on dbs3 no matter which server I try to designate in the 4 part naming convention. Any ideas what could be going on here. The servers appear in the linked servers list.
If you can make changes without breaking stuff (or if it's already broken enough in your opinion), I recommend recreating your linked servers. If your linked server is another SQL Server, you can do
exec sp_dropserver 'dbs';
exec sp_addlinkedserver 'dbs';
This creates a linked server definition with the default configuration, which is appropriate for most applications (and can still be tweaked afterwards).
Related
I have 3 databases hosted across 3 different servers, all of which have different data and structures. I need to perform a query that will draw data from various tables across all three of them.
I've registered the three servers into a server group, and I've confirmed that all of the connections are working properly.
Here's an example of the problem I'm facing. For the first part of this query, I need to retrieve a list of records from the 'Applications' table in DB1 so I write:
SELECT * FROM [DB1].[dbo].[Application]
I know that this query works partially because it starts returning results from the correct table. The problem is that I haven't specified the server that DB1 is on, so once the query has finished querying DB1.dbo.Application, it looks for the same database and table on the next server. The database and table don't exist on the other servers so the query fails.
So how do I specify the server that I want the query to run on? I've tried [server_name].[DB1].[dbo].[Application], but it still runs the query across all of them.
Server groups are for maintenance purposes, what you need to use is a linked server
On one of the servers, say SERVER1, you will need to set up two linked servers - one to SERVER2 and one to SERVER3
From SERVER1 you will then be able to query the other servers using the four part name in a normal query window:
SELECT * FROM DatabaseName.dbo.Table1;
SELECT * FROM SERVER2.DatabaseName.dbo.Table2;
SELECT * FROM SERVER3.DatabaseName.dbo.Table3;
You can also use the tables from the remote server in JOINs etc as though they were on the local server and the remote servers don't even need to run SQL Server - they can be Oracle, MYSQL etc.
Be aware though, remote servers are slow and you may struggle with large datasets
This question already has answers here:
Querying data by joining two tables in two database on different servers
(11 answers)
Closed 4 years ago.
How can I fetch the data from two different databases that are on different servers in SQL Server?
You will need to create a linked server to the server where you will execute the Query.
You can use sp_addlinkedserver to create one. You may reference here.
Sample would be for the same SQL Server
EXEC sp_addlinkedserver
N'SEATTLESales',
N'SQL Server';
GO
Once you have created the linked server. You can now reference it in your query by doing something like this:
SELECT * FROM [NameOfLinkedServer].[DatabaseInLinkedServer].[dbo].[TableInLinkedServer]
Or you can check Dave Brown's comment to your question: Here.
You can indeed do this, but I recommend against it. You can create a linked server on DB-SERVER-1, connecting it to DB-SERVER-2.
(A linked server definition is setup by the DBA. It contains the login credentials to the remote server. If you want to provide the login credentials yourself each time, that would be an openquery or openrowset command.)
You then could write a query using four part naming convention as follows, assuming you are running this query on DB-SERVER-1:
select *
from DB1.dbo.YourTable a
join [DB-SERVER-2].DB2.dbo.OtherTable b on b.ID = a.ID
Cross server joins are notorious for having performance problems. I would only do it for administrative purposes, and definitely not against large result sets.
A view that references a remote server
4part name ([ServerName], [DatabaseName], [Owner], [Object Name]
OpenQuery
Which is better performance?
Why is performance good?
AFAIK, it depends a lot on your remote server type.
With recent SQL version (2016) on both server (local and remote), I didn't noticed any difference.
If your remote server is anything else (postgres, mysql...) your really should use OpenQuery as it executes the query on the remote server, getting only the correct resultset. If you use the 4 part name, SQL server will order and filter on local.
For example, take a 4 million record table and execute a query like :
SELECT * FROM reoteserver.database.schema.table where id = 4
With openquery, sql server will get only the record with id 4. Without, it will get all the table, and then filter it to get the id 4.
Late to the party here, but the difference essentially is that 4 part queries are executed locally, thus cannot utilise indexes or keys since the local server doesn't know about them. Instead it essentially retrieves the entire object, then applies the filter. On a small table, you would be unlikely to notice a difference, but on a table with millions of rows, you'd notice a difference. Openquery essentially tells the remote server to execute the query on it's behalf then pass the result back.
General rule I would say is;
NEVER join on to a table using 4 part. Only join using Openquery and I would even avoid that where possible, but that's more of a personal preference.
However, 4 part SP execution i.e. EXEC ServerName.DBName.SchemaName.ObjectName is essentially the same since that also tells the remote server to execute the query on its behalf.
Is that possible? I'm using Aquadesk and I can't get it to work. The tables have a matching unique identifier and wondering if I can match them up in some way.
What you need - as I think - are "Federated Servers" (Databases) (you can look this up)
The basic idea behind that is, the you can create (catalog) a table in you local Database that is already residing on an other Database (or Server, or even an other DB System, but that depends in you SQL system and version) -> that is defintely a question for your DBAS
You get a table like 'MYSQL'.'PERSONS' that resides remotely (eg. 'BASE','PERSDATA'), so you can use them in a
`SELECT *
from 'LOCALNAME'.'USERS usr
JOIN 'MYSQL'.'PERSONS' pers
on usr.user_id=pers.id`
So jou can select and join over different Databases (and Servers)
I only used that whith IBM/UDB but it works realy fine, and has a fair performance (altough heavily depending on your statement)
I have a linked server where I can clearly see all the databases and tables, so I know the server is properly linked. However, when I try to execute a query, it says invalid object name, at the linked server's table.
The linked server is aliased as TCS, therefore, my query takes that table as
FROM [TCS].dbo.table as b
I have also tried including the database name also as FROM [TCS\db1].dbo.table.
What am I missing here?
Try including the DB name like so:
FROM [TCS].db1.dbo.table as b
I don't think you can specify the DB using a slash.
I would also check to make sure your security settings for the linked server are allowing your account to connect. This article touches on how to do that.
either:
the user (used for the link) doesn't have access to the table; Grant access;
the default DB on the server doesn't have the table. You have to change it to the relevant one or included in the db in the name: [TCS].DATABASE.dbo.table as b;