How to create a function that takes a custom table type as a parameter and returns it in sql?

In fact, I created the User defined table type in sql server 2008. The structure is as follows.

I pass it as a parameter in the function, and this function also returns the type of the table type. I ran into a problem while I declare a variable of this type in a function, inserting some data into it and return this parameter.

Table type structure:

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)
   )

And the function I'm trying to create:

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
+5
source share
1 answer

You just need to expand the type as shown below.
FYI - Can a T-SQL function return a custom table type?

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)
+4

All Articles