0

我已经定义了返回包含 2 列的表的函数。我可以重命名这些列,以便生成的表如下所示:

Press name  |  Sum of pages

?

CREATE FUNCTION F_3
(@press nvarchar(255))
RETURNS @table TABLE ( Press nvarchar(255),
                       PagesSum int )
AS
BEGIN
    INSERT @table SELECT @press, SUM(Books.Pages)
    FROM Books, Press
    WHERE Press.Name = @press AND
        Books.Id_Press = Press.Id
    GROUP BY Press.Name

    RETURN
END
GO

SELECT * FROM F_3('BHV')
GO

我试过这样做

Press AS 'Press name' nvarchar(255)

但这行不通。

4

1 回答 1

1

你是这个意思吗?

CREATE FUNCTION F_3
(@press nvarchar(255))
RETURNS @table TABLE ( [Press name]  nvarchar(255),
                   [Sum of pages] int )
AS
BEGIN
    INSERT @table SELECT @press, SUM(Books.Pages)
    FROM Books, Press
    WHERE Press.Name = @press AND
        Books.Id_Press = Press.Id
    GROUP BY Press.Name

    RETURN
END
GO

SELECT * FROM F_3('BHV')
GO

我刚刚更改了您的函数的 RETURNS 行。除此之外,这个函数的性能很差,因为它不是一个内联表值函数。查看 MSDN 在http://technet.microsoft.com/en-us/library/ms177499(v=sql.90).aspx提供的示例

于 2012-11-28T05:01:48.453 回答