USE [Soking]
GO
/****** Object:  StoredProcedure [dbo].[Proc_TravelNote_NetPager]    Script Date: 09/18/2013 21:13:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Proc_TravelNote_NetPager] 
@SpotId  Int,
@startIndex Int,
@endIndex Int
AS
BEGIN
BEGIN TRY
BEGIN
create table #temptbl(RowNumber int not null,SpotId int not null)
insert into #temptbl(RowNumber,SpotId) select ROW_NUMBER() OVER (ORDER BY TB_Travel.AddDate desc),SpotId from TB_Travel where TB_Travel.SpotId = @SpotId
--select * from #temptbl
--select a.TnId,a.SpotId,a.TnName,a.TnContent,a.ThumbNailSrc,a.AdderId,a.AddDate,b.SpotName from TB_Travel as a  inner join TB_Spot as b on a.SpotId = b.SpotId
--select a.TnId,a.SpotId,a.TnName,a.TnContent,a.ThumbNailSrc,a.AdderId,a.AddDate,b.SpotName from(TB_Travel as a  inner join TB_Spot as b  on a.SpotId = b.SpotId)
select c.RowNumber, a.TnId,a.SpotId,a.TnName,a.TnContent,a.ThumbNailSrc,a.AdderId,a.AddDate,b.SpotName from TB_Travel as a  inner join TB_Spot as b  on a.SpotId = b.SpotId inner join #temptbl as c on a.SpotId = c.SpotId where RowNumber between @startIndex and @endIndex
END
END TRY
BEGIN CATCH
Insert into TB_Error(ErrorMessage,AddDate,ErrorLocation) values ( ERROR_MESSAGE(),GETDATE(),'[Proc_TravelNote_NetPager]')
END CATCH  
END

上述存储过程数据重复一次


USE [Soking]
GO
/****** Object: StoredProcedure [dbo].[Proc_TravelNote_NetPager] Script Date: 09/18/2013 21:13:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Proc_TravelNote_NetPager]
@SpotId Int,
@startIndex Int,
@endIndex Int
AS
BEGIN
BEGIN TRY
BEGIN
create table #temptbl(RowNumber int not null,TnId int not null)
insert into #temptbl(RowNumber,TnId) select ROW_NUMBER() OVER (ORDER BY TB_Travel.AddDate desc),TnId from TB_Travel where TB_Travel.SpotId = @SpotId
select a.TnId,a.SpotId,a.TnName,a.TnContent,a.ThumbNailSrc,a.AdderId,a.AddDate,b.SpotName from TB_Travel as a
inner join TB_Spot as b
on a.SpotId = b.SpotId
inner join #temptbl as c
on a.TnId= c.TnId where RowNumber between @startIndex and @endIndex
END
END TRY
BEGIN CATCH
Insert into TB_Error(ErrorMessage,AddDate,ErrorLocation) values ( ERROR_MESSAGE(),GETDATE(),'[Proc_TravelNote_NetPager]')
END CATCH 
END

这样修改就应该没错误了。

发生错误的原因是,创建的临时表的SpotId字段作为内联条件,导致#temptbl的每一行数据都能匹配到(@endIndex-@startIndex)个数据,导致有数据重复