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.
Related
I just have two servers [sm33ooo.net] and [sm22ooo.net] , i would like to make query between two different tables (tbl_doc) in server [sm33ooo.net] and (tbl_archive) in [sm22ooo.net] using oracle sql developer ,any idea ?
you should be able to create a DBLINK on one of the databases which will allow you to query a table on that database from the other database. I'm not sure of your setup from your post so you might need to look into the difference between private and public dblinks to decide what is relevant to you.
For example if you add your link to server sm33ooo.net called dblink then on server sm22ooo.net you will be able to write select * from tbl_doc#dblink;
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
A colleague of mine has given me a task to ensure all table name and pages in a stored procedure contains the relevant schemas?
For example if I have a table in a database that is dbo.table1, then in a query if I have:
Select * from table1
He wants me to change it to:
Select * from dbo.table1
This is same for pages that start with Support.
What is the significance of adding in a scheme like dbo. At the start when manually writing SQL? Is it suppose to be better for performance as it seems to know where the tables even if I don't include .dbo at the start?
I'm using SQL server 2012 and its management studio.
Thank you
You may not notice the performance if you write the schema in all of your queries. If you don't specify it, the engine will look through all of your schemas in your databases ( I'm not pretty sure if the engine will check the sys schemas too ) until it finds the table you're getting the data; It's recommended as a best practice to write it because you are telling to the engine what schema need to search the table.
I hope this answer was helpful.
dbo is the schema which is used as part naming convention for tables in a mssql database
look at this post SQL Server edits the table name with dbo as prefix
I need to pull a large amount of data from various tables across a line that has very low bandwidth. I need to minimize the amount of data that gets sent too and fro.
On that side is a Sybase database, on this side SQL Server 2008.
What I need is to pull all the tables from the Sybase database that have to do with this office. Lets say I have the following tables as an example:
Farm
Tree
Branch
etc.
(one farm has many trees, one tree has many branches etc.)
Lets say the "Farm" table has a field called "CountryID", and I only want the data for where CountryID=12. The actual table structures I am looking at are very complex (and I am also not very familiar with them) so I want to try to keep the queries simple.
So I am thinking of setting up a series of views:
CREATE VIEW vw_Farm AS
SELECT * from Farm where CountryID=12
CREATE VIEW vw_Tree AS
SELECT * from Tree where FarmID in (SELECT FarmID FROM vw_Farm)
CREATE VIEW vw_Branch AS
SELECT * from Tree where BranchID in (SELECT BranchID FROM vw_Branch)
etc.
To then pull the actual data across I would then do:
SELECT * from vw_Farm into localDb.Farm
SELECT * from vw_Tree into localDb.Tree
SELECT * from vw_Branch into localDb.Branch
etc.
Simple enough to set up. I am wondering how this will perform though? Will it perform all the SELECT statements on the Sybase side and then just send back the result? Also, since this will be an iterative process, is it possible to index the views for subsequent calls?
Any other optimisation suggestions would also be welcome!
Thanks
Karl
EDIT: Just to clarify, the views will be set up in SQL Server. I am using a linked server using Sybase ASE to set up those views. What is worrying me in particular is whether the fact that the view is in SQL Server on this side and not on Sybase on that side will mean that for each iteration the data from the preceeing view will get pulled across to SQL Server first before the calculations get executed. I want Sybase to do all the calcs and just pass the results across.
It's difficult to be certain without testing, but my somewhat-relevant experience (using linked servers to platforms other than Sybase, and on SQL Server 2005) has been that using subqueries (such as your code for vw_Tree and vw_Branch) more or less guarantees that SQL Server will pull all the data for the outer table into a local temp table, then match it to the results of the inner query.
The problem is that SQL Server has no access to the linked server's table statistics, so can make no meaningful decisions about how to optimise the query.
If you want to be sure to have the work done on the Sybase server, your best bet will be to write code (could be views or stored procedures) on the Sybase side and reference them from SQL Server.
Linked server connections are, in my experience, not particularly resilient over flaky networks. If it's available, you could consider using Integration Services rather than linked-server queries - but even that may not be much better. You may need to consider falling back on moving text files with robocopy and bcp.
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.