Exec into temp table without columns as they are dynamic - sql-server

I'm working on pivot functionality, where the pivot columns comes dynamically. All the pivot columns I needs to count and display separate column in pivot select statement itself.
SELECT CurriculumSegment,attendancedate,section,Class,count(*) as
TotalWorkingDays into #NewTempWithTotalDaysCount
FROM #temp1 group by CurriculumSegment,attendancedate,Section,Class
order by CurriculumSegment, attendancedate,Class,Section
SELECT CurriculumSegment,attendancedate,section,Class,count(*) as
CountOfPresentDays into #NewTempWithTotalPresentDaysCount
FROM #temp1 where attendance='P' group by
CurriculumSegment,attendancedate,Section,Class order by CurriculumSegment,
attendancedate,Class,Section
select a.AttendanceDate,a.Class,a.CurriculumSegment,
a.Section,a.TotalWorkingDays,b.CountOfPresentDays,(a.TotalWorkingDays -
b.CountOfPresentDays) as TotalAbsentDays, (b.CountOfPresentDays *
100)/a.TotalWorkingDays as PercentOfPresentDays
into #TempWithTotalDaysCount
from #NewTempWithTotalDaysCount a left outer join
#NewTempWithTotalPresentDaysCount b
on a.AttendanceDate =b.AttendanceDate and a.CurriculumSegment =
b.CurriculumSegment and a.Section= b.Section and a.Class = b.Class
-- Query 1
select sum(TotalWorkingDays) as TotalCount,sum(TotalAbsentDays) as
AbsentCount,sum(CountOfPresentDays) as
PresentCount,curriculumsegment,section,class
from #TempWithTotalDaysCount group by class,curriculumsegment,section
SET #query = 'SELECT [CurriculumSegment],Class,section,'+#cols+'
FROM (
SELECT [CurriculumSegment],
Class,section,PercentOfPresentDays,AttendanceDate FROM
#TempWithTotalDaysCount) AS up
PIVOT (MAX(PercentOfPresentDays) FOR AttendanceDate IN ('+#cols+')) AS pvt'
-- Query 2
EXEC (#query)
From the query 1 and query 2 results, I needs to do join and get the TotalCount, AbsentCount, PresentCount
in the result of query 2.
Is it possible to put "EXEC(#query)" in temp table ?
I tried but I'm getting invalid object. Because I cant give the column names as they come dynamically form pivot.
So,is there any other approach ?
Thank you.
Find out my result in below image:

Related

Return specific number of rows in result set from Stored Procedure

When we make a stored procedure call we pass input parameter of how many rows we want to get from result. Also, we want specific columns returned which is obtained through join operation on tables.
My doubt is can we return the result as table but if in that approach how to limit result rows to specific count which is passed as input parameter.
I also searched and found about using Fetch next rows only but can we use that without offset logic.
Can somebody suggest me if there is any better approach than above mentioned?
Here is an example of how you could use TOP.
create or alter procedure TopTest
(
#RowCount int
) as
select top (#RowCount) *
from sys.columns c
order by c.name
And here is how you could do this using OFFSET/FETCH
create or alter procedure TopTestOffset
(
#RowCount int
) as
select *
from sys.columns c
order by c.name
offset 0 rows
fetch first (#RowCount) rows only
TOP and OFFSET are easier to use if you need to get first n rows. If you need a range of rows (i.e. for paging), you can use CTE
with vw as (
SELECT ROW_NUMBER() OVER (ORDER BY column1) AS RowNumber,
columnlist
from YourTable
) select * from vw
where RowNumber between 1 and #NumberOfRows

How to suppress displaying specific columns on dynamic tables

I have two dynamic tables that I am left joining. I want to suppress displaying specific column from both tables. So far, this is my query:
SELECT a.*
,b.*
FROM APEXCOUNTYREVIEW_REPORT a
LEFT JOIN TBL_LOW_VOLUME_test b
ON a.fips_data_type = b.fips_datatype
Both of these are dynamic tables with plenty of columns. I want to suppress columns in the middle of the tables.
For example, Table1 has columns C1, C2, C3, C4, C5, C6, C7,...etc. I do not want to pull columns C3, C4 and C5, but want to display C1, C2, C6, C7 and so forth. Since both of these are dynamic tables, the number of columns is not known until execution of the query. I'm using SSMS 2008 R2.
Is there a way to do this with SQL query?
Here's the query to build table2, tbl_low_voulume_test:
I'm also trying to add a text to the column name on table2, since they are the same as the column names on table1, that is being created dynamically and I'm getting all kinds of invalid error messages. How can I add the text "Flag" on the dynamic column names?
The query below runs fine.
DECLARE #T AS TABLE(Y INT NOT NULL PRIMARY KEY);
DECLARE
#COLS AS NVARCHAR(MAX),
#COLNAMES AS NVARCHAR(MAX),
#Y AS INT,
#SQL AS NVARCHAR(MAX);
SET #COLS = STUFF(
(SELECT N',' + QUOTENAME(Y)
FROM (SELECT DISTINCT RECMONTH AS Y FROM REPORTING.dbo.TBL_LOW_VOLUME A
WHERE (RECMONTH>= '2005-01')
AND RECMONTH <= CONVERT(VARCHAR(7), getdate(), 120)
) AS Y
ORDER BY Y
FOR XML PATH('')),
1, 1, N'');
SET #COLNAMES = STUFF(
(SELECT N',ISNULL(' + QUOTENAME(Y)+',0) AS '+ QUOTENAME(Y)
FROM (SELECT DISTINCT RECMONTH AS [Y] FROM REPORTING.dbo.TBL_LOW_VOLUME A
WHERE (RECMONTH>= '2005-01')
AND RECMONTH <= CONVERT(VARCHAR(7), getdate(), 120)
) AS Y
ORDER BY Y
FOR XML PATH('')),
1, 1, N'');
IF OBJECT_ID('TBL_LOW_VOLUME_test') IS NOT NULL
BEGIN DROP TABLE reporting.dbo.TBL_LOW_VOLUME_test END
SET #SQL = N'SELECT * INTO dbo.TBL_LOW_VOLUME_test
FROM (
SELECT CONVERT(DATE, GETDATE()) AS RUN_DATE, FIPS+DATATYPE AS FIPS_DATATYPE,
FIPS,DATATYPE,' +#COLNAMES + '
FROM (SELECT A.[FIPS], A.DATATYPE
,RECMONTH
,[LV_FLAG]
FROM dbo.TBL_LOW_VOLUME A
where FIPS is not NULL and isnumeric(FIPS) = 1 and len(ltrim(FIPS)) = 5
and FIPS not in (''-0000'',''00000'',''-0001'')
and FIPS <= ''56999''
) AS D
PIVOT(SUM([LV_FLAG]) FOR RECMONTH IN(' + #COLS + N')) AS P) AS Z';
EXEC SP_EXECUTESQL #SQL;
Here's a picture of my two tables.
Table1:
col1, col2, col3, col4, col5, col6, etc...
columns 1 thru 5 are static, starting on column c0l6, it becomes dynamic where every beginning of the month it adds a new month.
Table2:
cola, colb, colc, cold, cole, colf, etc...
columns a and b are both static, the rest are dynamic where every beginning of the month it adds a new month.
This should be the final results I want to display.
col1, col2, col6, etc...(all the dynamic columns on table1), cola, colc, cole, etc...(all the dynamic columns on table2)
So I want to pick and choose which columns I need from each table.
Here's the query after I did print #sql:
For this table, I do not want to display Run_Date and FIPS_DATATYPE, just all the date columns.
SELECT * INTO dbo.TBL_LOW_VOLUME_test
FROM (
SELECT CONVERT(DATE, GETDATE()) AS RUN_DATE, FIPS+DATATYPE AS FIPS_DATATYPE,
ISNULL([2005-01],0) AS [2005-01],ISNULL([2005-02],0) AS [2005-02],ISNULL([2005-03],0) AS [2005-03],ISNULL([2005-04],0) AS [2005-04],
ISNULL([2005-05],0) AS [2005-05],ISNULL([2005-06],0) AS [2005-06],ISNULL([2005-07],0) AS [2005-07],ISNULL([2005-08],0) AS [2005-08],
ISNULL([2005-09],0) AS [2005-09],ISNULL([2005-10],0) AS [2005-10],ISNULL([2005-11],0) AS [2005-11],ISNULL([2005-12],0) AS [2005-12],
ISNULL([2006-01],0) AS [2006-01],ISNULL([2006-02],0) AS [2006-02],ISNULL([2006-03],0) AS [2006-03],ISNULL([2006-04],0) AS [2006-04],
ISNULL([2006-05],0) AS [2006-05],ISNULL([2006-06],0) AS [2006-06],ISNULL([2006-07],0) AS [2006-07],ISNULL([2006-08],0) AS [2006-08],
ISNULL([2006-09],0) AS [2006-09],ISNULL([2006-10],0) AS [2006-10],ISNULL([2006-11],0) AS [2006-11],ISNULL([2006-12],0) AS [2006-12],
ISNULL([2007-01],0) AS [2007-01],ISNULL([2007-02],0) AS [2007-02],ISNULL([2007-03],0) AS [2007-03],ISNULL([2007-04],0) AS [2007-04],
ISNULL([2007-05],0) AS [2007-05],ISNULL([2007-06],0) AS [2007-06],ISNULL([2007-07],0) AS [2007-07],ISNULL([2007-08],0) AS [2007-08],
ISNULL([2007-09],0) AS [2007-09],ISNULL([2007-10],0) AS [2007-10],ISNULL([2007-11],0) AS [2007-11],ISNULL([2007-12],0) AS [2007-12],
ISNULL([2008-01],0) AS [2008-01],ISNULL([2008-02],0) AS [2008-02],ISNULL([2008-03],0) AS [2008-03],ISNULL([2008-04],0) AS [2008-04],
ISNULL([2008-05],0) AS [2008-05],ISNULL([2008-06],0) AS [2008-06],ISNULL([2008-07],0) AS [2008-07],ISNULL([2008-08],0) AS [2008-08],
ISNULL([2008-09],0) AS [2008-09],ISNULL([2008-10],0) AS [2008-10],ISNULL([2008-11],0) AS [2008-11],ISNULL([2008-12],0) AS [2008-12],
ISNULL([2009-01],0) AS [2009-01],ISNULL([2009-02],0) AS [2009-02],ISNULL([2009-03],0) AS [2009-03],ISNULL([2009-04],0) AS [2009-04],
ISNULL([2009-05],0) AS [2009-05],ISNULL([2009-06],0) AS [2009-06],ISNULL([2009-07],0) AS [2009-07],ISNULL([2009-08],0) AS [2009-08],
ISNULL([2009-09],0) AS [2009-09],ISNULL([2009-10],0) AS [2009-10],ISNULL([2009-11],0) AS [2009-11],ISNULL([2009-12],0) AS [2009-12],
ISNULL([2010-01],0) AS [2010-01],ISNULL([2010-02],0) AS [2010-02],ISNULL([2010-03],0) AS [2010-03],ISNULL([2010-04],0) AS [2010-04],
ISNULL([2010-05],0) AS [2010-05],ISNULL([2010-06],0) AS [2010-06],ISNULL([2010-07],0) AS [2010-07],ISNULL([2010-08],0) AS [2010-08],
ISNULL([2010-09],0) AS [2010-09],ISNULL([2010-10],0) AS [2010-10],ISNULL([2010-11],0) AS [2010-11],ISNULL([2010-12],0) AS [2010-12],
ISNULL([2011-01],0) AS [2011-01],ISNULL([2011-02],0) AS [2011-02],ISNULL([2011-03],0) AS [2011-03],ISNULL([2011-04],0) AS [2011-04],
ISNULL([2011-05],0) AS [2011-05],ISNULL([2011-06],0) AS [2011-06],ISNULL([2011-07],0) AS [2011-07],ISNULL([2011-08],0) AS [2011-08],
ISNULL([2011-09],0) AS [2011-09],ISNULL([2011-10],0) AS [2011-10],ISNULL([2011-11],0) AS [2011-11],ISNULL([2011-12],0) AS [2011-12],
ISNULL([2012-01],0) AS [2012-01],ISNULL([2012-02],0) AS [2012-02],ISNULL([2012-03],0) AS [2012-03],ISNULL([2012-04],0) AS [2012-04],
ISNULL([2012-05],0) AS [2012-05],ISNULL([2012-06],0) AS [2012-06],ISNULL([2012-07],0) AS [2012-07],ISNULL([2012-08],0) AS [2012-08],
ISNULL([2012-09],0) AS [2012-09],ISNULL([2012-10],0) AS [2012-10],ISNULL([2012-11],0) AS [2012-11],ISNULL([2012-12],0) AS [2012-12],
ISNULL([2013-01],0) AS [2013-01],ISNULL([2013-02],0) AS [2013-02],ISNULL([2013-03],0) AS [2013-03],ISNULL([2013-04],0) AS [2013-04],
ISNULL([2013-05],0) AS [2013-05],ISNULL([2013-06],0) AS [2013-06],ISNULL([2013-07],0) AS [2013-07],ISNULL([2013-08],0) AS [2013-08],
ISNULL([2013-09],0) AS [2013-09],ISNULL([2013-10],0) AS [2013-10],ISNULL([2013-11],0) AS [2013-11],ISNULL([2013-12],0) AS [2013-12],
ISNULL([2014-01],0) AS [2014-01],ISNULL([2014-02],0) AS [2014-02],ISNULL([2014-03],0) AS [2014-03],ISNULL([2014-04],0) AS [2014-04],
ISNULL([2014-05],0) AS [2014-05],ISNULL([2014-06],0) AS [2014-06],ISNULL([2014-07],0) AS [2014-07],ISNULL([2014-08],0) AS [2014-08],
ISNULL([2014-09],0) AS [2014-09],ISNULL([2014-10],0) AS [2014-10],ISNULL([2014-11],0) AS [2014-11],ISNULL([2014-12],0) AS [2014-12],
ISNULL([2015-01],0) AS [2015-01],ISNULL([2015-02],0) AS [2015-02]
How can I add the text "Flag" on the dynamic column names?
Looks to me like you want to replace
QUOTENAME(Y)
with
QUOTENAME(Y+'Flag')
Everywhere it occurs in both the SET #COLS and SET #COLNAMES statements.
EDIT: To filter out columns, looking at your final select, you're doing SELECT * FROM (a derived table that specifies static columns + COLNAMES).
So to filter out columns, filter them out of the derived table. They have to be either the static columns RUN_DATE, FIPS_DATATYPE, FIPS, DATATYPE, or one of the columns in COLNAMES, which you can filter out with a REPLACE function.
Or if I am not understanding the problem, please post sample output of your current query and which rows you want to filter out.
2ND EDIT: To take Run_Date and FIPS_DATATYPE out of your results, simply take them out of the middle-level select:
SET #SQL = N'SELECT * INTO dbo.TBL_LOW_VOLUME_test
FROM (
SELECT ' +#COLNAMES + '
FROM ...

Dynamic Pivot with varying columns

I have a POA Code dynamic pivot that pulls data from a DX temp table and inserts the data into a temp POA table.
The issue I'm having is that there is a possibility of up to 35 different columns that can be returned. Depending on the month there could be 15 columns (POA1...POA15) or there could be all 35 columns (POA1...POA35). I join this dynamic pivot temp table on another patient table. My problem is, I need to show all 35 columns even if some of the columns do not exist in the temp POA table.
--Pivot DX POA Codes
DECLARE #POANAME VARCHAR(40)
SELECT #POAName = '##tmpPOA'
DECLARE #colsPOA NVARCHAR(2000)
SELECT #colsPOA = STUFF((SELECT DISTINCT TOP 100 PERCENT
'],[' + 'POA' + CAST(Dx.RowNum AS NVARCHAR)
FROM #tmpDX DX
ORDER BY '],[' + 'POA' + CAST(Dx.RowNum AS NVARCHAR)
FOR XML PATH ('')
),1,2,'') + ']'
DECLARE #queryPOA NVARCHAR(4000)
SET #queryPOA = 'N
SELECT
EncObjID,
'+
#colsPOA
+' INTO ' + POAName + '
FROM
(SELECT
Dx.EncObjID
,''POA'' + Dx.RowNum AS RowNum
,Dx.POAMne
FROM #tmpDx Dx
) p
PIVOT
(
MIN([POAMne])
FOR RowNum IN
( ' + #colsPOA + ' )
) AS pvt'
EXECUTE(#queryPOA)
I'm receiving an Invalid Column Name in my patient query because some of the columns don't exist in ##tmpPOA. I thought about creating a temp table called #tmpDxPOA and doing an insert (Insert Into #tmpDxPOA select * from ##tmpPOA), but that doesn't work (I receive a Column Name or number of supplied values does not match error).
Any thoughts on how to create all 35 columns even if there isn't any data? I don't care if they're null, I just need to have those place holders in the main patient query and it doesn't help that the number of columns returned varies every month.
With the help of #mxix I was able to come up with the following:
DECLARE #POASQL NVARCHAR(MAX)
SET #POASQL = N'INSERT INTO #tmpPOAFinal (EncObjID,'+#colsPOA+') SELECT * FROM ##tmpPOA'
EXECUTE(#POASQL)
I put this after the EXECUTE(#queryPOA) in my main query.
In order for this to work with Dynamic SQL the rows/colums need to exists more than zero times. Whether it be for one or more patient. I would try to fan out the number of POA possibilities right off the bat and then left outer join to get the actual values back.
IF OBJECT_ID('tempdb..#tmpPOA') IS NOT NULL DROP TABLE #tmpPOA
CREATE TABLE #tmpPOA (POA varchar(10))
IF OBJECT_ID('tempdb..#tmpPatient') IS NOT NULL DROP TABLE #tmpPatient
CREATE TABLE #tmpPatient (Patient varchar(15))
INSERT INTO #tmpPatient VALUES ('ABC123'),('ABC456'),('ABC789')
DECLARE #POAFlag as INT = 0
WHILE #POAFlag <36
BEGIN
INSERT INTO #tmpPOA
VALUES('POA' +CONVERT(varchar,#POAFlag))
SET #POAFlag = #POAFlag + 1
END
SELECT * FROM #tmpPOA
CROSS JOIN #tmpPatient
This should fan out all of the possibilities of the 35DXCodes for you to get their POA flag.

Execute multiple dynamic T-SQL statements and obtain a limited number of unique values while preserving order

I have a SourceTable and a table variable #TQueries containing various T-SQL predicates that target SourceTable.
The expected result is to dynamically generate SELECT statements that return a list of Id's as specified by the predicates in #TQueries. Each dynamically generated SELECT statement also needs to execute in a particular order, and the final set of values needs to be unique and the ordering must be preserved.
Fortunately, there's a limit to how many values need to be retrieved and how many dynamic queries need to be generated. The Id list should contain at most 10 Ids, and we don't expect more than 7 queries.
The following is a sample of this setup, not the actual data/database:
-- Set up some test data, this is quick and dirty just to provide some data to test against
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SourceTable]') AND type in (N'U'))
BEGIN
-- Create a numbers table, sorta
SELECT TOP 20
IDENTITY(INT,1,1) AS Id,
ABS(CHECKSUM(NewId())) % 100 AS [SomeValue]
INTO [SourceTable]
FROM sysobjects a
END
DECLARE #TQueries TABLE (
[Ordinal] INT,
[WherePredicate] NVARCHAR(MAX),
[OrderByPredicate] NVARCHAR(MAX)
);
-- Simulate SELECTs with different order by that get different data due to varying WHERE clauses and ORDER conditions
INSERT INTO #TQueries VALUES ( 1, N'[Id] IN (6,11,13,7,10,3,15)', '[SomeValue] ASC' ) -- Sort Asc
INSERT INTO #TQueries VALUES ( 2, N'[Id] IN (9,15,14,20,17)', '[SomeValue] DESC' ) -- Sort Desc
INSERT INTO #TQueries VALUES ( 3, N'[Id] IN (20,10,1,16,11,19,9,15,17,6,2,3,13)', 'NEWID()' ) -- Sort Random
My main issue has been avoiding the use of a CURSOR or iterating through the rows one by one. The closest I've come to a set operation that meets this criteria is using a table variable to store the results of each query or a massive CTE.
Suggestions and comments are welcome.
Here's a solution that builds a single statement both to run all the queries and to return the results.
It uses a similar approach as in your answer when iterating over the #TQueries table, i.e. it also uses {...} tokens where column values from #TQuery should go, and it puts the values there with nested REPLACE() calls.
Other than that, it heavily depends on ranking functions, and I'm not sure if doesn't really abuse them. You'd need to test this method before deciding if it's better or worse than the one you've got so far.
DECLARE #QueryTemplate nvarchar(max), #FinalSQL nvarchar(max);
SET #QueryTemplate =
N'SELECT
[Id],
QueryRank = {Ordinal},
RowRank = ROW_NUMBER() OVER (ORDER BY {OrderByPredicate})
FROM [dbo].[SourceTable]
WHERE {WherePredicate}
';
SET #FinalSQL =
N'WITH AllData AS (
' +
SUBSTRING(
(
SELECT
'UNION ALL ' +
REPLACE(REPLACE(REPLACE(#QueryTemplate,
'{Ordinal}' , [Ordinal] ),
'{OrderByPredicate}', [OrderByPredicate]),
'{WherePredicate}' , [WherePredicate] )
FROM #TQueries
ORDER BY [Ordinal]
FOR XML PATH (''), TYPE
).value('.', 'nvarchar(max)'),
11, -- starting just after the first 'UNION ALL '
CAST(0x7FFFFFFF AS int) -- max int; no need to specify the exact length
) +
'),
RankedData AS (
SELECT
[Id],
QueryRank,
RowRank,
ValueRank = ROW_NUMBER() OVER (PARTITION BY [Id] ORDER BY QueryRank)
FROM AllData
)SELECT TOP (#top)
[Id]
FROM RankedData
WHERE ValueRank = 1
ORDER BY
QueryRank,
RowRank
';
PRINT #FinalSQL;
EXECUTE sp_executesql #FinalSQL, N'#top int', 10;
Basically, every subquery gets these auxiliary columns:
QueryRank – a constant value (within the subquery's result set) derived from [Ordinal];
RowRank – a ranking assigned to a row based on the [OrderByPredicate].
The result sets are UNIONed and then every entry of every unique value is again ranked (ValueRank) based on the query ranking.
When pulling the final result set, duplicates are suppressed (by the condition ValueRank = 1), and QueryRank and RowRank are used in the ORDER BY clause to preserve the original row order.
I used EXECUTE sp_executesql #query instead of EXECUTE (#query), because the former allows you to add parameters to the query. In particular, I parametrised the number of results to return (the argument of TOP). But you could certainly concatenate that value into the dynamic script directly, just like other things, if you prefer EXECUTE () over EXECUTE sq_executesql.
If you like, you can try this query at SQL Fiddle. (Note: the SQL Fiddle version replaces the #TQueries table variable with the TQueries table.)
This is what I've managed to piece together cobbled from my original response and improved upon by comments from #AndriyM
DECLARE #sql_prefix NVARCHAR(MAX);
SET #sql_prefix =
N'DECLARE #TResults TABLE (
[Ordinal] INT IDENTITY(1,1),
[ContentItemId] INT
);
DECLARE #max INT, #top INT;
SELECT #max = 10;';
DECLARE #sql_insert_template NVARCHAR(MAX), #sql_body NVARCHAR(MAX);
SET #sql_insert_template =
N'SELECT #top = #max - COUNT(*) FROM #TResults;
INSERT INTO #TResults
SELECT TOP (#top) [Id]
FROM [dbo].[SourceTable]
WHERE
{WherePredicate}
AND NOT EXISTS (
SELECT 1
FROM #TResults AS [tr]
WHERE [tr].[ContentItemId] = [SourceTable].[Id]
)
ORDER BY {OrderByPredicate};';
WITH Query ([Ordinal],[SqlCommand]) AS (
SELECT
[Ordinal],
REPLACE(REPLACE(#sql_insert_template, '{WherePredicate}', [WherePredicate]), '{OrderByPredicate}', [OrderByPredicate])
FROM #TQueries
)
SELECT
#sql_body = #sql_prefix + (
SELECT [SqlCommand]
FROM Query
ORDER BY [Ordinal] ASC
FOR XML PATH(''),TYPE).value('.', 'varchar(max)') + CHAR(13)+CHAR(10)
+N' SELECT * FROM #TResults ORDER BY [Ordinal]';
EXEC(#sql_body);
The basic idea is to use a table variable to hold the results of each query. I create a template for the SQL and replace the values in the template based on what is stored in #TQueries.
Once the entire script is completed I run it with EXEC.

Paging, sorting and filtering in a stored procedure (SQL Server)

I was looking at different ways of writing a stored procedure to return a "page" of data. This was for use with the ASP ObjectDataSource, but it could be considered a more general problem.
The requirement is to return a subset of the data based on the usual paging parameters; startPageIndex and maximumRows, but also a sortBy parameter to allow the data to be sorted. Also there are some parameters passed in to filter the data on various conditions.
One common way to do this seems to be something like this:
[Method 1]
;WITH stuff AS (
SELECT
CASE
WHEN #SortBy = 'Name' THEN ROW_NUMBER() OVER (ORDER BY Name)
WHEN #SortBy = 'Name DESC' THEN ROW_NUMBER() OVER (ORDER BY Name DESC)
WHEN #SortBy = ...
ELSE ROW_NUMBER() OVER (ORDER BY whatever)
END AS Row,
.,
.,
.,
FROM Table1
INNER JOIN Table2 ...
LEFT JOIN Table3 ...
WHERE ... (lots of things to check)
)
SELECT *
FROM stuff
WHERE (Row > #startRowIndex)
AND (Row <= #startRowIndex + #maximumRows OR #maximumRows <= 0)
ORDER BY Row
One problem with this is that it doesn't give the total count and generally we need another stored procedure for that. This second stored procedure has to replicate the parameter list and the complex WHERE clause. Not nice.
One solution is to append an extra column to the final select list, (SELECT COUNT(*) FROM stuff) AS TotalRows. This gives us the total but repeats it for every row in the result set, which is not ideal.
[Method 2]
An interesting alternative is given here (https://web.archive.org/web/20211020111700/https://www.4guysfromrolla.com/articles/032206-1.aspx) using dynamic SQL. He reckons that the performance is better because the CASE statement in the first solution drags things down. Fair enough, and this solution makes it easy to get the totalRows and slap it into an output parameter. But I hate coding dynamic SQL. All that 'bit of SQL ' + STR(#parm1) +' bit more SQL' gubbins.
[Method 3]
The only way I can find to get what I want, without repeating code which would have to be synchronized, and keeping things reasonably readable is to go back to the "old way" of using a table variable:
DECLARE #stuff TABLE (Row INT, ...)
INSERT INTO #stuff
SELECT
CASE
WHEN #SortBy = 'Name' THEN ROW_NUMBER() OVER (ORDER BY Name)
WHEN #SortBy = 'Name DESC' THEN ROW_NUMBER() OVER (ORDER BY Name DESC)
WHEN #SortBy = ...
ELSE ROW_NUMBER() OVER (ORDER BY whatever)
END AS Row,
.,
.,
.,
FROM Table1
INNER JOIN Table2 ...
LEFT JOIN Table3 ...
WHERE ... (lots of things to check)
SELECT *
FROM stuff
WHERE (Row > #startRowIndex)
AND (Row <= #startRowIndex + #maximumRows OR #maximumRows <= 0)
ORDER BY Row
(Or a similar method using an IDENTITY column on the table variable).
Here I can just add a SELECT COUNT on the table variable to get the totalRows and put it into an output parameter.
I did some tests and with a fairly simple version of the query (no sortBy and no filter), method 1 seems to come up on top (almost twice as quick as the other 2). Then I decided to test probably I needed the complexity and I needed the SQL to be in stored procedures. With this I get method 1 taking nearly twice as long as the other 2 methods. Which seems strange.
Is there any good reason why I shouldn't spurn CTEs and stick with method 3?
UPDATE - 15 March 2012
I tried adapting Method 1 to dump the page from the CTE into a temporary table so that I could extract the TotalRows and then select just the relevant columns for the resultset. This seemed to add significantly to the time (more than I expected). I should add that I'm running this on a laptop with SQL Server Express 2008 (all that I have available) but still the comparison should be valid.
I looked again at the dynamic SQL method. It turns out I wasn't really doing it properly (just concatenating strings together). I set it up as in the documentation for sp_executesql (with a parameter description string and parameter list) and it's much more readable. Also this method runs fastest in my environment. Why that should be still baffles me, but I guess the answer is hinted at in Hogan's comment.
I would most likely split the #SortBy argument into two, #SortColumn and #SortDirection, and use them like this:
…
ROW_NUMBER() OVER (
ORDER BY CASE #SortColumn
WHEN 'Name' THEN Name
WHEN 'OtherName' THEN OtherName
…
END *
CASE #SortDirection
WHEN 'DESC' THEN -1
ELSE 1
END
) AS Row
…
And this is how the TotalRows column could be defined (in the main select):
…
COUNT(*) OVER () AS TotalRows
…
I would definitely want to do a combination of a temp table and NTILE for this sort of approach.
The temp table will allow you to do your complicated series of conditions just once. Because you're only storing the pieces you care about, it also means that when you start doing selects against it further in the procedure, it should have a smaller overall memory usage than if you ran the condition multiple times.
I like NTILE() for this better than ROW_NUMBER() because it's doing the work you're trying to accomplish for you, rather than having additional where conditions to worry about.
The example below is one based off a similar query I'm using as part of a research query; I have an ID I can use that I know will be unique in the results. Using an ID that was an identity column would also be appropriate here, though.
--DECLARES here would be stored procedure parameters
declare #pagesize int, #sortby varchar(25), #page int = 1;
--Create temp with all relevant columns; ID here could be an identity PK to help with paging query below
create table #temp (id int not null primary key clustered, status varchar(50), lastname varchar(100), startdate datetime);
--Insert into #temp based off of your complex conditions, but with no attempt at paging
insert into #temp
(id, status, lastname, startdate)
select id, status, lastname, startdate
from Table1 ...etc.
where ...complicated conditions
SET #pagesize = 50;
SET #page = 5;--OR CAST(#startRowIndex/#pagesize as int)+1
SET #sortby = 'name';
--Only use the id and count to use NTILE
;with paging(id, pagenum, totalrows) as
(
select id,
NTILE((SELECT COUNT(*) cnt FROM #temp)/#pagesize) OVER(ORDER BY CASE WHEN #sortby = 'NAME' THEN lastname ELSE convert(varchar(10), startdate, 112) END),
cnt
FROM #temp
cross apply (SELECT COUNT(*) cnt FROM #temp) total
)
--Use the id to join back to main select
SELECT *
FROM paging
JOIN #temp ON paging.id = #temp.id
WHERE paging.pagenum = #page
--Don't need the drop in the procedure, included here for rerunnability
drop table #temp;
I generally prefer temp tables over table variables in this scenario, largely so that there are definite statistics on the result set you have. (Search for temp table vs table variable and you'll find plenty of examples as to why)
Dynamic SQL would be most useful for handling the sorting method. Using my example, you could do the main query in dynamic SQL and only pull the sort method you want to pull into the OVER().
The example above also does the total in each row of the return set, which as you mentioned was not ideal. You could, instead, have a #totalrows output variable in your procedure and pull it as well as the result set. That would save you the CROSS APPLY that I'm doing above in the paging CTE.
I would create one procedure to stage, sort, and paginate (using NTILE()) a staging table; and a second procedure to retrieve by page. This way you don't have to run the entire main query for each page.
This example queries AdventureWorks.HumanResources.Employee:
--------------------------------------------------------------------------
create procedure dbo.EmployeesByMartialStatus
#MaritalStatus nchar(1)
, #sort varchar(20)
as
-- Init staging table
if exists(
select 1 from sys.objects o
inner join sys.schemas s on s.schema_id=o.schema_id
and s.name='Staging'
and o.name='EmployeesByMartialStatus'
where type='U'
)
drop table Staging.EmployeesByMartialStatus;
-- Populate staging table with sort value
with s as (
select *
, sr=ROW_NUMBER()over(order by case #sort
when 'NationalIDNumber' then NationalIDNumber
when 'ManagerID' then ManagerID
-- plus any other sort conditions
else EmployeeID end)
from AdventureWorks.HumanResources.Employee
where MaritalStatus=#MaritalStatus
)
select *
into #temp
from s;
-- And now pages
declare #RowCount int; select #rowCount=COUNT(*) from #temp;
declare #PageCount int=ceiling(#rowCount/20); --assuming 20 lines/page
select *
, Page=NTILE(#PageCount)over(order by sr)
into Staging.EmployeesByMartialStatus
from #temp;
go
--------------------------------------------------------------------------
-- procedure to retrieve selected pages
create procedure EmployeesByMartialStatus_GetPage
#page int
as
declare #MaxPage int;
select #MaxPage=MAX(Page) from Staging.EmployeesByMartialStatus;
set #page=case when #page not between 1 and #MaxPage then 1 else #page end;
select EmployeeID,NationalIDNumber,ContactID,LoginID,ManagerID
, Title,BirthDate,MaritalStatus,Gender,HireDate,SalariedFlag,VacationHours,SickLeaveHours
, CurrentFlag,rowguid,ModifiedDate
from Staging.EmployeesByMartialStatus
where Page=#page
GO
--------------------------------------------------------------------------
-- Usage
-- Load staging
exec dbo.EmployeesByMartialStatus 'M','NationalIDNumber';
-- Get pages 1 through n
exec dbo.EmployeesByMartialStatus_GetPage 1;
exec dbo.EmployeesByMartialStatus_GetPage 2;
-- ...etc (this would actually be a foreach loop, but that detail is omitted for brevity)
GO
I use this method of using EXEC():
-- SP parameters:
-- #query: Your query as an input parameter
-- #maximumRows: As number of rows per page
-- #startPageIndex: As number of page to filter
-- #sortBy: As a field name or field names with supporting DESC keyword
DECLARE #query nvarchar(max) = 'SELECT * FROM sys.Objects',
#maximumRows int = 8,
#startPageIndex int = 3,
#sortBy as nvarchar(100) = 'name Desc'
SET #query = ';WITH CTE AS (' + #query + ')' +
'SELECT *, (dt.pagingRowNo - 1) / ' + CAST(#maximumRows as nvarchar(10)) + ' + 1 As pagingPageNo' +
', pagingCountRow / ' + CAST(#maximumRows as nvarchar(10)) + ' As pagingCountPage ' +
', (dt.pagingRowNo - 1) % ' + CAST(#maximumRows as nvarchar(10)) + ' + 1 As pagingRowInPage ' +
'FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY ' + #sortBy + ') As pagingRowNo, COUNT(*) OVER () AS pagingCountRow ' +
'FROM CTE) dt ' +
'WHERE (dt.pagingRowNo - 1) / ' + CAST(#maximumRows as nvarchar(10)) + ' + 1 = ' + CAST(#startPageIndex as nvarchar(10))
EXEC(#query)
At result-set after query result columns:
Note:
I add some extra columns that you can remove them:
pagingRowNo : The row number
pagingCountRow : The total number of rows
pagingPageNo : The current page number
pagingCountPage : The total number of pages
pagingRowInPage : The row number that started with 1 in this page

Resources