SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


ALTER       PROC user_GetUserInAlphabetical
(
	@PageIndex int,
	@PageSize int,
        @Letter   nvarchar(1),
        @Count    int output 
        
)
as
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
SET @PageLowerBound = @PageSize * @PageIndex - @PageSize
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
create Table #IDs
(
	TempID int IDENTITY (1, 1) NOT NULL,
	EntryID nvarchar(50) not null
)
begin
Insert  into #IDs(EntryID)  select [UserID] from Users u  where u.[Account] Like @Letter+'%' order by u.Created desc
end

select  u.Account,c.Title,u.Created,u.Active,c.Theme,u.DateLastLogin from Users u inner join User_UserConfig c on (u.Account=c.Account)
   	INNER JOIN #IDS tmp ON (u.[UserID] = tmp.EntryID)
WHERE 	tmp.TempID > @PageLowerBound 
	AND tmp.TempID < @PageUpperBound
ORDER BY u.Created  desc
SELECT @Count=COUNT(*) FROM 	#IDS 
DROP TABLE #IDS
return @Count
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

