anorthcote
Programmer
Hi,
I hope someone can help with a query on a stored procedure.
Records being displayed in our application are split into pages of x amount of records. x being a value (default of 30) that is user specified.
I have a stored procedure that takes the @NumPerPage from the user and divided the count of records by that number to provide a count of pages.
My problem is that the return value is always an integer and, even with using CEILING the number is rounding down and when dividing records into pages of 30 this number should be rounded up. I have tried changing @Calls and @NumPerPage to REAL but that doesn't work either, it still returns an integer.
The only fix I have found is very clunky and, although it works, I don't like it!
Can anyone suggest a better way of writing the procedure?
I hope someone can help with a query on a stored procedure.
Records being displayed in our application are split into pages of x amount of records. x being a value (default of 30) that is user specified.
I have a stored procedure that takes the @NumPerPage from the user and divided the count of records by that number to provide a count of pages.
Code:
CREATE PROCEDURE dbo.CallPages
(
@LocationID int,
@StatusID int,
@Area int,
@Contractor int,
@NumPerPage int
)
AS
BEGIN
DECLARE @Calls int
select @Calls = COUNT(dbo.Calls.LogID)
FROM dbo.Location LEFT OUTER JOIN
dbo.Area ON dbo.Location.AreaID = dbo.Area.ID RIGHT OUTER JOIN
dbo.Calls ON dbo.Location.ID = dbo.Calls.LocationID LEFT OUTER JOIN
dbo.ContractorOrder INNER JOIN
dbo.Contractor ON dbo.ContractorOrder.ContractorID = dbo.Contractor.ID ON dbo.Calls.OrderID = dbo.ContractorOrder.OrderID
where (@LocationID = 0 OR LocationID = @LocationID) AND
(@StatusID = 0 OR StatusID = @StatusID) AND
(@area = 0 OR Area.ID = @area) AND
(@Contractor = 0 OR dbo.Contractor.ID = @Contractor)
RETURN CEILING(@Calls / @NumPerPage)
END
GO
My problem is that the return value is always an integer and, even with using CEILING the number is rounding down and when dividing records into pages of 30 this number should be rounded up. I have tried changing @Calls and @NumPerPage to REAL but that doesn't work either, it still returns an integer.
The only fix I have found is very clunky and, although it works, I don't like it!
Code:
CREATE PROCEDURE dbo.CallPages
(
@LocationID int,
@StatusID int,
@Area int,
@Contractor int,
@NumPerPage int
)
AS
BEGIN
DECLARE @Calls int
DECLARE @Pages int
DECLARE @CallTest int
DECLARE @Diff int
select @Calls = COUNT(dbo.Calls.LogID)
FROM dbo.Location LEFT OUTER JOIN
dbo.Area ON dbo.Location.AreaID = dbo.Area.ID RIGHT OUTER JOIN
dbo.Calls ON dbo.Location.ID = dbo.Calls.LocationID LEFT OUTER JOIN
dbo.ContractorOrder INNER JOIN
dbo.Contractor ON dbo.ContractorOrder.ContractorID = dbo.Contractor.ID ON dbo.Calls.OrderID = dbo.ContractorOrder.OrderID
where (@LocationID = 0 OR LocationID = @LocationID) AND
(@StatusID = 0 OR StatusID = @StatusID) AND
(@area = 0 OR Area.ID = @area) AND
(@Contractor = 0 OR dbo.Contractor.ID = @Contractor)
set @Pages = round (@Calls / @NumPerPage,2)
set @CallTest = (@Pages * @NumPerPage)
set @Diff = (@Calls - @CallTest)
if @Diff > 0
BEGIN
set @Pages = (@Pages + 1)
END
RETURN @Pages
END
GO
Can anyone suggest a better way of writing the procedure?