if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_GetSiteLicenseUsageReports]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[usp_GetSiteLicenseUsageReports] GO CREATE PROCEDURE [dbo].[usp_GetSiteLicenseUsageReports] -- Add the parameters for the stored procedure here @sFromDate varchar(20), @sToDate varchar(20), @sAccoutNumber varchar(50)='', @iEditionId tinyint = 0, @pageNo int, @pageLength int, @recordCount int out AS BEGIN IF 1=0 BEGIN SET FMTONLY OFF END -- SET NOCOUNT ON added to prevent extra result sets from SET NOCOUNT ON DECLARE @dtFromDate DATETIME DECLARE @dtToDate DATETIME -- convert the datatype of fromdate & todate parameter to datetime SELECT @dtFromDate = CONVERT(DATETIME,@sFromDate) SELECT @dtToDate = DATEADD(ms,-3,DATEADD(DAY,1,CONVERT(DATETIME,@sToDate))) Select RowNum,AccountNumber, EditionTitle, ReferalUrl, InstitutionName, LicenseCreationDate,TotalLogins,LastLogin from ( SELECT ROW_NUMBER() OVER (ORDER BY UserLoginLog.AccountNumber) AS RowNum , UserLoginLog.AccountNumber, Edition.Title AS EditionTitle, UserLoginLog.ReferalUrl, (SELECT License.InstitutionName FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as InstitutionName, (SELECT CONVERT(VARCHAR,License.CreationDate,101) FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as LicenseCreationDate, COUNT(DISTINCT UserLoginLog.LogDate) AS TotalLogins, CONVERT(VARCHAR,MAX(UserLoginLog.LogDate),101) AS LastLogin FROM UserLoginLog INNER JOIN Edition ON UserLoginLog.Edition = CAST(Edition.Id AS NVARCHAR) WHERE UserLoginLog.FailureId IS NULL AND UserLoginLog.LogDate BETWEEN @dtFromDate AND @dtToDate AND UserLoginLog.AccountNumber = (CASE WHEN LEN(@sAccoutNumber) > 0 THEN @sAccoutNumber ELSE UserLoginLog.AccountNumber END) AND Edition.IsActive = 1 AND Edition.Id = (CASE WHEN @iEditionId > 0 THEN @iEditionId ELSE Edition.Id END) GROUP BY UserLoginLog.AccountNumber, Edition.Title, UserLoginLog.ReferalUrl) as usr WHERE RowNum > @pageLength * (@pageNo - 1) AND RowNum <= @pageLength * @pageNo order by AccountNumber --Calculate total number of records select @recordCount = count(ResultTable.AccountNumber) from ( SELECT UserLoginLog.AccountNumber, Edition.Title AS EditionTitle, UserLoginLog.ReferalUrl, (SELECT License.InstitutionName FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as InstitutionName, (SELECT CONVERT(VARCHAR,License.CreationDate,101) FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as LicenseCreationDate, COUNT(DISTINCT UserLoginLog.LogDate) AS TotalLogins, CONVERT(VARCHAR,MAX(UserLoginLog.LogDate),101) AS LastLogin FROM UserLoginLog INNER JOIN Edition ON UserLoginLog.Edition = CAST(Edition.Id AS NVARCHAR) WHERE UserLoginLog.FailureId IS NULL AND UserLoginLog.LogDate BETWEEN @dtFromDate AND @dtToDate AND UserLoginLog.AccountNumber = (CASE WHEN LEN(@sAccoutNumber) > 0 THEN @sAccoutNumber ELSE UserLoginLog.AccountNumber END) AND Edition.IsActive = 1 AND Edition.Id = (CASE WHEN @iEditionId > 0 THEN @iEditionId ELSE Edition.Id END) GROUP BY UserLoginLog.AccountNumber, Edition.Title, UserLoginLog.ReferalUrl) as ResultTable; END