Advertisement
evilbloodydemon

Untitled

Jul 27th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.81 KB | None | 0 0
  1. CREATE PROCEDURE [dbo].[GetMailReportData]
  2.     -- Add the parameters for the stored procedure here
  3.     @P_CampusId bigint,
  4.     @P_MailTypeID int=null,
  5.     @P_PageNo int,
  6.     @P_PageRows int,
  7.     @P_SortColum varchar(100)=null,
  8.     @P_SortOrder varchar(10)=null,
  9.     @P_SentByID  int=null,
  10.     @P_From Datetime=null,
  11.     @P_To Datetime=null,
  12.     @P_Status varchar(50)=null,
  13.     @P_Condition varchar(50)=null
  14.  
  15.    
  16. AS
  17. BEGIN
  18. BEGIN TRAN
  19.  
  20.     if(@P_CampusId!=0)
  21.     begin
  22.  
  23.     if(@P_Condition !='REPORT DATA')
  24.     BEGIN
  25.         select distinct TemplateId,TemplateName from Mail_Templates where CampusId=@P_CampusId
  26.         select distinct case when SentStatus is null  then 'Pending' else SentStatus end as Status, isnull(Status,0) as StatusID from SendMail where CampusId=@P_CampusId
  27.         select distinct US.UserId as SentByID,US.FirstName+' '+US.LastName as SentBy from SendMail SM inner join Users US on SM.CreatedBy=US.UserId where SM.CampusId=@P_CampusId
  28.     END
  29.     ELSE
  30.     BEGIN
  31.  
  32.         Declare @RowsFrom int,
  33.             @RowsTo int,
  34.             @varQuery NVARCHAR(max)='',
  35.             @varQuery1 NVARCHAR(max)='',
  36.             @varQueryCount NVARCHAR(max)=''; --Use this variable for find out the total rows of searching record.
  37.  
  38.     If (@P_PageNo is not null and @P_PageRows is not null)
  39.     Begin
  40.         Set @RowsTo     = ((@P_PageNo) * @P_PageRows);
  41.         Set @RowsFrom   = (((@P_PageNo) * @P_PageRows)+1) - @P_PageRows;       
  42.     End;
  43.    
  44.     Set @varQueryCount = 'SELECT Count(*) as TotalRowsCount From SendMail where CampusID='+cast(@P_CampusId AS varchar);
  45.    
  46.  
  47.     Set @varQuery = 'Select * from  (select Row_Number() over (order by Logid Asc) as RowIndex ,LogId,TemplateName as MailType,MT.TemplateId as MailTypeID,Subject,Body,SM.Status as StatusID,case when SentStatus is null then ''Pending'' else SentStatus end as Status,SM.CreatedOn,SM.MailSentOn as SentOn,SM.CreatedBy as SentByID,US.FirstName+'' ''+US.LastName as SentBy from SendMail SM
  48.     inner join Mail_Templates MT on MT.TemplateId=SM.TemplateId
  49.     inner join Users US on US.UserId=SM.CreatedBy
  50.     where SM.CampusId='+cast(@P_CampusId AS varchar);
  51.  
  52.  
  53.  
  54.    
  55.  
  56.     If (@P_MailTypeID is not null and @P_MailTypeID <> 0 and LEN(@P_MailTypeID) > 0)
  57.     Begin
  58.         Set @varQuery = @varQuery +' and MT.TemplateId='+ cast( @P_MailTypeID as varchar) ;            
  59.     End;
  60.    
  61.     If (@P_SentByID is not null and @P_SentByID <> 0 and LEN(@P_SentByID) > 0)
  62.     Begin
  63.         Set @varQuery = @varQuery +' and SM.CreatedBy = '+ cast (@P_SentByID as varchar);              
  64.     End;
  65.    
  66.  
  67.     If (@P_From is not null and @P_From <> '')
  68.     Begin
  69.         Set @varQuery = @varQuery +' AND CONVERT(date,MailSentOn) >= '''+ CONVERT(varchar(10),@P_From,101) +''' ';
  70.     End;
  71.  
  72.  
  73.     If (@P_To is not null and @P_To <> '')
  74.     Begin
  75.         Set @varQuery = @varQuery +' AND CONVERT(date,MailSentOn) <= '''+ CONVERT(varchar(10),@P_To,101) +''' ';
  76.     End;
  77.  
  78.     If (@P_Status is not null and @P_Status='Success' )
  79.     Begin
  80.         Set @varQuery = @varQuery +' AND SentStatus like ''%'+ cast( @P_Status as varchar)+'%''';
  81.     End;
  82.         If (@P_Status is not null and @P_Status='Pending')
  83.     Begin
  84.         Set @varQuery = @varQuery +' AND SentStatus is null';
  85.     End;
  86.         Set @varQuery = ''+ @varQuery +' '+ @varQuery1 +' ) as Sub WHERE Sub.RowIndex >= '+ cast(@RowsFrom as varchar) +'  and Sub.RowIndex <= '+ cast(@RowsTo as varchar) +'';    
  87.    
  88.  
  89.  
  90.     --Set @varQueryCount = ''+ @varQueryCount +' '+ @varQuery;
  91.  
  92.    
  93.  
  94.     If (@P_SortColum is not null and @P_SortColum <> '')
  95.     Begin
  96.         Set @varQuery = ''+ @varQuery +' '+ ' Order by Sub.'+ @P_SortColum +' '+ @P_SortOrder + '';
  97.     End;
  98.     ELSE
  99.     Begin
  100.         Set @varQuery = ''+ @varQuery +' '+ ' Order by Sub.logid Desc ';       
  101.     End;
  102.    
  103.         truncate table temp
  104.     insert into temp values(@varQueryCount)
  105.     Exec ( @varQueryCount ); --find the record count
  106.     Exec ( @varQuery); --display the record
  107.  
  108.     --select @varQuery;
  109.     --truncate table temp
  110.     insert into temp values(@varQuery)
  111.     end
  112.     END
  113.    
  114.    
  115.     IF(@@ERROR = 0)
  116.         COMMIT TRAN
  117.     ELSE   
  118.         ROLLBACK TRAN
  119.         RETURN (@@ERROR)
  120.     END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement