3

我正在使用sql server 2008進行生產。下面是我在SQL Server 2012上正常工作的存儲過程,但在SQL Server 2008中給出了一個提取錯誤。原因是fetch不是在SQL Server 2008中構建的,而是在SQL Server 2012中可用。幫助我將此腳本轉換爲使用SQL Server 2008.如何將此過程從SQL Server 2012更改爲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; 
+0

'CONCAT(一, '',b)== A + '' + B' – Devart

+1

也取SQL Server 2008中不工作 – Ammar

+0

您需要填充一個臨時表與IDENTITY_column和使用SELECT的組合TOP [@pageSize] ..並使用IDENTITY_column> [@Offset]。在WHERE子句 –

回答

1

您可以爲查詢動態生成ROW_NUMBER。我不打算在這裏重現了整個事情,但這裏是一個使用Products表從Northwind示例數據庫

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) 

內查詢產生的行數個簡單的例子,外部查詢得到的字段,你想要(即沒有行號)。內部查詢是你把所有你有以上 的東西,外部查詢是你的WHERE子句使用並限制P.rn

0

的價值運用AdventureWorksLT2008R2 Product表DB作爲一個例子把你

DECLARE @pageSize int = 10, 
     @Offset int = 20 

IF OBJECT_ID('tempdb..#xRecords') IS NOT NULL 
    DROP TABLE #xRecords 
CREATE TABLE #xRecords(
    ID    INT IDENTITY(1,1) CONSTRAINT PK_xRecords PRIMARY KEY 
    ,ProductID  INT 
    ,Name   NVARCHAR(50) 
    ,ProductNumber NVARCHAR(25) 
) 

INSERT INTO #xRecords (ProductID, Name, ProductNumber) 
SELECT 
    ProductID 
    ,Name 
    ,ProductNumber 
FROM 
    [AdventureWorksLT2008R2].[SalesLT].[Product] 

SELECT 
    TOP (@pageSize) * 
FROM 
    #xRecords 
WHERE 
    ID > @Offset 
相關問題