那么为什么这行不通呢?
<!-- language: lang-sql -->
DECLARE @tn NVARCHAR
SET @tn = 'MyTable'
SELECT OBJECT_ID(@tn)
当这样做时:
<!-- language: lang-sql -->
SELECT OBJECT_ID('MyTable')
我需要将一个变量传递给这个函数。
最佳答案
确实如此。
declare @name sysname = N'sys.objects';
select object_id(@name);
-----------
-385
(1 row(s) affected)
在您的示例中,您声明了一个长度为 1 的变量(因为您是 omit the length )。 OBJECT_ID(N'M')
未找到任何内容并返回 NULL。
When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.
关于sql - 为什么 SQL Server 的 OBJECT_ID(NVARCHAR) 不带变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8073442/