Hi all,
I am taking an old SPROC and trying to make it work with a new system.
The SPROC and the old system work in not an efficient way as it grabs all the records so I want to page.
I have done paging before but not quite like this and need some help
Here is the original SPROC and I want to keep it like this:
SELECT (SELECT SUM(costPerUnit * quantity)
FROM travelExpense
WHERE (fkTravelID = tr.[id])) AS total,
(SELECT SUM(costPerUnit * quantity)
FROM travelExpense
WHERE (fkTravelID = tr.[id]) AND (NOT LEFT(expenseType,7) = 'ON P.O.') ) AS totalExpensed,
(SELECT SUM(amount)
FROM travelBudgetCode
WHERE (fkRequestID = tr.[id])) AS totalBudgeted,
tr.[id],
tr.dateCreated,
tr.fkEmployeeID,
tr.fkPreparerID,
tr.currentPosition,
tr.currentLocation,
tr.destination,
tr.[description],
tr.numStudents,
tr.dateLeaving,
tr.dateReturning,
tr.numDaysAbsent,
tr.numSubDays,
tr.numTotal,
tr.notes,
tr.empApproval,
tr.empApprovalTimeStamp,
tr.fkCurrentPrincipal,
tr.pplApproval,
tr.pplApprovalTimeStamp,
tr.fkCurrentExecutive,
tr.execApproval,
tr.execApprovalTimeStamp,
tr.proofRead,
tr.proofReadTimeStamp,
tr.encumbered,
tr.encumberedTimeStamp,
tr.empFinalApproval,
tr.empFinalApprovalTimeStamp,
tr.accounted,
tr.accountedTimeStamp,
tr.denied,
tr.deniedTimeStamp,
tr.cancelled,
tr.cancelledTimeStamp,
tr.advance,
tr.advanceAmount,
tr.advancePrint,
emp.efirst,
emp.elast,
emp2.efirst AS principalFirst,
emp2.elast AS principalLast,
emp1.efirst AS executiveFirst,
emp1.elast AS executiveLast,
emp3.efirst AS preparerFirst,
emp3.elast AS preparerLast
FROM dbo.travelRequest tr
INNER JOIN dbo.staffEmployee emp
ON tr.fkEmployeeID = emp.eid
LEFT OUTER JOIN dbo.staffEmployee emp1
ON tr.fkCurrentExecutive = emp1.eid
LEFT OUTER JOIN dbo.staffEmployee emp2
ON tr.fkCurrentPrincipal = emp2.eid
LEFT OUTER JOIN dbo.staffEmployee emp3
ON tr.fkPreparerID = emp3.eid
ORDER BY tr.dateCreated DESC
Now I want to be able to give it the parameters:
- PageNumber
- ItemsPerPage
- SortOnColumn
- SortDirection
- ExactMatch (bit)
- SearchText
adding params is easy but to leverage it is difficult. I want to ideally NOT use dynamic SQL here.
I want the ability to be able to sort based on a given column name (and direction to sort in) and optionally the text they want to search on (with a LIKE or exact match based on the bit value)
And of course, paging the results based on the page number provided and records per page.
I am not quite sure the syntax and hoping someone can be as kind to help here.
using SQL Server 2012 I believe. I am just taking the existing SPROC and altering it to use in the new system as is with the changes.
Thank you!