在用户定义的函数中声明一个 table 变量

Declare a table variable within a user defined function

我正在处理示例查询,这是我得到的唯一 outcomes table:

    **ship**
Bismarck
California
California
Duke of York
Fuso
Hood
King George V
Kirishima
Prince of Wales
Rodney
Schamhorst
South Dakota
Tennessee
Washington
West Virginia
Yamashiro

我正在努力用 * 替换字符串中第一个和最后一个空格之间的字符。我得到了以下正确的代码:

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;

代码正在运行,但我想在用户定义的函数中创建一个 table 变量,以便我可以毫不费力地重用它。我用来声明 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

问题是,当我使用以下代码使其成为用户定义函数时:

CREATE FUNCTION dbo.shippad (@tbl nvarchar(30))
RETURNS TABLE
AS
RETURN 

    declare @ship_outcome table
    (
        final_work nvarchar(30)
    )

    insert into @ship_outcome

    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
;

系统说Incorrect syntax near the keyword 'declare'.

我不知道我是怎么弄错的。请帮忙。

multi statement table valued functionInline table valued function

的语法有点不同

您需要为 multi statement table valued function 使用 BEGIN..END

CREATE FUNCTION dbo.Shippad (@tbl NVARCHAR(30))
RETURNS @ship_outcome TABLE (
  final_work NVARCHAR(30))
AS
  BEGIN
      INSERT INTO @ship_outcome
      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

      RETURN
  END;

但我更愿意Inline table valued function这样做

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