HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlMinor

Unable to query SQL server metadata through linked server

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlqueryunablethroughserverlinkedmetadata

Problem

I have a scenario where a user wants to do the following through a linked server:

SELECT *
FROM [DBTEST].[AdventureWorks].sys.objects
WHERE object_id = OBJECT_ID(N'dbo.DatabaseLog');


The linked server is configured to use Be made using this security context and the linked server login has read access on the [DBTEST].[AdventureWorks] database.

The above query does not return any rows when executed through the linked server. However, when I login directly to DBTEST using the linked server login and do the following, it does return a row:

SELECT * 
FROM [AdventureWorks].sys.objects
WHERE object_id = OBJECT_ID(N'dbo.DatabaseLog')


I am confused why it is not working through the linked server and what I need to do in order to fix it.

One more strange thing is even if I grant sa rights to the linked server login it still does not return any rows when executed through the linked server.

Both servers are SQL Server 2008 Enterprise Edition.

Solution

The issue is not security related :-).

In your query, you are selecting from a table on a remote system, but the WHERE condition is using the OBJECT_ID() function, which can only see the local instance, and is not running in the scope of the Linked Server connection.

Why don't you try:

select *
from [DBTEST].[AdventureWorks].sys.objects
where [name] = N'DatabaseLog';


And that query makes more sense anyway since you already had the name, so no sense in querying to get the ID from the name just to use the ID in a second query ;-).

If you want / need to also filter on the Schema name, then you will have to add the JOIN to the [DBTEST].[AdventureWorks].sys.schemas table as OBJECT_SCHEMA_NAME is also local scope only.

Context

StackExchange Database Administrators Q#116147, answer score: 3

Revisions (0)

No revisions yet.