我低于 SP 并且花费了太多时间,最后停止了
EXEC MemberListing 1,20,'','','RegDate','Desc',0
如果将任何值传递给@IP
参数,它会起作用。不知道为什么。
Create PROCEDURE MemberListing
@PageNum as Int,
@PerPageResult as Int,
@Username as nvarchar(50),
@IP as varchar(50),
@sortColumn as Varchar(50),
@sortOrder as Varchar(4),
@TotalCount as int OUTPUT
AS
BEGIN
declare @Temp Table(RowNum int, id bigint, Username nvarchar(50), Email nvarchar(50), RegDate DateTime, Country varchar(25),
LastLogin DateTime, IsGoldMember varchar(1))
declare @sort varchar(50)
if @IP = ''
BEGIN
print 'if'
SET FMTONLY OFF;
Insert into @Temp
Select ROW_NUMBER() over (order by
case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'id' then id end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Username' then Username end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Email' then Email end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Country' then Country end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end ASC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'id' then id end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Username' then Username end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Email' then Email end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Country' then Country end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end DESC
) As RowNum, * From (
SELECT
m.id,m.login as Username,m.email as Email,m.registrationdate as RegDate,c.name as Country,m.lastlogindate as LastLogin,
CASE WHEN (r.description='goldmember' or r.description='goldmember_forever') then 1 end As IsGoldMember
from member m
join country c on m.country_id = c.id
join user_role ur on m.id=ur.member_id
join role r on r.id=ur.role_id
left join lastip l on m.id=l.user_id
where r.description <> 'administrator' and m.login like @Username+'%'
) as aab
Select * from @Temp Where RowNum> @PageNum-1 and RowNum<@PerPageResult+@PageNum
order by
case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'id' then id end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Username' then Username end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Email' then Email end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Country' then Country end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end ASC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'id' then id end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Username' then Username end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Email' then Email end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Country' then Country end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end DESC
END
ELSE
BEGIN
print 'else'
SET FMTONLY OFF;
Insert into @Temp
Select ROW_NUMBER() over (order by
case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'id' then id end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Username' then Username end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Email' then Email end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Country' then Country end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end ASC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'id' then id end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Username' then Username end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Email' then Email end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Country' then Country end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end DESC
) As RowNum, * From (
SELECT
m.id,m.login as Username,m.email as Email,m.registrationdate as RegDate,c.name as Country,m.lastlogindate as LastLogin,
CASE WHEN (r.description='goldmember' or r.description='goldmember_forever') then 1 end As IsGoldMember
from member m
join country c on m.country_id = c.id
join user_role ur on m.id=ur.member_id
join role r on r.id=ur.role_id
left join lastip l on m.id=l.user_id
where r.description <> 'administrator' and m.login like @Username+'%' and l.address = @IP
) as aa
Select * from @Temp Where RowNum> @PageNum-1 and RowNum<@PerPageResult+@PageNum
order by
case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'id' then id end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Username' then Username end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Email' then Email end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'Country' then Country end ASC
, case when @sortOrder <> 'ASC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end ASC
, case when @sortOrder <> 'ASC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end ASC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'id' then id end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Username' then Username end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Email' then Email end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'RegDate' then RegDate end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'Country' then Country end DESC
, case when @sortOrder <> 'DESC' then cast(null as datetime) when @sortColumn = 'LastLogin' then LastLogin end DESC
, case when @sortOrder <> 'DESC' then 0 when @sortColumn = 'IsGoldMember' then IsGoldMember end DESC
END
END
如果我为@IP 参数传递'',我会得到以下错误。
if
Msg 8152, Level 16, State 2, Procedure MemberListing, Line 24
String or binary data would be truncated.
The statement has been terminated.
(0 row(s) affected)