CREATE   FUNCTION  [dbo].[GetWearHouseName]  (@Id Bigint)
RETURNS
@WearHouseName TABLE
   (
     SenderName NVARCHAR(35),
	 ReciverName NVARCHAR(35)
   )
AS  
BEGIN 
DECLARE @ReciverName NVARCHAR(MAX); 
DECLARE @SenderName NVARCHAR(MAX); 

SELECT  @ReciverName = STUFF(( SELECT   ', ' + Reciver
                               FROM     ( SELECT    Reciver
                                          FROM      TmpHavaleh
                                          WHERE     Id_Havaleh = @Id
                                          GROUP BY  Reciver
                                        ) AS T
                               GROUP BY Reciver
                             FOR
                               XML PATH('')
                             ), 1, 2, '');
IF LEN(@ReciverName) <> 3
    SELECT  @ReciverName = @ReciverName
ELSE
    SELECT  @ReciverName = Name
    FROM    AnbarId
    WHERE   A_Code = @ReciverName

SELECT  @SenderName = STUFF(( SELECT    ', ' + Sender
                              FROM      ( SELECT    Sender
                                          FROM      TmpHavaleh
                                          WHERE     Id_Havaleh = @Id
                                          GROUP BY  Sender
                                        ) AS T
                              GROUP BY  Sender
                            FOR
                              XML PATH('')
                            ), 1, 2, '');
IF LEN(@SenderName) <> 3
    SELECT  @SenderName = @SenderName
ELSE
    SELECT  @SenderName = Name
    FROM    AnbarId
    WHERE   A_Code = @SenderName

INSERT @WearHouseName
SELECT  @SenderName SenderName ,
        @ReciverName ReciverName
RETURN
END
