Aquí, finalmente, agregaré proyectos para utilizar procedimientos almacenados en lugar de sentencias construidas en el código.
De paso, y por recomendación de un mayor experto que yo en Entity Framework, agregué «AsNoTracking()» a la consulta LINQ de Entity Framework establecida en Generando una API REST (JSON) [2].
El procedimiento Almacenado.
Este es el procedimiento almacenado que recibe, el Id de país, la fecha desde y la fecha hasta, y la página a mostrar.
Es el procedimiento almacenado el responsable de establecer valores adecuados a los parámetros de fecha, en lugar de establecerlos desde el componente en C#.
Exactamente igual, pero con «FOR JSON PATH» al final, se usa en el proyecto que utiliza código puro.
El Cambio en Entity Framework
Basado en la propuesta y comentario, el código queda como sigue:
public IEnumerable<OwidCovidDatum> GetCountryData(
intCountryId,
DateTime?fromDate=null,
DateTime?toDate=null,
intPage=1)
{
fromDate=fromDate??
(from el in _context.OwidCovidData orderby el.Date select el.Date).FirstOrDefault();
toDate=toDate??
(from el in _context.OwidCovidData orderby el.Date descendingselect el.Date).FirstOrDefault();
return (from OwidCovidDatum el in
_context.OwidCovidData
.Where(x=> x.Date>=fromDate && x.Date<=toDate && x.CountriesId==CountryId)
.Skip((Page-1)*100)
.Take(100) select el).AsNoTracking().ToList();
}
Dapper Usando Procedimientos Almacenados
Utilizamos la capacidad de ejecución de procedimientos almacenados de Dapper, que es capaz de asignar valores a los parámetros por coincidencia de nombres.
publicasyncTask<string>GetCountryData(intCountryId,DateTime?fromDate=null,DateTime?toDate=null,intPage=1){varresult=await_dal.GetDataAsync("[Owid Covid Data Get By Country]",new{fromDate,toDate,CountryId,Page});stringjson=JsonSerializer.Serialize(result,newJsonSerializerOptions(){WriteIndented=true,ReferenceHandler=ReferenceHandler.Preserve});returnjson;}
Código usando Procedimientos Almacenados
En el caso del código directo, asignamos los parámetros uno a uno, especificando además el tipo de dato, que permite una mayor especificidad.
publicasyncTask<string>GetCountryData(intCountryId,DateTime?fromDate=null,DateTime?toDate=null,intPage=1){varcommand=_dal.CreateCommand("[Owid Covid Data Get By Country JSON]");command.Parameters.Add("@fromDate",System.Data.SqlDbType.SmallDateTime).Value=fromDate;command.Parameters.Add("@toDate",System.Data.SqlDbType.SmallDateTime).Value=toDate;command.Parameters.Add("@CountryId",System.Data.SqlDbType.Int).Value=CountryId;command.Parameters.Add("@skip",System.Data.SqlDbType.Int).Value=Page;varjson=await_dal.GetJSONDataAsync(command);returnjson;}
Rendimiento
El gráfico muestra que, aún cuando se utilizan características que mejoran la efectividad, la simpleza del código mejora el rendimiento.
O sea, para mejor respuesta al usuario, se deberá invertir más tiempo de los desarrolladores en mejorar su desarrollo.
Como detalle, las llamadas de procedimiento almacenado, realizan directamente llamadas al mismo, en lugar de utilizar, como vimos en la publicación anterior, sp_executesql.
EXEC[Owid Covid Data Get By Country]@fromDate=NULL,@toDate=NULL,@CountryId=4,@Page=3;
Let’s consider another requirement, to evaluate how you can best take advantage of the features of the Entity Framework and emulate that functionality in cases where it cannot be used, or it is more convenient to do something else.
In this example, we are using the same database explained in Data-for-demos
The requirement
You need to get the statistical information of cases, vaccinations, etc. By country, between certain dates, with the following conditions:
If no start date is entered, the first available date is used.
If the end date is not entered, the last available date is used.
The information must be returned in batches of 100 entries, so the requested page number must be received.
In this case, it will be implemented in the “Country» controller
Entity Framework.
The code leverages EF’s Fluent capabilities to nest conditions. Similarly, below, the Entity Framework generates a statement according to the data engine in use, in this case, SQL Server.
publicasync Task<ActionResult<IEnumerable<OwidCovidDatum>>> GetCountryData(
intCountryId,
DateTime?fromDate=null,
DateTime?toDate=null,
intPage=1)
{
fromDate=fromDate??
(from el in _context.OwidCovidData orderby el.Date select el.Date).FirstOrDefault();
toDate=toDate??
(from el in _context.OwidCovidData orderby el.Date descendingselect el.Date).FirstOrDefault();
return (from OwidCovidDatum el in
_context.OwidCovidData
.Where(x=> x.Date>=fromDate && x.Date<=toDate && x.CountriesId==CountryId)
.Skip((Page-1)*100)
.Take(100) select el).ToList();
}
Dapper
Using the returned DapperRows, we implement the call with an SQL statement that is almost the same as the one automatically generated by EF.
As in the previous example, we create a parameterized Command object that returns a string of characters with the resulting JSON, implemented in the SQL statement.
Consideremos otro requisito, para evaluar como puede aprovecharse mejor las características de Entity Framework y emular esa funcionalidad en los casos en que no se pueda utilizar, o sea más conveniente otra forma de realizar la tarea.
En este ejemplo, estamos utilizando la misma base de datos explicada en Datos-para-demos
El requisito
Se necesita obtener la información estadística de casos, vacunaciones, etc. Por país, entre determinadas fechas, con las siguientes condiciones:
Si no se consigna fecha de inicio, se usa la primera disponible.
Si no se consigna la fecha de fin, se usa la última disponible.
Se debe retornar la información en lotes de a 100 entradas, con lo cual, se deberá recibir el número de página solicitado.
En este caso, se implementará en el controlador «Country«
Entity Framework.
El código aprovecha las funcionalidades Fluent de EF para anidar las condiciones. Igualmente, por debajo, Entity Framework genera una sentencia acorde al motor de datos en uso, en este caso, SQL Server.
publicasync Task<ActionResult<IEnumerable<OwidCovidDatum>>> GetCountryData(
intCountryId,
DateTime?fromDate=null,
DateTime?toDate=null,
intPage=1)
{
fromDate=fromDate??
(from el in _context.OwidCovidData orderby el.Date select el.Date).FirstOrDefault();
toDate=toDate??
(from el in _context.OwidCovidData orderby el.Date descendingselect el.Date).FirstOrDefault();
return (from OwidCovidDatum el in
_context.OwidCovidData
.Where(x=> x.Date>=fromDate && x.Date<=toDate && x.CountriesId==CountryId)
.Skip((Page-1)*100)
.Take(100) select el).ToList();
}
Dapper
Utilizando los DapperRow de retorno, implementamos la llamada con una sentencia SQL que es casi igual a la generada automáticamente por EF.
Al igual que en el ejemplo previo, creamos un objeto Command con parámetros que retorne una cadena de caracteres con el JSON resultante, implementado en la sentencia SQL.
I’m not going to go into detail here describing what is REST (Representational State Transfer) an API (Application Programming Interface) or JSON (JavaScript Object Notation). I start from the base that is already known to which we refer.
In this case, using the database published in the previous entry Data for demos, I will present how to expose the information from that database, in a read-only API.
At the same time, I will try to show advantages and disadvantages of different methods to achieve the same objective.
Entity Framework
Although in general, I dislike its use, I will try to include it in each demonstration, to see what advantages it brings, and the disadvantages that arise.
The project within the solution is ApiRestEF
Dapper
As a data access package/library, it allows you to easily obtain information from databases.
The project, within the solution is ApiRestDapper
Coding directly
In this case, I’ll be showing how to do, the same thing, but step by step, without libraries.
The project, within the solution is ApiRestCode
First requirement
A method is needed to obtain the information that is displayed, for a continent indicated as a parameter.
Continent
Country
Total Cases
Total Deaths
Total Cases per Million
Total Deaths per Million
Population
Population Density
GDP per Capita
Africa
Seychelles
12466
46
126764
468
98340
208
26382
Africa
Cape Verde
31433
271
56535
487
555988
136
6223
Africa
Tunisia
362658
13305
30685
1126
11818618
74
10849
Africa
South Africa
1722086
57410
29036
968
59308690
47
12295
Africa
Libya
188386
3155
27416
459
6871287
4
17882
Africa
Botswana
59480
896
25293
381
2351625
4
15807
Africa
Namibia
61374
968
24154
381
2540916
3
9542
Africa
Eswatini
18705
676
16123
583
1160164
79
7739
Africa
Morocco
522765
9192
14163
249
36910558
80
7485
Africa
Djibouti
11570
154
11711
156
988002
41
2705
Africa
Gabon
24696
156
11096
70
2225728
8
16562
Entity Framework
The code used obtains the continent and its constituent countries in a single query but required to go through the countries, to obtain from each one, the demographic data.
As I said, I don’t like EF and maybe that’s why my research into methods to do it, may not have found another way.
Of course, if someone offers another proposal in the comments, I’ll add it here, and I’ll also proceed to evaluate the corresponding metric.
For the case of implementation with Dapper, we directly use the «DapperRow» types as the query return, thus decreasing the mapping between columns and properties. If defined classes were used, the response time would surely be longer.
Finally, for direct query by code, we optimize using the FOR JOSN modifier, and then directly obtaining the resulting JSON string.
publicasyncTask<ActionResult<string>>GetContinent(intid){stringsql=@" SELECT
[C].[Continent]
, [CO].[Country]
, [D].[total_cases]
, [D].[total_deaths]
, [D].[total_cases_per_million]
, [D].[total_deaths_per_million]
, [D].[population]
, [D].[population_density]
, [D].[gdp_per_capita]
FROM
[OwidCountriesData] AS [D]
INNER JOIN
[Continents] AS [C]
ON [D].[ContinentId] = [C].[Id]
INNER JOIN
[Countries] AS [CO]
ON [D].[CountriesId] = [CO].[Id]
WHERE([C].[Id] = @continent)
ORDER BY
[D].[total_cases_per_million] DESC FOR JSON AUTO, INCLUDE_NULL_VALUES, ROOT('CountriesInfo');";SqlCommandcom=_dal.CreateCommand(sql);com.Parameters.AddWithValue("@continent",id);returnawait_dal.GetJSONDataAsync(com);}
Note
In both the Dapper and code projects, a minimal data access layer was built to emulate functionality similar to that provided by EF-generated code.
Performance
The graph below shows the comparison in CPU utilization, lifetime, and disk reads, in each of the cases. A picture is worth a thousand words. 🙂
In the graph, values are evaluated only from the point of view of the database, not the .Net code, nor its runtime. I will add this in the next installment.
Lately, I have detected in social networks, various queries about the generation of datasets to expose through, for example, REST APIs.
Having answered some of them directly, I thought it better to leave this published, to facilitate the task of possible future doubts.
For this and other possible examples to come, I decided to generate a sample database, which can be built from freely used published data, by Our World in Data,regarding COVID-19.
This sample database, using real data but not containing personal information of any kind, will allow me to set out some issues related to real-world work. For example, that the volume of data to be used may be large, or that the structure of the data is not always in accordance with what we would like to have.
For the generation of such a database, I decided to create a Jupyter Notebook,which is available here. On the same share, I also left a backpac file that allows you to import the sample base directly into a SQL Server.
However, I also left the Notebook, because that way you can run it to generate the database with updated data, as many times as you want.
Here I will leave as a reference, those other publications that make use of this database.
No voy a entrar aquí en detalles de descripción de lo que es REST (Representational State Transfer) una API (Application Programming Interface) o JSON (JavaScript Object Notation). Parto de la base que ya se conoce a que nos referimos.
En este caso, utilizando la base de datos publicada en la entrada anterior Datos para demos, presentaré como exponer la información de esa base, en una API de sólo lectura.
Al mismo tiempo, intentaré mostrar ventajas y desventajas de distintos métodos para lograr el mismo objetivo.
Entity Framework
Aunque en líneas generales, me disgusta bastante su utilización, intentaré incluirlo en cada demostración, por ver que ventajas aporta, y los inconvenientes que surjan.
El proyecto dentro de la solución es ApiRestEF
Dapper
Como paquete/biblioteca de acceso a datos, permite fácilmente obtener información de bases de datos.
El proyecto, dentro de la solución es ApiRestDapper
Código directo
En este caso, estaré mostrando como hacer, lo mismo, pero paso a paso, sin bibliotecas.
El proyecto, dentro de la solución es ApiRestCode
Primer requisito.
Se necesita un mecanismo por el cual obtener la información que se muestra, para un continente indicado como parámetro.
Continent
Country
Total Cases
Total Deaths
Total Cases per Million
Total Deaths per Million
Population
Population Density
GDP per Capita
Africa
Seychelles
12466
46
126764
468
98340
208
26382
Africa
Cape Verde
31433
271
56535
487
555988
136
6223
Africa
Tunisia
362658
13305
30685
1126
11818618
74
10849
Africa
South Africa
1722086
57410
29036
968
59308690
47
12295
Africa
Libya
188386
3155
27416
459
6871287
4
17882
Africa
Botswana
59480
896
25293
381
2351625
4
15807
Africa
Namibia
61374
968
24154
381
2540916
3
9542
Africa
Eswatini
18705
676
16123
583
1160164
79
7739
Africa
Morocco
522765
9192
14163
249
36910558
80
7485
Africa
Djibouti
11570
154
11711
156
988002
41
2705
Africa
Gabon
24696
156
11096
70
2225728
8
16562
Entity Framework
El código utilizado obtiene el continente y sus países constitutivos en una sola consulta pero requirió recorrer los países, para obtener de cada uno, los datos demográficos.
Como ya dije, no me gusta EF y quizás por ello, mi investigación de métodos para hacerlo, puede no haber encontrado otra forma.
Por supuesto, si alguien ofrece otra propuesta en los comentarios, la agregaré aquí, y también procederé a evaluar la métrica correspondiente.
para el caso de la implementación con Dapper, utilizamos directamente los tipos «DapperRow» como retorno de la consulta, disminuyendo así el mapeo entre columnas y propiedades. De usarse clases definidas, seguramente el tiempo de respuesta sería mayor.
Finalmente, para la consulta directa por código, optimizamos utilizando el modificador FOR JOSN, y obteniendo entonces directamente la cadena JSON resultante.
publicasyncTask<ActionResult<string>>GetContinent(intid){stringsql=@" SELECT
[C].[Continent]
, [CO].[Country]
, [D].[total_cases]
, [D].[total_deaths]
, [D].[total_cases_per_million]
, [D].[total_deaths_per_million]
, [D].[population]
, [D].[population_density]
, [D].[gdp_per_capita]
FROM
[OwidCountriesData] AS [D]
INNER JOIN
[Continents] AS [C]
ON [D].[ContinentId] = [C].[Id]
INNER JOIN
[Countries] AS [CO]
ON [D].[CountriesId] = [CO].[Id]
WHERE([C].[Id] = @continent)
ORDER BY
[D].[total_cases_per_million] DESC FOR JSON AUTO, INCLUDE_NULL_VALUES, ROOT('CountriesInfo');";SqlCommandcom=_dal.CreateCommand(sql);com.Parameters.AddWithValue("@continent",id);returnawait_dal.GetJSONDataAsync(com);}
Aclaración
Tanto en el proyecto Dapper como en el de código, se construyó una capa de acceso a datos mínima, para emular similar funcionalidad a la brindada por el código generado por EF.
Rendimiento
El gráfico inferior muestra la comparativa en utilización de CPU, tiempo de duración, y lecturas a disco, en cada uno de los casos. Una imagen, vale más que mil palabras. 🙂
En el gráfico, solo se evalúan valores desde el punto de vista de la base de datos, no del código .Net, ni su tiempo de ejecución. Agregaré esto en la próxima entrega.
Últimamente, he detectado en redes sociales, variadas consultas acerca de la generación de conjuntos de datos para exponer a través de, por ejemplo, API REST.
Habiendo respondido algunas de ellas directamente, creí mejor dejar esto publicado, para facilitar la tarea de posibles futuras dudas.
Para éste y otros posibles ejemplos que vendrán, decidí generar una base de datos de ejemplo, que se puede construir a partir de los datos publicados de libre utilización, por Our World in Data, respecto de la COVID-19.
Esta base de datos de ejemplo, utilizando datos reales pero que no contienen información personal de ningún tipo, me permitirá exponer algunas cuestiones relacionadas con el trabajo del mundo real. Por ejemplo, que el volumen de datos a utilizar puede ser grande, o que no siempre la estructura de los datos es acorde a lo que desearíamos disponer.
Para la generación de dicha base de datos, decidí crear un Jupyter Notebook, el cual está disponible aquí. En el mismo recurso compartido, también dejé un archivo backpac que permita importar la base de ejemplo directamente en un SQL Server.
Sin embargo, dejé también el Notebook, porque de esa forma se puede ejecutar el mismo para generar la base con datos actualizados, tantas veces como se desee.
Aquí dejaré como referencia, aquellas otras publicaciones que hagan uso de esta base de datos.
In the previous post,Paging data, NOT client side. | Universidad Net, I described how to use OFFSET-FETCH pair to paginate data in the server side. In this one, I will describe an example of how to use it in a Web Assembly App.
Note: To test this code, you must create a new Blazor app in Visual Studio.
The Model.
The data model will be a class with two properties, one for the list of items and other with the page’s information.
This classes will be added in the Shared project of the Application.
The first class vary depending on the data you want to display, but the second one will be always the same, enhanced by some code.
Since the pager class will be a standardized and enhanced version of the data retrieved by using the stored procedure, and could be used in different projects, let’s define an interface for it.
IPagesInfo interface.
This is the code for the Interface.
publicinterfaceIPagesInfo
{
#region Properties
///<summary>/// Gets or sets the current page.....///</summary>
System.Int32 CurrentPage { get; set; }
///<summary>/// Gets the current first page number..///</summary>Int32 FirstPageNumber { get; }
///<summary>/// Gets the HasNextGroup/// Gets a value indicating whether this instance has next page..///</summary>Boolean HasNextGroup { get; }
///<summary>/// Gets the HasPreviousGroup/// Gets a value indicating whether this instance has previous page..///</summary>Boolean HasPreviousGroup { get; }
///<summary>/// Gets the last page number..///</summary>Int32 LastPageNumber { get; }
///<summary>/// Gets or sets the number of links to show..///</summary>Int32 NumberOfLinks { get; set; }
///<summary>/// Gets or sets the size of the page.....///</summary>
System.Int32 PageSize { get; set; }
///<summary>/// Gets or sets the Qty of rows skipped from the top of the select.....///</summary>
System.Int32 Skip { get; set; }
///<summary>/// Gets or sets the Qty of rows taken.....///</summary>
System.Int32 Take { get; set; }
///<summary>/// Gets or sets the amount of items to display.///</summary>
System.Int32 TotalItems { get; set; }
///<summary>/// Gets the total pages available to display..///</summary>Int32 TotalPages { get; }
///<summary>/// Gets the page number for the previous group start.///</summary>///<value>/// The page number.///</value>Int32 PreviousGroupStart { get; }
///<summary>/// Gets the page number for the next group start.///</summary>///<value>/// The page number.///</value>Int32 NextGroupStart { get; }
#endregion
}
PagerInfo class.
Here, you have the code for the class implementing the IPagesInfo interface.
Notice the class is responsible of the calculations about page numbers displayed, if there are next or previous groups of pages, etc.
publicclassPagerInfo : IPagesInfo
{
#region Fields
///<summary>/// Defines the lastPageNumber.///</summary>internalInt32 lastPageNumber =0;
#endregion#region Properties
publicInt32 TotalPages
{
get=> (Int32)Math.Ceiling(TotalItems / (Double)PageSize);
}
///<summary>/// Gets the page number for the previous group start.///</summary>///<value>/// The page number.///</value>publicInt32 PreviousGroupStart => LastPageNumber - NumberOfLinks;
///<summary>/// Gets the page number for the next group start.///</summary>///<value>/// The page number.///</value>publicInt32 NextGroupStart => LastPageNumber +1;
///<summary>/// Gets or sets the current page.///</summary>public System.Int32 CurrentPage { get; set; }
///<summary>/// Gets the current first page number..///</summary>publicInt32 FirstPageNumber
{
get=> LastPageNumber - (NumberOfLinks -1);
}
///<summary>/// Gets a value indicating whether this instance has next page.///</summary>publicBoolean HasNextGroup
{
get=> CurrentPage + NumberOfLinks < TotalPages;
}
///<summary>/// Gets a value indicating whether this instance has previous page.///</summary>publicBoolean HasPreviousGroup
{
get=> CurrentPage > NumberOfLinks;
}
///<summary>/// Gets the last page number..///</summary>publicInt32 LastPageNumber
{
get
{
lastPageNumber = (Int32)Math.Ceiling((Double)CurrentPage / NumberOfLinks) * NumberOfLinks;
if (lastPageNumber > TotalPages)
{
lastPageNumber = TotalPages;
}
return lastPageNumber;
}
}
///<summary>/// Gets or sets the number of links to show..///</summary>publicInt32 NumberOfLinks { get; set; } =10;
///<summary>/// Gets or sets the size of the page.....///</summary>public System.Int32 PageSize { get; set; }
///<summary>/// Gets or sets the Qty of rows skipped from the top of the select.....///</summary>public System.Int32 Skip { get; set; }
///<summary>/// Gets or sets the Qty of rows taken.....///</summary>public System.Int32 Take { get; set; }
///<summary>/// Gets or sets the amount of items to display.///</summary>public System.Int32 TotalItems { get; set; }
///<summary>/// Gets the total pages available to display..///</summary>#endregion
}
The Data Class.
This class will contain your data and the IPagesInfo properties. In this sample, it will be called PersonsPager.
Note: You can easily create it by executing the stored procedure, copying the result and in a new code window (for example, an empty one created for PersonsPager), paste it by the Edit – Paste Special-Past JSON as Classes.
Then replace the names autogenerated by your own and change the type for the second one for the PagesInfo class.
The pasted code names the main class as Rootobject, which has been renamed to PersonsPager.
The Pager class will be changed by PagesInfo, and the Pager class defined could be removed.
This is the final code for the PersonsPager class.
You must add a Razor Component. In this sample, it is called Pager.razor.
In the UI code, I use an UL tag, adding a button for previous group of pages (when the user moves beyond the first set of page numbers), buttons for the different page numbers, and a button for the next group as well.
For the buttons, the pagination, page-item, and page-link classes are used.
<navclass="text-center"><ulclass="pagination">@{//Previous page group button.stringclassName="page-item "+ (!PagesInfo.HasPreviousGroup ?" disabled":""); //If there is in previous group, the button will be disabled<liclass="@className"><buttonclass="page-link "@onclick="@(()=>
{
if (PagesInfo.HasPreviousGroup)
ChangePage(PagesInfo.PreviousGroupStart);
}
)">
⏪
</button></li>}@*Buttons for page numbers*@@for (Int32i= PagesInfo.FirstPageNumber; i<= PagesInfo.LastPageNumber; i++)
{
intpageSelector=i;
className="page-item "+ (i== PagesInfo.CurrentPage ?" active":"");
<liclass="@className"><buttonclass="page-link"@onclick="@(()=>ChangePage(pageSelector))">@string.Format("{0:00}", pageSelector)</button></li>
}
@{//Next page group button.className="page-item "+ (!PagesInfo.HasNextGroup ?" disabled":"");
<liclass="@className"><buttonclass="page-link "@onclick="@(()=>
{ if (PagesInfo.HasNextGroup)
ChangePage(PagesInfo.NextGroupStart);
}
)">
⏩
</button></li>}</ul></nav>
In the code section, parameters are defined for:
an instance of a class implementing the IPagerInfo
a value to persist the selected page
an EventCallback to notify the client page about the changes in the selection by the user.
Finally, the ChangePage function called every time the user clicks ant of the buttons is defined to change the selected page and notify the client page.
It is frequent to make web sites, mobile, UWP or Windows Applications, which need to present information of several rows which must be presented in pages of data.
That could be accomplished by 3 basic methods:
Send the content to the client and manage the pagination in it, for example, using some of the JScript frameworks available.
Using some sort of LinQ sentence, against EF, which will get the group of data after retrieving the entire set from the database.
Get the exact set of rows needed each time, by using T-SQL from SQL Server
The third method is the most responsive, since it processes the selection in the server side, and reduce the amount of traffic between server, application level and client UI.
This is how I get the information and paging support by using T-SQL.
The OFFSET – FETCH statements.
The basic tool for getting pages of data is the combination of OFFSET <n> FETCH <c>.
The n value is the number of rows excluded from the beginning of the result set, and the c value, the quantity to return.
Any query you create, could be modified by this pair of statements.
For example, let us get the employees form the database:
SELECT
[BusinessEntityID],
[Title],
[FirstName],
[MiddleName],
[LastName]
FROM
[Person].[Person]
WHERE [PersonType] = N'EM'
ORDER BY
[LastName],
[FirstName]
OFFSET 10 ROWS FETCH NEXT 20 ROWS ONLY
It is just a standard select, but with the addition of the OFFSET and FETCH NEXT statements.
Of course, the numeric values could be replaced by parameters, like in:
SELECT
[BusinessEntityID],
[Title],
[FirstName],
[MiddleName],
[LastName]
FROM
[Person].[Person]
WHERE [PersonType] = N'EM'
ORDER BY
[LastName],
[FirstName]
OFFSET @Skip ROWS FETCH NEXT @Take ROWS ONLY
In case you want to get the first set of rows, the offset value could be 0, and it will work perfectly for your need.
Getting information about the entire set of data.
If you want to display to the user, precise information about the total rows in the set of data etc. you just need to perform another query after this one, by using the same filter in WHERE statement, to count the rows.
At the same time, you can get the current page based in the offset and fetch values, like in this query:
SELECT
COUNT(*) AS [TotalItems],
@Skip / @Take + 1 AS [CurrentPage],
@Take AS [PageSize]
FROM
[Person].[Person]
WHERE [PersonType] = N'EM'
Combining both sentences in the same query, you get two result sets to obtain, for example, using a data wrapper like Dapper:
SELECT
[BusinessEntityID],
[Title],
[FirstName],
[MiddleName],
[LastName]
FROM
[Person].[Person]
WHERE [PersonType] = N'EM'
ORDER BY
[LastName],
[FirstName]
OFFSET @Skip ROWS FETCH NEXT @Take ROWS ONLY;
SELECT
COUNT(*) AS [TotalItems],
@Skip / @Take + 1 AS [CurrentPage],
@Take AS [PageSize]
FROM
[Person].[Person]
WHERE [PersonType] = N'EM';
A JSON for an API.
This sentence could be modified to retrieve a JSON structure containing the entire set, to transfer it by an API to any client, following these rules:
The entire set of information must be returned as a well-formed JSON.
Each part of the information must be property identified.
All the information must be returned as a single string value, which enables to get the information as a scalar value.
The first part must be identified as a list/array of data. The second one, just as a unique entry.
For the list of rows, adding FOR JSON PATH, will transform the result in a single JSON content.
For the information about the paging process, it will be modified to avoid create an array of JSON elements, by issuing FOR JSON PATH,WITHOUT_ARRAY_WRAPPER modifier.
Finally, concatenating both sets as different attributes of a single JSON structure, will do the trick.
To have it easy to use, we will make a stored procedure with the entire query, with the corresponding parameters, as follows:
CREATE PROCEDURE [Person].[Person_GetforPager]
( @skip INT = 0,
@take INT = 20
)
AS
BEGIN
SELECT
'{"List":' +
(
SELECT
[BusinessEntityID],
[Title],
[FirstName],
[MiddleName],
[LastName]
FROM
[Person].[Person]
WHERE [PersonType] = N'EM'
ORDER BY
[LastName],
[FirstName]
OFFSET @Skip ROWS FETCH NEXT @Take ROWS ONLY FOR JSON PATH
)
+ ',"Pager":' +
(
SELECT
COUNT(*) AS [TotalItems],
@Skip / @Take + 1 AS [CurrentPage],
@Take AS [PageSize]
FROM
[Person].[Person]
WHERE [PersonType] = N'EM' FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
)
+ '}';
END;
Segunda época del rockblog "Atascado en los 70". VIEJAS canciones y artistas PASADOS DE MODA. Tratamos al lector de usted y escribimos "rocanrol" y "roquero" con ortografía castellana.