1 use [aps_future_ft] 2 GO 3/****** object:storedprocedure [dbo]. [A_crudeprice] Script DATE:2013/11/5 19:13:21 ******/4 SET ANSI_NULLS on 5 go 6 SET QUOTED_IDENTIFIER on 7 Go 8--==================== ========================= 9--Author: <author,,name>10--Create Date: <create date,,>11--Descriptio N: <description,,>12--=============================================13 ALTER PROCEDURE [dbo]. [A_crudeprice]14 (@BeginDate DATETIME, @EndDate datetime,17 @PageSize int,18 @PageIndex INT, @TotalCount INT OUTPUT20) AS22 BEGIN23--SET NOCOUNT on added to prevent extra result sets From24-- Interfering with SELECT statements.25 SET NOCOUNT on;26 DECLARE @CodeList NVARCHAR (max) DECLARE @str NVAR CHAR (max) SET @CodeList = (SELECT DISTINCT ' [' + ISCP. Crudestandard+ '], ' from dbo. T_h_internalstandardcrudeprice iscp31 for XML PATH (") 32 ) SET @CodeList = SUBSTRING (@CodeList, 1,len (@CodeList)-1) PRINT @CodeList35 Iscp. PRICEDATE,ISCP. CRUDESTANDARD,ISCP. CrudeStandardPrice37 to #A38 from dbo. T_h_internalstandardcrudeprice iscp39 WHERE ISCP. Pricedate between @BeginDate and @EndDate40 SET @str = ' Row=row_number () over (order by pricedate Desc) , Pricedate, ' [email protected]+ ' from (#A46) as A47 PIVOT (SUM (A.crud Estandardprice) for A.crudestandard in (' [email protected]+ ') "as B '," SET @str = ' SELECT * into # #A50 From (' [email protected]+ ') as C ' (@str), SELECT * from # #A as tempA56 WHE RE Tempa.row between @PageSize * @PageIndex and @PageSize * (@PageIndex + 1) * * * @TotalCount = (select COUNT (*) From # #A60) drop table #A62 drop table # #A63
T-SQL row to column, database query paging