USE [AAA]
GO
/****** Object: StoredProcedure [dbo].[RPT_FK_Visitors] Script Date: 07/22/2016 14:12:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[BBB]
@pIndex int --页号
,@pPage int --页行数
,@dateTimeStart varchar(20)--开始时间
,@dateTimeEnd varchar(20)--结束时间
,@visitorName varchar(100)
,@no varchar(30)
,@vPhone varchar(20)
as
begin
SELECT * INTO #tmp
FROM(
SELECT '0' AS ID,'正在' AS Name
UNION
SELECT '1' AS ID,'离开' AS Name
UNION
SELECT '2' AS ID,'预约' AS Name
UNION
SELECT '3' AS ID,'取消预约' AS Name
UNION
SELECT '4' AS ID,'其他' AS Name) A
declare @userName varchar(100)
set @userName='%'+ @visitorName +'%'
declare @credentialsNo varchar(30)
set @credentialsNo='%'+ @no +'%'
declare @vistorPhone varchar(20)
set @vistorPhone='%'+ @vPhone +'%'
--记录总数
SELECT intCount=Count(*) from T_FK_Visitor where VisitTime between
@dateTimeStart and @dateTimeEnd
and Name like @userName
and CredentialsNo like @credentialsNo
and Phone like @vistorPhone
if(@pPage=0)
begin
select v.VisitorNumber,
v.Name,
v.Sex,
v.CredentialsType,
v.CredentialsNo,
v.Phone,
v.Company,
v.Address,
v.VCardID,
v.VCause,
v.Belongings,
v.CarInfo,
v.VisitorSum,
CONVERT(varchar(100),VisitTime,120)as VisitTime,
CONVERT(varchar(100),LeaveTime,120)as LeaveTime,
t.Name as VisitStateID
from T_FK_Visitor v,#tmp t
where v.VisitTime between @dateTimeStart and @dateTimeEnd
and v.Name like @userName
and v.CredentialsNo like @credentialsNo
and Phone like @vistorPhone
and t.ID = v.VisitStateID
end
else
begin
declare @MID int
set @MID=(@pIndex-1)*@pPage
;with T as (select row_number() over(order by VisitTime) as VisitorID,
v.VisitorNumber,
v.Name,
v.Sex,
v.CredentialsType,
v.CredentialsNo,
v.Phone,
v.Company,
v.Address,
v.VCardID,
v.VCause,
v.Belongings,
v.CarInfo,
v.VisitorSum,
v.VisitTime,
v.LeaveTime,
v.VisitStateID
from T_FK_Visitor v
where v.VisitTime between @dateTimeStart and @dateTimeEnd
and v.Name like @userName
and v.CredentialsNo like @credentialsNo
and Phone like @vistorPhone)
select Top(@pPage)VisitorNumber,t.Name,t.Sex,t.CredentialsType,t.CredentialsNo,t.Phone,t.Company,t.Address
,t.VCardID,t.VCause,t.Belongings,t.CarInfo,t.VisitorSum
,CONVERT(varchar(100),t.VisitTime,120)as VisitTime
,CONVERT(varchar(100),t.LeaveTime,120)as LeaveTime
,tmp.Name as VisitStateID
from T t,#tmp tmp
where VisitorID> @MID
and t.VisitStateID = tmp.ID
end
end
GO