Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Check if table exists on Linked Server

Author  Topic 

drako17
Starting Member

9 Posts

Posted - 2008-06-25 : 06:20:45
Hi

I am trying to check if some table exists in linked server.
The flow is this:
1.check if Linked Server Exists
2.Check if Needed DB exists
3.Check if needed table exists

Here the script:

IF EXISTS (SELECT 1 FROM [master].[dbo].[sysservers] WHERE srvname = 'LinkedServerName')

BEGIN
IF EXISTS(SELECT 1 FROM [LinkedServerName].[master].[dbo].[sysdatabases] WHERE [name]='DBNAME')

BEGIN
IF EXISTS (SELECT 1 FROM [LinkedServerName].[DBNAME].[dbo].[sysobjects] AS sysobjcts WHERE [sysobjcts].[name] ='TableName' AND [sysobjcts].[xtype]='U')

PRINT 'OK'
END
END

The problem occurs when the needed DB ('DBNAME') not exists in the Linked Server.
and i get the following error:

Server: Msg 7314, Level 16, State 1, Line 4
OLE DB provider 'LinkedServerName' does not contain table '"DBNAME"."dbo"."sysobjects"'. The table either does not exist or the current user does not have permissions on that table.
OLE DB error trace [Non-interface error: OLE DB provider does not contain the table: ProviderName='LinkedServerName', TableName='"DBNAME"."dbo"."sysobjects"'].

How can i check if table exists in linked server?

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-06-25 : 12:20:35
Hi,
try to build functions for checking if the wanted object exists and use the returnvalue of each function to make your controlflow.
It's because, i think, the parser is not looking at your if-statement at parse-time and then the not existing DBNAME always results in an error.
Greetings
Webfred
Go to Top of Page

drako17
Starting Member

9 Posts

Posted - 2008-06-25 : 14:34:05
Thanks
I will try and inform you if it succeded
Go to Top of Page

drako17
Starting Member

9 Posts

Posted - 2008-06-26 : 04:36:27
OK,

Its working,
But instead Function,I created Stored Procedure.
UDF cannot be called in Linked Server.

Thanks

Go to Top of Page
   

- Advertisement -