在 UDF 中声明 table 变量以输入 table 名称作为参数

Declare table variable in a UDF to enter table name as a parameter

我正在处理一个查询并创建了一个函数来使用以下代码从 outcomes table 中获取结果。

CREATE FUNCTION dbo.Shippad (@tbl NVARCHAR(30))
RETURNS TABLE
AS
    RETURN
      SELECT LEFT(ship, Charindex(' ', ship) - 1) + ' '
             + Replicate('*', Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) + 1 -2)
             + ' '
             + Reverse(LEFT(Reverse(ship), Charindex(' ', Reverse(ship)) - 1)) as final_work 
      FROM   outcomes
      WHERE  Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) > 1 

上面代码的问题在于,执行时不管我输入什么参数,只要我输入的参数是数据库中有效的table名称,它总是给出结果.

现在我想知道是否可以创建一个参数来将 table 名称输入到 FROM 部分,这样只有当我输入 outcomes 时它才会显示结果。

我尝试使用以下代码声明一个 table 变量:

declare @ship_outcome table
(   final_work nvarchar(30)
)

insert into @ship_outcome (final_work)
select 
        left(ship, charindex(' ', ship) - 1) + ' ' + 
        replicate('*', charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) + 1 -2) + ' ' +
        reverse(left(reverse(ship), charindex(' ', reverse(ship)) - 1))
from outcomes
where charindex(' ', substring(ship, charindex(' ', ship) + 1, len(ship))) > 1;

select * from @ship_outcome

但是我不确定如何将 table 变量合并到 UDF 中。请帮忙。

您还可以在多语句 table 值函数中使用 IF 语句来实现结果,如下所述:-

CREATE FUNCTION dbo.Shippad (@tbl NVARCHAR(30))
RETURNS @t table
(final_work nvarchar(30))
AS
begin
if @tbl = 'outcomes'
begin
      Insert into @t
      SELECT LEFT(ship, Charindex(' ', ship) - 1) + ' '
             + Replicate('*', Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) + 1 -2)
             + ' '
             + Reverse(LEFT(Reverse(ship), Charindex(' ', Reverse(ship)) - 1)) as final_work 
      FROM   outcomes
      WHERE  Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) > 1 
end
return
end

注意 - 多语句 table 值函数的性能对于大型记录来说很糟糕。建议在内联 table 值函数的 where 子句中使用 @tbl = 'outcomes' 来使用内联函数,例如 -

CREATE FUNCTION dbo.Shippad (@tbl NVARCHAR(30))
RETURNS TABLE
AS
    RETURN
      SELECT LEFT(ship, Charindex(' ', ship) - 1) + ' '
             + Replicate('*', Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) + 1 -2)
             + ' '
             + Reverse(LEFT(Reverse(ship), Charindex(' ', Reverse(ship)) - 1)) as final_work 
      FROM   outcomes
      WHERE  Charindex(' ', Substring(ship, Charindex(' ', ship) + 1, Len(ship))) > 1 and @tbl = 'outcomes'