创建 TVP 函数时必须声明表变量错误

Posted

技术标签:

【中文标题】创建 TVP 函数时必须声明表变量错误【英文标题】:Must declare the table variable error while creating a TVP function 【发布时间】:2019-11-16 04:38:38 【问题描述】:

我使用 TVP 创建了以下函数。我最初将数据插入 TVP,然后从中进行选择。

CREATE FUNCTION [dbo].employees_data(
  @employeeIds NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
-- WITH SCHEMABINDING
AS
BEGIN

  CREATE TYPE employees_data
  AS TABLE (can_view_all BIT,role_id INT ,employee_id INT);

  DECLARE @EmployeeDataTVP AS employees_data;

  INSERT INTO @EmployeeDataTVP (can_view_all, role_id, employee_id)
  SELECT
      ur.role_id,
      ua.employee_id
      INNER JOIN user_roles ur ON ur.user_account_id = ua.id
      WHERE ua.employee_id IN (SELECT * FROM STRING_SPLIT(@employeeIds, ','));


   RETURN (SELECT
    (
      CASE
      WHEN ed.role_id = 1 THEN (
        SELECT
          cc.id,
          ISNULL(cc.standard_name, cc.localized_name) AS name,
          cc.client_cost_center_id AS cost_center_id
        FROM s eccs
        LEFT JOIN centers cc on eccs.center_id = cc.id
        WHERE eccs.employee_id = ed.employee_id
        FOR JSON PATH
      )
      ELSE NULL
    END
    ) AS managed_places,
    ed.employee_id
  FROM @EmployeeDataTVP ed
   )
END

但是当我执行该函数时,我得到了Must declare the table variable \"@EmployeeDataTVP\". 错误。我无法弄清楚我做错了什么。

【问题讨论】:

table-valued parameter 在哪里?您应该使用一个而不是逗号分隔的值列表。 【参考方案1】:

您不需要创建类型而是创建表变量:

CREATE FUNCTION [dbo].employees_data(
  @employeeIds NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
-- WITH SCHEMABINDING
AS
BEGIN

  DECLARE @EmployeeDataTVP TABLE (can_view_all BIT,role_id INT ,employee_id INT);


  INSERT INTO @EmployeeDataTVP (can_view_all, role_id, employee_id)
  SELECT
      ur.role_id,
      ua.employee_id
      INNER JOIN user_roles ur ON ur.user_account_id = ua.id
      WHERE ua.employee_id IN (SELECT * FROM STRING_SPLIT(@employeeIds, ','));


   RETURN (SELECT
    (
      CASE
      WHEN ed.role_id = 1 THEN (
        SELECT
          cc.id,
          ISNULL(cc.standard_name, cc.localized_name) AS name,
          cc.client_cost_center_id AS cost_center_id
        FROM s eccs
        LEFT JOIN centers cc on eccs.center_id = cc.id
        WHERE eccs.employee_id = ed.employee_id
        FOR JSON PATH
      )
      ELSE NULL
    END
    ) AS managed_places,
    ed.employee_id
  FROM @EmployeeDataTVP ed
   )
END

【讨论】:

谢谢它的工作。但我得到Only one expression can be specified in the select list when the subquery is not introduced with EXISTS 错误我相信这是因为(SELECT * FROM STRING_SPLIT(@employeeIds, ',') 声明。有什么办法可以消除错误吗? 尝试用SELECT [value] FROM STRING_SPLIT(@employeeIds, ',')更改它 为什么要使用多线 TVF?这看起来很容易成为一个内联 TVF,它的性能会更好。 内嵌 tvf 如何工作。有相关文档吗?

以上是关于创建 TVP 函数时必须声明表变量错误的主要内容,如果未能解决你的问题,请参考以下文章

表变量和错误“必须声明标量变量”

更新另一个表时必须声明标量变量@tablename [重复]

SP2-0552:未声明绑定变量“NEW”并且 END 错误报告 - 未知命令

必须用参数声明标量变量

带有 std::map 的模板函数给出错误:声明为 void 的变量或字段

JavaScript - 运行函数时声明的变量未定义错误