5

实际上我在 sql server 2008 中创建了一个用户定义的表类型。结构如下所示。

我将它作为函数中的参数传递,并且该函数也返回该类型的表类型。我正面临这个问题,而我在函数中声明了该类型的变量,在其中插入了一些数据并返回了该参数。

表类型结构为:

Create Type OfferWithSubscription  as  Table                                        
   (                                        
     OfferID int, 
     OfferUserID  int,                                    
     OfferImage  varchar(200),                          
     OfferExactPrice Decimal(18,2),                                
     OfferContent varchar(max),
     OfferTitle varchar(100),                                    
     StartDate datetime,                                    
     EndDate datetime,                                    
     StartTime datetime,                                    
     StopTime datetime,                            
     ShowToUser bit,    
     SubID  int,                    
     SubLevel varchar(100)
   )

我想要创建的功能是:

CREATE FUNCTION FN_ShowOffer
(   
    @Gold int,
    @Silver int,
    @Bronze int,
    @table dbo.OfferWithSubscription Readonly )
RETURNS dbo.OfferWithSubscription 
AS
BEGIN

DECLARE @ReturnTable AS dbo.OfferWithSubscription;
Declare @Case as varchar(20)
         if(@Gold=0 and @Silver=1 and @Bronze=0 )
            begin
            set @Case='1S'
            end
           if(@Case='1S')
           Begin
                   insert into @ReturnTable                                    
                   select OfferID, OfferUserID, OfferImage, 
                     OfferExactPrice, OfferContent,
                     OfferTitle, StartDate, EndDate, 
                     StartTime, StopTime, ShowToUser,
                     SubID, SubLevel
                   from @table
                   where SubID=4 
           End

RETURN (

@ReturnTable
)
END
4

1 回答 1

4

您只需要像下面这样扩展类型。
仅供参考 - T-SQL 函数可以返回用户定义的表类型吗?

CREATE FUNCTION FN_ShowOffer
(   
    @Gold int,
    @Silver int,
    @Bronze int,
    @table dbo.OfferWithSubscription Readonly )
RETURNS @ReturnTable Table                                        
   (                                        
     OfferID int, 
     OfferUserID  int,                                    
     OfferImage  varchar(200),                          
     OfferExactPrice Decimal(18,2),                                
     OfferContent varchar(max),
     OfferTitle varchar(100),                                    
     StartDate datetime,                                    
     EndDate datetime,                                    
     StartTime datetime,                                    
     StopTime datetime,                            
     ShowToUser bit,    
     SubID  int,                    
     SubLevel varchar(100)
   )
AS
BEGIN
Declare @Case as varchar(20)
         if(@Gold=0 and @Silver=1 and @Bronze=0 )
            begin
            set @Case='1S'
            end
           if(@Case='1S')
           Begin
           insert into @ReturnTable                                    
             select OfferID,OfferUserID,OfferImage,OfferExactPrice,OfferContent,OfferTitle,
             StartDate,EndDate,StartTime,StopTime,ShowToUser,SubID,SubLevel from @table where SubID=4 
           End

RETURN
END

为了进一步澄清,这完全兼容并可分配给该表类型的变量,例如SQL Fiddle

declare @t OfferWithSubscription
insert @t
select * from fn_showoffer(1,2,3,@t)
于 2012-09-26T11:21:48.177 回答