如何从存储过程返回用户定义的表类型

时间:2016-01-23 09:30:36

标签: sql sql-server stored-procedures sql-function

我的要求是根据传递的id从存储过程返回用户定义的表。

首先,我将表格类型创建为:

CREATE TYPE test AS TABLE 
(
    id int not null,
    name nvarchar(50) not null,
    value nvarchar(50) not null
)

现在我将我的存储过程定义为:

CREATE PROCEDURE [dbo].sp_test
    @id int,
    @racun_stavke dbo.test READONLY -- I need this as OUTPUT ?
AS
BEGIN
    SET NOCOUNT ON;

    /*FILL MY OUTPUT Type Table AS I LIKE and return*/
       //enter code here

END

1 个答案:

答案 0 :(得分:1)

您可以创建用户定义的函数而不是存储过程。它很直接,在https://technet.microsoft.com/en-us/library/aa214485(v=sql.80).aspx

处得到了很好的解释

示例:

CREATE FUNCTION LargeOrderShippers ( @FreightParm money )
RETURNS @OrderShipperTab TABLE
   (
    ShipperID     int,
    ShipperName   nvarchar(80),
    OrderID       int,
    ShippedDate   datetime,
    Freight       money
   )
AS
BEGIN
   INSERT @OrderShipperTab
        SELECT S.ShipperID, S.CompanyName,
           O.OrderID, O.ShippedDate, O.Freight
        FROM Shippers AS S INNER JOIN Orders AS O
              ON S.ShipperID = O.ShipVia
        WHERE O.Freight > @FreightParm
   RETURN
END