在用户定义的函数中声明表变量

时间:2016-03-26 05:15:54

标签: sql sql-server tsql user-defined-functions

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

    **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;

代码正在运行,但我想在用户定义的函数中创建一个表变量,这样我就可以轻松地重用它。我用来声明表变量的代码如下所示:

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'.

我无法弄清楚我是怎么弄错的。请帮忙。

1 个答案:

答案 0 :(得分:3)

来自multi statement table valued function

Inline table valued function语法略有不同

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

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