As we all know in LINQ we have the functionality of Skip and Take on list of objects , where we can skip some rows of list of objects and take the required list of objects. So here in this - "skip and take in sql server 2008" article i will try to explain the same functionality in SQL Server.
Before that lets see how skip and take functions work in LINQ -
To explain this i will create a class called "Employee.cs" as below -
For demonstration purpose i will create a table called "Employee" and insert the same list of employees into that table.
The result is below -
Hope this article is useful. Please comment your feedback.
Before that lets see how skip and take functions work in LINQ -
To explain this i will create a class called "Employee.cs" as below -
//Example for Skip and take in sql server 2008 public class Employee { public int ID { get; set; } public string Name { get; set; } }Now lets create a method which will return the list of employees -
//Example for Skip and take in sql server 2008 public static List<Employee> GetEmployees() { List<Employee> employees = new List<Employee>(); employees.Add(new Employee() { ID = 1, Name = "Jack" }); employees.Add(new Employee() { ID = 2, Name = "Aaron" }); employees.Add(new Employee() { ID = 3, Name = "Peter" }); employees.Add(new Employee() { ID = 4, Name = "David" }); employees.Add(new Employee() { ID = 5, Name = "Abel" }); employees.Add(new Employee() { ID = 6, Name = "Adam" }); employees.Add(new Employee() { ID = 7, Name = "Brian" }); return employees; }Now my intention would be to get 2 employees from the above list namely "Abel" and "Adam" and skip first 4 employees of the list.
//Example for Skip and take in sql server 2008 ListNow lets get into SQL and check the same functionality -selectedEmps = emps.Skip(4).Take(2).ToList();
For demonstration purpose i will create a table called "Employee" and insert the same list of employees into that table.
CREATE TABLE Employee ( ID INT IDENTITY(100,1), NAME VARCHAR(50) ) INSERT INTO Employee VALUES ('Jack') ,('Aaron') ,('Peter') ,('David') ,('Abel') ,('Adam') ,('Brian')Now i will go ahead and create a table valued function to get the table with required number of records.
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION FN_TakeNRows ( @startRowNo INT = 1 ,@endRowNo INT = 1 ) RETURNS @filteredRows TABLE ( ID INT ,Name VARCHAR(50) ,UniqueID INT ) AS BEGIN INSERT INTO @filteredRows ( ID, Name, UniqueID ) SELECT TOP (@endRowNo) allRows.ID, allRows.Name, allRows.uniqueID FROM ( SELECT ID ,Name ,ROW_NUMBER() over(order by ID) as uniqueID FROM Employee ) allRows WHERE allRows.uniqueID > @startRowNo RETURN; END GOAs in LINQ query we will pass the values 4 and 2 as shown below to get 2 employee records. Execute statement of the function is as below -
SELECT * FROM FN_TakeNRows (4, 2)So here in the above execute statement similar to LINQ query it skips 4 employee records and takes 2 employee records.
The result is below -
Hope this article is useful. Please comment your feedback.
No comments:
Post a Comment
Comments Section