在链接服务器中获取OBJECT_ID

时间:2016-03-14 10:11:10

标签: tsql linked-server

第二个select(来自链接服务器)不返回任何值.Object_ID不起作用。有任何解决方法吗?

select '', name 
FROM sys.databases
WHERE 1 = 1
    AND NAME <> db_name() -- exclude current database
    AND CASE 
        WHEN STATE = 0
            THEN CASE 
                WHEN OBJECT_ID(NAME + '.dbo.tPA_SysParam', 'U') IS NOT NULL
                        THEN 1
                    END
        END = 1
union
select '[LINKED]', name
FROM [LINKED].master.sys.databases
WHERE 1 = 1
    AND CASE 
        WHEN STATE = 0
            THEN CASE 
                    WHEN OBJECT_ID('[LINKED].'+NAME +     '.dbo.tPA_SysParam', 'U') IS NOT NULL
                        THEN 1
                    END
        END = 1

1 个答案:

答案 0 :(得分:0)

您还可以在PARSENAME函数的一点帮助下模仿OBJECT_ID:

Declare @FullTableName nvarchar(max) = '[dbo].[MyTable]';
Select t.object_id
    From [LINKED].MyDatabase.sys.tables As t
    Inner Join [LINKED].MyDatabase.sys.schemas As s On t.schema_id = s.schema_id
    Where t.[name] = PARSENAME(@FullTableName, 1)
    And s.[name] = PARSENAME(@FullTableName, 2)