IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'UserMstSelectByConditions')
	BEGIN
		DROP  Procedure  UserMstSelectByConditions
	END

GO
/***********************************************
***功能描述：
***传入传出参数：
***	@parameter1：
***	@parameter2：

***作者：
***建立时间:2009-1-6
***更改时间：
***更改人：
***更改原因：
****************************************************/
CREATE PROCEDURE UserMstSelectByConditions
	(
	@SubSystemID varchar(6),
	@UserName varchar(50),
	@EMail varchar(100),
	@LastLoginTimeBegin datetime,
	@LastLoginTimeEnd datetime,
	@CreateTimeBegin datetime,
	@CreateTimeEnd datetime,
	@PageSize int,
	@CurrentPage int output,
	@TotalPage int output,
	@TotalRecord int output,
	@SortBy varchar(100)
	)
AS


--把所有单引号都替换成两个单引号
SET @UserName = REPLACE(LTRIM(RTRIM(@UserName)),'''','''''')
SET @EMail = REPLACE(LTRIM(RTRIM(@EMail)),'''','''''')
SET @SortBy = REPLACE(LTRIM(RTRIM(@SortBy)),'''','''''')

DECLARE @TempTime datetime
--如果开始时间大于结束时间，就调换过来
IF(@CreateTimeBegin<>'1900-1-1' AND @CreateTimeEnd<>'1900-1-1')
	BEGIN
	IF(@CreateTimeBegin>@CreateTimeEnd)
	BEGIN
		SET @TempTime = @CreateTimeBegin
		SET @CreateTimeBegin = @CreateTimeEnd
		SET @CreateTimeEnd = @TempTime
	END
END
--如果结束时间不为'1900-1-1'，把结束时间加 1 天
IF(@CreateTimeEnd<>'1900-1-1')
	SET @CreateTimeEnd = DateAdd(day,1,@CreateTimeEnd)
--如果开始时间大于结束时间，就调换过来
IF(@LastLoginTimeBegin<>'1900-1-1' AND @LastLoginTimeEnd<>'1900-1-1')
	BEGIN
	IF(@LastLoginTimeBegin>@LastLoginTimeEnd)
	BEGIN
		SET @TempTime = @LastLoginTimeBegin
		SET @LastLoginTimeBegin = @LastLoginTimeEnd
		SET @LastLoginTimeEnd = @TempTime
	END
END
--如果结束时间不为'1900-1-1'，把结束时间加 1 天
IF(@LastLoginTimeEnd<>'1900-1-1')
	SET @LastLoginTimeEnd = DateAdd(day,1,@LastLoginTimeEnd)



DECLARE @Sql NVARCHAR(2000)
DECLARE @SqlCount NVARCHAR(2000)
DECLARE @WhereClause NVARCHAR(2000)
SET @Sql = 'SELECT UserID,UserName,EMail,Tele,TrueName,Visitation,LastLoginTime,CreateTime,UpdateTime FROM [dbo].[UserMst] '
SET @SqlCount = 'SELECT count(*) FROM [dbo].[UserMst] '
SET @WhereClause = 'WHERE SubSystemID=''' + @SubSystemID + ''' AND '
IF RTrim(@UserName)<>''
	SET @WhereClause = @WhereClause + ' UserName=''' + @UserName + ''' AND '
IF RTrim(@EMail)<>''
	SET @WhereClause = @WhereClause + ' EMail=''' + @EMail + ''' AND '
IF RTrim(@CreateTimeBegin)<>Cast('1900-1-1' AS DateTime)
	SET @WhereClause = @WhereClause + ' CreateTime>=''' + Cast(@CreateTimeBegin AS VARCHAR(30)) + ''' AND '
IF RTrim(@CreateTimeEnd)<>Cast('1900-1-1' AS DateTime)
	SET @WhereClause = @WhereClause + ' CreateTime<=''' + Cast(@CreateTimeEnd AS VARCHAR(30)) + ''' AND '
IF RTrim(@LastLoginTimeBegin)<>Cast('1900-1-1' AS DateTime)
	SET @WhereClause = @WhereClause + ' LastLoginTime>=''' + Cast(@LastLoginTimeBegin AS VARCHAR(30)) + ''' AND '
IF RTrim(@LastLoginTimeEnd)<>Cast('1900-1-1' AS DateTime)
	SET @WhereClause = @WhereClause + ' LastLoginTime>=''' + Cast(@LastLoginTimeEnd AS VARCHAR(30)) + ''' AND '

SET @WhereClause = @WhereClause + '1=1'

SET @Sql = @Sql + @WhereClause
if(@SortBy<>'')
	SET @Sql = @Sql + ' ORDER BY ' + @SortBy
SET @SqlCount = @SqlCount + @WhereClause

CREATE TABLE #RecordCount(RecordCount int)
INSERT INTO #RecordCount EXEC sp_executesql @SqlCount
SELECT TOP 1 @TotalRecord=RecordCount FROM #RecordCount
SET @TotalPage=CEILING(CAST(@TotalRecord AS DECIMAL)/@PageSize)

IF @CurrentPage > @TotalPage-1
	SET @CurrentPage=@TotalPage-1
IF @CurrentPage < 0
	SET @CurrentPage=0

DECLARE @_Start INT, @_End INT
SET @_Start = ((@CurrentPage * @PageSize) + 1)
SET @_End = (@_Start + @PageSize - 1)

print @SQL
CREATE TABLE #TempTable
(
	RowID INT IDENTITY(1,1) PRIMARY KEY,UserID int,UserName varchar(30),EMail varchar(100),Tele varchar(50),TrueName varchar(50),Visitation int,LastLoginTime datetime,CreateTime datetime,UpdateTime datetime)
INSERT INTO #TempTable EXEC sp_executesql @SQL

SELECT *
	FROM #TempTable
WHERE (RowID >= @_Start) AND (RowID <= @_End)
