在链接服务器中获取OBJECT_ID
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在链接服务器中获取OBJECT_ID相关的知识,希望对你有一定的参考价值。
第二个选择(来自链接服务器)不返回任何值.. 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
答案
您还可以在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)
以上是关于在链接服务器中获取OBJECT_ID的主要内容,如果未能解决你的问题,请参考以下文章