SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SP with paging and individual record select
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dhinasql
Posting Yak Master

177 Posts

Posted - 07/19/2012 :  23:39:41  Show Profile  Reply with Quote
Team,

I am trying to use the below SP for two different purpose,

1. Fetch all the records without passing @FileID, Used to bind all the records in Grid and used for the custom paging. ( This is working fine as per expected )

2. Pass @FileID to get the individual record, this is working fine if i give @fineId below 12 but not working if i pass the @fileId more than that value

Find my Sp below

-- SAMPLE DATA
/*
Declare @Total INT

EXEC SP_Files_Select
@FileID = 32,
@StatusID = null,
@StartIndex = 1,
@PageSize = 10,
@TotalCount = @Total OUTPUT

SELECT @Total as TotalCount


*/

ALTER PROCEDURE SP_Files_Select
(
@FileID INT = NULL,
@StatusID INT = NULL,
@StartIndex INT,
@PageSize INT,
@TotalCount INT = NULL OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;

DECLARE @EndIndex INT

IF @StartIndex <= 1
BEGIN
SET @StartIndex = 0
END
ELSE
BEGIN
SET @StartIndex = (@StartIndex - 1) * @PageSize
END

SET @EndIndex = @StartIndex + @PageSize

SELECT ROW_NUMBER() OVER (ORDER BY [FileID]) + @StartIndex AS SNO
,[FileID]
,[FileLocation]

FROM
(SELECT ROW_NUMBER() OVER (ORDER BY [FileID]) + @StartIndex AS Row,
,[FileID]
,[FileLocation]
FROM [Input_Files]) AS RecordRows
WHERE Row > @StartIndex AND
Row <= @EndIndex AND
(@FileID IS NULL OR FileID = @FileID) AND
(@StatusID IS NULL OR StatusID = @StatusID)

SELECT @TotalCount = COUNT (1)
FROM [Input_Files]
WHERE (@FileID IS NULL OR FileID = @FileID) AND
(@StatusID IS NULL OR StatusID = @StatusID)

END



It will be much appreciated if you help to get the expected result and fine tune.

Thanks.

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 07/20/2012 :  00:06:17  Show Profile  Reply with Quote
first check in nput_Files if you've records with FileID > 12

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dhinasql
Posting Yak Master

177 Posts

Posted - 07/20/2012 :  00:18:12  Show Profile  Reply with Quote
Thanks for your reply

SELECT * FROM [Input_Files] WHERE FileID > 12

Returns more than 100 records.

Something strange in my SP, It need to be fixed, please provide suggestion
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 07/20/2012 :  00:31:26  Show Profile  Reply with Quote
ok i got it. you're doing the filtering only after paging. it should be other way around

see


ALTER PROCEDURE SP_Files_Select 
( 
@FileID INT = NULL, 
@StatusID INT = NULL, 
@StartIndex INT, 
@PageSize INT, 
@TotalCount INT = NULL OUTPUT 
) 
AS 
BEGIN 
SET NOCOUNT ON; 

DECLARE @EndIndex INT 

IF @StartIndex <= 1 
BEGIN 
SET @StartIndex = 0 
END 
ELSE 
BEGIN 
SET @StartIndex = (@StartIndex - 1) * @PageSize 
END 

SET @EndIndex = @StartIndex + @PageSize 

SELECT ROW_NUMBER() OVER (ORDER BY [FileID]) + @StartIndex AS SNO 
,[FileID] 
,[FileLocation] 

FROM 
(SELECT ROW_NUMBER() OVER (ORDER BY [FileID]) + @StartIndex AS Row, 
,[FileID] 
,[FileLocation] 
FROM [Input_Files]
WHERE (@FileID IS NULL OR FileID = @FileID) AND 
(@StatusID IS NULL OR StatusID = @StatusID)) AS RecordRows 
WHERE Row > @StartIndex AND 
Row <= @EndIndex 
 

SELECT @TotalCount = COUNT (1) 
FROM [Input_Files] 
WHERE (@FileID IS NULL OR FileID = @FileID) AND 
(@StatusID IS NULL OR StatusID = @StatusID) 

END 




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 07/20/2012 00:39:56
Go to Top of Page

dhinasql
Posting Yak Master

177 Posts

Posted - 07/20/2012 :  00:51:17  Show Profile  Reply with Quote
Thanks visakh, its working great :-)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 07/20/2012 :  00:55:11  Show Profile  Reply with Quote
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000