I am using sql server 2008 in production. Below is my stored procedure which is working fine on SQL Server 2012 but gives a fetch error in SQL Server 2008. The reason is fetch is not built in SQL Server 2008 but it is available in SQL Server 2012. Help me with converting this script to work with SQL Server 2008.
Create PROCEDURE sp_LoadSponsorsListofDonorsforReminder
@pageSize int,
@Offset int,
@TotalRecords int out
as
BEGIN
SELECT max(cd.OID) as OID, max(cd.DonationId) as DonationId,
max(cd.DonorId) as DonorId,
max(Concat(do.FIRSTNAME,' ', do.LASTNAME)) as Donor_Name,
max(do.PHONENUMBER) as PHONENUMBER,
max(do.MOBILENUMBER) as MOBILENUMBER, max(do.ADDRESS) as ADDRESS,
max(do.EMAIL) as EMAIL, max(cd.PaidUpTo) as PaidUpTo,
max(cd.StartDate) as StartDate, max(ca.ChildCode) as ChildCode,
max(concat (ca.FirstName,' ', ca.LastName)) as Child_Name,
max(org.ORGANIZATION_NAME) as Village,
max(d.DonationDate) as DonationDate,
max(r.ReminderOneDate) as ReminderOneDate
FROM child_sponsorship cd
inner join donations d
ON cd.DonationId = d.OID
inner JOIN donor do
ON cd.DonorId = do.OID
inner join child_admission ca
ON cd.ChildId = ca.OID
inner join organization org
ON do.ORGANIZATION = org.OID
left join Reminder_Information r
ON cd.DonorId = r.DonorId
WHERE d.DonationDate < DATEADD(day, -365, GETDATE()) AND
cd.DonorId <> 1174 AND
cd.DonorId <> 1175
GROUP by cd.childId
ORDER By Max(d.DonationDate), max(cd.DonorId) desc
OFFSET @Offset ROWS
FETCH NEXT @pageSize ROWS ONLY
SET @TotalRecords = (select count(*) from child_sponsorship WHERE 1=1);
END;
You can generate a ROW_NUMBER on the fly for your query. I'm not going to reproduce the entire thing here, but here is a simple example that uses the Products table from the Northwind sample database
DECLARE @pageNumber INT = 2, @pageSize INT = 6
SELECT ProductID,ProductName, UnitPrice
FROM
(
SELECT ProductID,ProductName, UnitPrice,
ROW_NUMBER() OVER (ORDER BY ProductID) AS rn
FROM Products
) AS P
WHERE P.rn BETWEEN ((@pageNumber-1)*@pageSize)+1 AND @pageSize*(@PageNumber)
The inner query produces the row number, the outer query gets the fields you want (i.e. without the row number). The inner query is where you put all the stuff you have above The outer query is where you put your WHERE clause using AND to limit the value of P.rn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With