I have this query which involves a table from another server:
select count(serialno), max(convert(varchar(10),Crtd_DateTime,101))
from tblSBox tsb (nolock)
inner join tblMBox tmb (nolock) on tsb.idMaster = tmb.idMaster
inner join [server_2].fdb.dbo.sns_in_out sio
on tsb.serialno = sio.lotsernbr and invtMult='-1' and RefNbr like 'I%'
where tmb.WO=34612
but this query is taking forever, and I cannot specify an index or locking hint for a remote data source, so I looked into the web and I found out that I can use this:
SELECT * FROM OpenQuery(server, 'Select * from sometable with (nolock)')
Which works fine, but as you can see I have a link between tsb.serialno and sio.lotsernbr, and when I place all that inside the ” like the following:
select serialno,
(SELECT *
FROM OpenQuery([server_2], 'select convert(varchar(10), Crtd_DateTime, 101)
from fdb.dbo.sns_in_out sio (nolock)
where (tsb.serialno = sio.lotsernbr
and invtMult=''-1'' and RefNbr like ''I%'')')) as crtDate
from tblSBox tsb (nolock)
inner join tblMBox tmb (nolock) on tsb.idMaster = tmb.idMaster
where tmb.WO=34612
I get this error:
[OLE/DB provider returned message: Deferred prepare could not be
completed.]
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier “tsb.serialno” could not be bound.
Any idea of how to accomplish this?
If you want to run queries with
(NOLOCK)on a remote server, create a stored procedure or view on the remote server and apply the locking hint there.If you have no access to the remote server, you could pull the data first by executing something like this through a linked server (I’ll assume your linked server is called server_2):
Now you can join against the local #temp table. The difference may be negligible but it may be significant. If this brings across a lot of rows and you only need a few, you may want to do it slightly differently: build your
INlist dynamically and pass it to the other server, e.g.