I have an sql linked server whose data I can access using openquery, but I have no idea of how to see the tables of that database, hence i cant do much.
I only have a query
SELECT * FROM OPENQUERY(MYSERVER, 'SELECT * FROM SERVXML.DATA AS A WHERE A.DATAID = 2355')
Which returns some stuff.
But otherwise how can I see all the info in MYSERVER? I mean all the tables more specifically.
I have tried using this
EXEC sp_tables_ex 'MYSERVER';
With no result.
I'm a little confused. If you have a linked server, then you can just access the tables using a four-part naming convention: ...table.
This is explained in the documentation.
You can see the linked servers using metadata tables and views. For instance:
select *
from <server>.<database>.INFORMATION_SCHEMA.TABLES
Related
I have a linked server setup and need to pull data from the linked server into my main server. It feels painful to do using the whole server name. I'm trying to see if I can establish the name upfront then use in the rest of my query below.
Such as this:
Linkedserver.Database.dbo.Table as Link1
then below in my queries be able to have this work:
select * from Link1
or
select * from main-server left join Link1
One method is a view, which does pretty much exactly what you want:
create view v_linked
select *
from Linkedserver.Database.dbo.Table;
You can then reference this as:
select *
from v_linked;
Synonyms are an alternative mechanism:
create synonym linked for Linkedserver.Database.dbo.Table;
There are few practical differences between synonyms and views. However, there are some, such as the effect on existing objects when one is deleted and the DDL triggers needed to track changes.
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.
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).
How to create a view from table A from server 1 and table B from server 2, based on a same column named as col? They use different credentials. The servers are SQL Server 2005
Without knowing details, I'm not sure this is the best idea - but this would work for you. It requires four part naming and linked servers.
Here is the syntax for the view.
Create VIEW [dbo].[vw_CrossServer]
AS
SELECT *
FROM Server1.DatabaseName.Schema.tableA TA
INNER JOIN Server2.DatabaseName.Schema.tableB TB ON TA.col = TB.col
GO
For this to work, you'll need to setup a linked server between the databases. Linked Server
Link also contains examples and other resources.
I have a few SQL Server databases (all in one server), containing their own set of users. Now I'm trying to design a small application that would query those users and then display them in a report (TBD). I've looked over online how to do this, however I didn't find any. Is it possible in SQL Server to retrieve all the users of a database? If so, how?
On SQL Server 2005 and up:
connect to that specific database you're interested in
USE Databasename
execute this query
SELECT * FROM sys.database_principals
That gives you a slew of information on all users defined in the database
See the MSDN documentation for a detailed explanation of all rows returned from that view.