views:

69

answers:

1

Hi

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.

+1  A: 

If both servers are SQL servers you can use the following syntax:

select * from LS_B.[Database_B].[dbo].[Table_1]

It would depend on your exisitng syntax whether this would make it easier to do a find and replace.

You could also create a bunch of views on server 1 named after the tables refered to in the sps, then have these views refernce the linked server:

CREATE VIEW Table_1
AS
select * from LS_B.[Database_B].[dbo].[Table_1]
Ben Robinson
+1: You beat me, here's a link to the documentation on qualified name syntax: http://msdn.microsoft.com/en-us/library/aa172676%28SQL.80%29.aspx
OMG Ponies
A [synonym](http://technet.microsoft.com/en-us/library/ms177544.aspx) is an alternative to a view.
OMG Ponies
@Ben: I get the below error on executing 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.But the same thing works when i executeselect * from openquery(LS_B, 'select * from [Database_B].[dbo].[Table_1]')
stackoverflowuser
Yes that might be a better alternative.
Ben Robinson
@StackOverflowUser, hmm strange it is valid syntax. Do you get the same error if you use actual field names in the select statement instead of *
Ben Robinson
@Ben: Yes i get the same error with actual field names as well. I have edited my question to reflect the error.
stackoverflowuser
@Ben: "You could also create a bunch of views on server 1 named after the tables refered to in the sps, then have these views refernce the linked server". That is an interesting option. But will this affect performance? I thought the earlier option (referring to table using 4 part schema) might filter results on server_2 while creating view will fetch all rows from server_2 and filter on server_1. Is that a correct understanding?
stackoverflowuser
I am not 100% sure but i don't believe using views will make much difference to this. However OMG Ponies' sugestion synonyms is probably a better way than using views.
Ben Robinson
Thanks Ben. @OMG Ponies: Any suggestion in terms of performance on View vs Synonyms?
stackoverflowuser
@stackoverflowuser: No, a synonym just means you don't have to type the qualified name out in full (four part for linked servers). As long as you aren't doing any functions on the columns, `SELECT * FROM synonym` will perform exactly the same as `SELECT * FROM linked_view`.
OMG Ponies
Thanks @OMG Ponies and @Ben for the help.
stackoverflowuser