LINKED Server does not return all rows - sql

This is a little strange for me. I have script like following
DECLARE #maxCustId INT
SELECT #maxCustId = MAX(CustomerId) FROM Customers
SELECT * INTO #temp FROM linkserver.DB.dbo.Customers where CustomerId > #maxCustId
-- Copy records to local db Custome table
DROP TABLE #temp
But sometimes #temp does not fetch all records from linked sever.
Like I had Max CustomerId = 1138 in my local db and when I run above script my temp table (which fetch records from linked server) misses CustoemrIds 1140, 1141. Linked server has customers upto 1160. #temp table has records upto 1160 but misses two records i.e. 1140, 1141
I run that script again and again and on 4th attempt the record 1141 added in #temp table but record 1140 was still missing.
I then put following query on local server to check record in linked server
SELECT * FROM linkserver.DB.dbo.Customers where CustomerId = 1140
Above query return no records.
To verify this I went to linked server and wrote same query on server from where I have created LINKED server.
-- This is the server which I am using as linked server in my local server
SELECT * FROM Customers where CustomerId = 1140
Custoemr 1140 was in db, which I was sure that it would be there but I run above script to verify it.
I come back to my local server and run this query
SELECT * FROM linkserver.DB.dbo.Customers where CustomerId = 1140
This time my linked server returns customer 1140 which it was not returning earlier.
I run the whole query again and now my #temp table has all records.
I am so confused that why first time linked server did not return all records.
This is sample from a long procedure which copy records from linked server to local server and because of this reason my local db has less records than linked server db.
Linked server: SQL Server 2005 Standard with SP2
Local Server: SQL Server 2008 Web Edition with SP2
Provider: SQL Native Client 10
Linked server security options:
Be made using the securing context
Remote Login
With Passowrd
login user details:
Server Roles: public and sysadmin
User Mappings: sa is not mapped with
db I query.
Linked server db updated frequently
No NOLOCK at any point in query
Any help.

I have seen similar behaviour with linked servers (although usually Oracle not SQL Server) and the problem was traced to the driver being used for the linked server.
I see you have SQL Native Client 10 as the provider, could you try using Microsoft OLE DB Provider for SQL Server instead and seeing if it makes a difference?

Also would be curious to know if you have the same problem when you use OpenQuery instead of the four-part naming convention:
SELECT * FROM OpenQuery(linkserver,'SELECT * FROM Customers where CustomerId = 1140')
IIRC, that should be faster, anyway... I believe that would force it to take care of the "where" part on the linked server and only return the appropriate values as opposed to returning the whole dataset and doing the filterin' on the destination side. Of course, I'm also pretty sure I saw a UFO at my Granny's house when I was eight, so take my wisdom with a grain of salt.

Related

Query SQL Linked Server only pulling data from one server

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 select a table from one machine to another using sql server 2008 r2?

I have explained the scenario below:
I am having two servers:
server 1
server 2
In both the servers I have Sql Server 2008 r2.
In server 1, I have a database named "DB_Server1" and in server 2, a database named "DB_Server2".
In DB_server1 database, I have a table named "TB_Server1" and in DB_Server2, a table named "TB_Server2".
My requirement is, in DB_Server1, I'm going to write a stored procedure which selects the table "TB_Server2" from DB_Server2 which is located in the server 2.
How can I achieve the above requirement?
you can use linkserver
at first select Server1
step 1: exec sp_addlinkedserver 'Server2'
step 2: select * from [Server2].[DB_Server2].TB_Server2
You can create linked server (http://msdn.microsoft.com/library/ff772782.aspx)
Use Linked Server:
Allows you to query databases and tables on remote servers as though they are part of the local database. Very easy to setup (just call exec sp_addlinkedserver) and once defined uses nothing but plain old SQL.
Here is a simple tutorial about how to create a linked server. After creating linked server, we can query it as follows:
select * from LinkedServerName.DatabaseName.dbo.TableName
Click here for another tutorial.
Read more about Linked Servers here.

Can I create a view that will query a table from another sql server on another server but same domain

I need to query a table from another SQL Server on a different server but same domain, but I am not sure how I will be able to do it.
I tried solution given in this answer Can I create view in my database server from another database server but it doesn't work for me as I got SQL Server 2000 (please don't hate :-) ).
When I try solution given then i get this error,
Line 23: Incorrect syntax near '-'.
which is because command is not compatible with SQL Server 2000.
Edit
SELECT * FROM AnotherServer.AnotherServerDatabase.Server.Table1
you can link the servers and run cross server queries as long as you put the server name before the DB your running the query on.
For example
SELECT * FROM "linkedserver".dbo.aTable
(without "" marks )
bear in mind different server versions though. I run cross server queries from 2008 to 2000 servers and its a pain adapting :)

How to retrieve records from server database into local database

I have a little problem. I want to create a query in my local database (tijdsregistratie.mdf) to retrieve rows from my server database (IT Solutions Develop.dbo) on server itshou-dev03\sql2008.
But I don't know how to connect to the server database. I tried it like this :
select TOP 10 * from [IT Solutions Develop].dbo.[IT Solutions BVBA$Planning]
.. but it gives me this error :
Invalid object name 'IT Solutions Develop.dbo.IT Solutions
BVBA$Planning'.
One way is to link the servers:
http://msdn.microsoft.com/en-us/library/ms188279.aspx?ppud=4
You can also define linked servers by using SQL Server Management
Studio. In the Object Explorer, right-click Server Objects, select
New, and select Linked Server. You can delete a linked server
definition by right-clicking the linked server name and selecting
Delete.
This is the process by which you tell SQL Server where another server is and how to connect to it. You can do this in SQL Server Management Studio or in T-SQL. You can then refer to the linked server by a four part name (similar to what is in your question):
[LinkedServerName].[Database].[Schema].[Object]

timeout on query against small table

I'm trying to do a query that gets all the ids from a table where the column document(varbinary(max)) is null.
The query always timesout, and I'm running it against a 5000-row table.
select ID from Invoice where Document is null
Im using SQL Express 2008 R2 and Sql Management Studio. Is this the right way? Am I missing something? Even if I add top 1 the query time out
Sometimes the tables gets locked. Try clicking away from the selected Table in your interface.
What RDBMS are you using? SQLserver? Sybase? or..
Does it still time out when you do:?
SELECT TOP 10 ID
from Invoice where Document is null
Do it this way, for a read-only look
select ID from Invoice (nolock) where Document is null
If connecting on localhost Then
If connecting from Management Studio Then
Try restarting SQL Server services...could be locks.
Try restarting machine...could be locks.
ElseIf connecting from remote app code Then
Check if SQL Server is setup for remote connections.
Check connection strings.
Check seccurity privleges.
Check log file.
End If
Else
Check if SQL Server is setup for remote connections
Check connection strings.
Check seccurity privledges.
End If