2012-09-26 10 views
5

In realtà ho creato un tipo di tabella definito dall'utente in SQL Server 2008. La struttura è riportata di seguito.Come creare una funzione che prenda il tipo di tabella definito dall'utente come parametro e restituisca lo stesso in sql?

Lo sto passando come parametro in funzione e quella funzione restituisce anche quel tipo di tipo di tabella. Sto affrontando il problema, mentre dichiaro una variabile di quel tipo in funzione, inserendo alcuni dati in essa e restituisco quel parametro.

Tabella tipo di struttura è:

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

E la funzione, quello che sto cercando di creare è:

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 

risposta

4

Dovrete solo per espandere il tipo come qui di seguito.
proposito - Can T-SQL function return user-defined 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 

E per chiarire ulteriormente, che è interamente compatibile e assegnabile a una variabile di quel tipo di tabella, ad esempio SQL Fiddle

declare @t OfferWithSubscription 
insert @t 
select * from fn_showoffer(1,2,3,@t) 
+0

Signor Richard, grazie mille per la risposta rapida. È utile per me .. –

+0

Mr. Mahmoud Gamal, mostra l'errore che "Dichiara la variabile scalare" @ReturnTable "." –

+0

Hai perso la riga 'RETURNS @ReturnTable Table' nell'intestazione della funzione? – RichardTheKiwi

Problemi correlati