2013-06-27 32 views
0

我想創建一個函數,它需要返回一個表,但即使函數也不是,我需要返回結果表。如何在SQL Server中聲明表?

我的劇本就像是返回table這個

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 [email protected] 
     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 [email protected] 
     end 
    end 

回答

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;