I am using MS SQL Server 2000 and have a link from a test db to a live db which is in replication. The link seems to work fine and I can select from any of the tables using any of the fields apart from the field with the constraints on creating ids. So if I run
select * from person where firstname like 'john' this works fine, but then if I run select * from person where id =1 then I get no data returned and I get no errors but the record exists.
Any advise is much appreciated.
Thanks
If select * from person where id =1 returns no rows then the record doesn't exist.
It's been a few years since I've worked with linked servers, but have you tried running profiler against the linked server (the live DB) to see that it's receiving the select statement and that it's receiving it correctly?
Can you see the record in enterprise manager through the dblink, or are you looking at the linked db directly?
Maybe your link is not pointing where you think it is.
Related
I have a service which is synchronizing 2 databases (software provider database, and our website database).
The website never changes the information, it only reads.
We have access to the software provider database connecting with SqlDbx (which I'm barely familiar with). The service synchronizing the databases is somewhat complicated, we have some parameters to see what tables gets synced and how. The service puts all the data into our SQL Server database and works fine. If we drop a table in SQL Server, the service rebuilds the whole table again.
But there is this case which is really weird.
I have this select on the SQL Server database which returns 4 rows:
SELECT SERIAL_NO
FROM TABLE
WHERE ID = 'D00357'
-- I get 4 rows from that.
-- let's say SERIAL1, SERIAL2, SERIAL3 and SERIAL4
But if I go to SqlDbx and run that same query:
SELECT SERIAL_NO
FROM TABLE
WHERE ID = 'D00357'
-- I only get 2 rows
-- let's say SERIAL1 and SERIAL2
Even more mysteriously, if I try to find the other 2 missing rows, by SERIAL_NO, they are there.
SELECT ID
FROM TABLE
WHERE SERIAL_NO = 'SERIAL3' OR SERIAL_NO = 'SERIAL4'
-- this results in 2 rows with 'D000357'
So, the software we using only shows the 2 rows we see from the first SELECT in SqlDbx. But the website shows the 4 rows from SQL Server, so they don't match.
We have dropped the table a few times and the syncing process always syncs back 4 rows.
Any ideas on how could I approach this? What can be causing this? Any suggestions will be very much appreciated.
EDIT
Adding photo link, hope it works.
unfortunately i create table call 'sysmessages' in SQL Server 2008. when i restore the DB to SQL Server 2012 i realize that i have two Tables call 'sysmessages'.
i don't want to change my table name because it using in the code.
can i remove only from specific database system table?
it is not a table, but a view
of course you cannot remove it, but you don't need to. It is in a different schema. You will not address it like select * from sys.sysmessages, you will address it like select * from dbo.sysmessages
"i don't want to change my table name because it is used in the code" - you can/should change the code as well :)
edit - no. 2. is not applicable in SQL 2012, however it is tested and working in SQL 2008R2
You cant drop system tables,your best bet is to change your code
I am trying to update a #temp table from a linked server using a top clause but am getting a very odd result - it seems to be ignoring the order by in my code.
I can fix the specific problem programatically but would like to know if this is a one off issue or there is a general problem with the way i have linked the servers.
The query is being run on a 2005 SQL Server (9.0.3042) linked to a 2008 SQL Server R2 (10.50.279) linked via the Microsoft OLE DB Provider for SQL Server.
The query looks like this - i have already created #TempTable which has the columns Id, Date and PrevDate and inserted data into the Id and Date columns.
update #TempTable
set PrevDate =
(select top 1
d.Date
from
linkedserver.DB.dbo.Date as d
where
d.Id = #TempTable.Id
and d.Date < #TempTable.Date
order by
d.Date desc)
The select is not picking the top 1, it appears to be picking the first date entered into the table for the specific Id and ignoring the order by clause.
When I just do a select, it works fine
When I put in the PrevDate via the inital insert, it works fine.
When I use a Max rather than a Top, it works fine.
When I run the exact same query/scenario on the 2008 SQL Server via a link (same provider) back to the 2005 SQL Server, it works fine
I am not looking for a fix for this specific query but would like to know if this is an isolated incedent or some fundamental problem with 2005 to 2008 linking that is going to manifest itself in very hard to find ways.
Thanks,
Tim
I have some bad news. I have replicated this issue here. It looks to me to be an issue with SQL Server 2005's query plan (I tested with a later version too, v 9.0.4035):
Image: Query plan for update statement
(sorry I can't embed images here, not enough reputation)
The details for that remote query:
Image: Remote Query details
There is no ORDER BY statement sent to the remote server, so the results that come back are indeed just the TOP (1) in an arbitrary order, which is likely to be in the order of the primary key. It seems to me that SQL Server should have emitted an ORDER BY Date desc there.
Therefore, I have to conclude that other queries like this, in this environment, could easily have the same issue (so, not an isolated incident).
By the way, there is a Microsoft Connect issue already raised for this one, from 2009: connect.microsoft.com/SQLServer/feedback/details/446017/missing-order-by-in-remote-query-with-update-statement
I am using Oracle 9i, Please suggest how can I select data from one remote database and insert the data in the local database?
Also suggest how the data can be copied from a remote to remote database.
You need to create a database link.
Please refer to this link: http://download.oracle.com/docs/cd/B10501_01/server.920/a96521/ds_concepts.htm#12354
excerpts:
example:
CREATE DATABASE LINK sales.us.americas.acme_auto.com CONNECT TO scott IDENTIFIED BY tiger USING 'sales_us';
query:
For example, using a database link to database sales.division3.acme.com, a user or application can reference remote data as follows:
SELECT * FROM scott.emp#sales.division3.acme.com; # emp table in scott's schema
SELECT loc FROM scott.dept#sales.division3.acme.com;
Based on the vagueness of the question. Make a backup of production and restore it in development.
If you are talking Microsoft SQL then you can create a Linked Server. Here is an article about doing this in SQL 2008, but you can do it in earlier versions as well. Then you can select from it using a four part name LinkedServer.database.schema.table
http://msdn.microsoft.com/en-us/library/ff772782.aspx
Define a link from the development server to the prooduction server. You can then use a select based insert to copy data into the development server.
Use the SAMPLE clause on the select to retrieve a percentage of the data. For child tables use a WHERE exists clause to copy child rows for which the parent was sampled.
Trying to figure out why there is such a significant difference between
select * from linkedserver..tablename
and
select * from openquery(linkedserver, select * from tablename).
4 minutes vs 13 seconds.
OPENQUERY connects to the destination server and runs the query on that server and returns the resultset. Whereas, I believe the Linked Server query is executed on the local server and runs across the connection.
Hope this helps.
Openquery connects directly to the destination server. Despite saying the RDBMS, you did not said what the other way to communicate with the data was.
Do you know if it is trying to enlist a distributed transaction over the linked server? If you watch the Trace output from Oracle when the calls are made, you should be able to see the difference in the requests happening. IIRC, the standard link query will request schema information from the oracle metadata in a different manner than the OPENQUERY call does. I don't know exactly why this happens, but I had a similar issue in the past that was resolved by using OPENQUERY.