Based on the 2 databases below:
Database_A on Server_1
Database_B on Server_2
I have created a linked server to Database_B on Server_1 instance by name 'LS_B'.
I have a huge script file which basically creates required tables, functions, views, and stored procs on Database_A.
These functions, views and stored procs in turn refer to Database_B tables.
Problem is I have to now go and include OPENQUERY(LS_B,<query refering to database_b table>) everywhere in the script file.
For instance
create procedure some_proc
as
begin
select * from openquery(LS_B, 'select * from [Database_B].[dbo].[Table_1]');
end
Is there a better way to do this?
Pls. suggest.
Thanks
update
why does this fail
select top (50) * from LS_B.[Database_B].[dbo].[Table_1]
while the below works
select * from OpenQuery(LS_B, 'select top (50) * from Database_B.dbo.Table_1')
The error message on executing the first query is
Cannot process the object ""Database_B"."dbo"."Table_1"". The OLE DB provider "SQLNCLI10" for linked server "LS_B" indicates that either the object has no columns or the current user does not have permissions on that object
Both servers are on same domain.
OPENQUERYis only useful if you're connecting to a database other than SQL Server (or older versions that don't support particular syntax) because of the query pass-through. The pass-through lets you write for the other database in native SQL - IE: TO_DATE on Oracle, etc.