Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are many way to implement <code>STORED PROCEDURE</code> which you need. For example you can use <code>ROW_NUMBER</code> construction inside of CTE SQL statement.</p> <p>If you use SQL Server 2012 you can use <code>OFFSET</code> and <code>FETCH</code> after <code>ORDER BY</code> to implement pagination (see <a href="http://technet.microsoft.com/en-us/library/ms188385.aspx#Offset" rel="nofollow">here</a>). In the case the SQL statement will be look very close to the corresponding MySQL or PostgreSQL statements which uses <code>OFFSET</code> and <code>LIMIT</code>. By the way Microsoft Entity Framework use <a href="http://msdn.microsoft.com/en-us/library/bb399560.aspx" rel="nofollow">Entity SQL Language</a> having close construct (<code>SKIP</code> and <code>LIMIT</code>). Probably <code>OFFSET</code> and <code>FETCH</code> would be preferred way if you use SQL Server 2012 or higher.</p> <p>Because you included SQL Server 2008 tag in your question I would not use new SQL Server 2012 constructs in my answer.</p> <p>One more good way would be to use <code>sp_executesql</code> which allows you to construct an SQL statement as string with parameters. It allows to reuse execution plans which is very important for the best performance. The approach allows you to extend the code of your <code>STORED PROCEDURE</code> to implement server side filtering (searching).</p> <p>I see that need to implement pagination in the SQL statement which contain ID of the returned data (<code>PersonId</code> in your case). So I decide to suggest you to use simplified way which use <code>SELECT TOP</code> in combination with <code>LEFT OUTER JOIN</code>.</p> <p>You <code>STORED PROCEDURE</code> <code>dbo.GetExtraPerson</code> can have two additional parameters of type <code>int</code>: <code>@skip</code> and <code>@pageSize</code>. In case of <code>@skip</code> is equal to <code>0</code> the <code>STORED PROCEDURE</code> can just execute</p> <pre class="lang-sql prettyprint-override"><code>SELECT TOP (@pageSize) PERS.PersonId ,PERS.FirstName ,PERS.LastName ,PERS.MobileNumber ,PERS.EmailId ,PERS.PersonNumber ,E.ExtraPersonId ,E.Diabetes ,E.BloodPressure FROM ExtraPerson E INNER JOIN Person PERS ON PERS.PersonId=E.PersonId WHERE E.CampId=@CampId AND ReferencePatientId=@ReferencePatientId AND E.IsDeleted=0 </code></pre> <p>If <code>@skip</code> is not equal to <code>0</code> then the corresponding SQL statement can be the following</p> <pre class="lang-sql prettyprint-override"><code>WITH GetAll AS ( SELECT PERS.PersonId ,PERS.FirstName ,PERS.LastName ,PERS.MobileNumber ,PERS.EmailId ,PERS.PersonNumber ,E.ExtraPersonId ,E.Diabetes ,E.BloodPressure FROM ExtraPerson E INNER JOIN Person PERS ON PERS.PersonId=E.PersonId WHERE E.CampId=@CampId AND ReferencePatientId=@ReferencePatientId AND E.IsDeleted=0 ),GetFirst AS ( SELECT TOP (@skip) * FROM GetAll ORDER BY Name ),GetNext AS ( SELECT TOP (@pageSize) a.* FROM GetAll AS a LEFT OUTER JOIN GetFirst AS f ON f.Id=a.Id WHERE f.Id IS NULL ORDER BY Name ) SELECT * FROM GetNext </code></pre> <p>The full code of <code>dbo.GetExtraPerson</code> could be about the following</p> <pre><code>SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE dbo.GetExtraPerson @CampId int, @ReferencePatientId bigint, @skip int, @pageSize int AS BEGIN DECLARE @records int; SET NOCOUNT ON; SET @records = (SELECT COUNT(*) FROM ExtraPerson E INNER JOIN Person PERS ON PERS.PersonId=E.PersonId WHERE E.CampId=@CampId AND ReferencePatientId=@ReferencePatientId AND E.IsDeleted=0); IF @skip &lt;= 0 SELECT TOP (@pageSize) PERS.PersonId ,PERS.FirstName ,PERS.LastName ,PERS.MobileNumber ,PERS.EmailId ,PERS.PersonNumber ,E.ExtraPersonId ,E.Diabetes ,E.BloodPressure FROM ExtraPerson E INNER JOIN Person PERS ON PERS.PersonId=E.PersonId WHERE E.CampId=@CampId AND ReferencePatientId=@ReferencePatientId AND E.IsDeleted=0 ELSE WITH GetAll AS ( SELECT PERS.PersonId ,PERS.FirstName ,PERS.LastName ,PERS.MobileNumber ,PERS.EmailId ,PERS.PersonNumber ,E.ExtraPersonId ,E.Diabetes ,E.BloodPressure FROM ExtraPerson E INNER JOIN Person PERS ON PERS.PersonId=E.PersonId WHERE E.CampId=@CampId AND ReferencePatientId=@ReferencePatientId AND E.IsDeleted=0 ),GetFirst AS ( SELECT TOP (@skip) * FROM GetAll ORDER BY Name ),GetNext AS ( SELECT TOP (@pageSize) a.* FROM GetAll AS a LEFT OUTER JOIN GetFirst AS f ON f.Id=a.Id WHERE f.Id IS NULL ORDER BY Name ) SELECT * FROM GetNext; RETURN @records; END GO </code></pre> <p>The procedure above returns the total number of records additionally and you can use it to assign <code>totalRecords</code> value.</p> <p>If you would use above code in combination with <code>sp_executesql</code> you can easy modify the code to include <code>ORDER BY</code> in all <code>SELECT TOP</code> statements so that the returned values will corresponds to sorting order requested by the user in jqGrid.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload