0

我正在尝试创建一个需要返回表的函数,但即使函数也没有创建,我需要返回结果表。

我的脚本是这样的

create function FNC_getPackageListById(@PkId int )
returns table
as
      return
        if exists (select Date1, Date2 from PromotionPackage 
                   where PkId = @PkId and Date1 is null and Date2 is null)
        begin
            select Rate,Remarks,PackageName from Package where PkId=@PkId
        end
        else
        begin
            select p.Rate,
                   p.Remarks,
                   p.PackageName,
                   pp.Date1,
                   pp.Date2
             from PromotionPackage pp,
                 Package p 
             where pp.PkId=p.PkId and  p.PkId=@PkId
       end 
   end
4

1 回答 1

1

该函数称为表值函数,它返回一个表。看这个例子:

CREATE FUNCTION TrackingItemsModified(@minId int)
RETURNS @trackingItems TABLE (
   Id       int      NOT NULL,
   Issued   date     NOT NULL,
   Category int      NOT NULL,
   Modified datetime NULL
) 
AS
BEGIN
   INSERT INTO @trackingItems (Id, Issued, Category)
   SELECT ti.Id, ti.Issued, ti.Category 
   FROM   TrackingItem ti
   WHERE  ti.Id >= @minId; 

   RETURN;
END;
于 2013-06-27T17:47:01.430 回答