Search
 
SCRIPT & CODE EXAMPLE
 

SQL

limit and offset in stored procedure mssql

CREATE PROCEDURE [sp_Mk]
 @page INT,
 @size INT,
 @sort nvarchar(50) ,
 @totalrow INT  OUTPUT
AS
BEGIN
    DECLARE @offset INT
    DECLARE @newsize INT
    DECLARE @sql NVARCHAR(MAX)

    IF(@page=0)
      BEGIN
        SET @offset = @page
        SET @newsize = @size
       END
    ELSE 
      BEGIN
        SET @offset = @page*@size
        SET @newsize = @size-1
      END
    SET NOCOUNT ON
    SET @sql = '
     WITH OrderedSet AS
    (
      SELECT *, ROW_NUMBER() OVER (ORDER BY ' + @sort + ') AS ''Index''
      FROM [dbo].[Mk] 
    )
   SELECT * FROM OrderedSet WHERE [Index] BETWEEN ' + CONVERT(NVARCHAR(12), @offset) + ' AND ' + CONVERT(NVARCHAR(12), (@offset + @newsize)) 
   EXECUTE (@sql)
   SET @totalrow = (SELECT COUNT(*) FROM [Mk])
END
Comment

PREVIOUS NEXT
Code Example
Sql :: create table kusto 
Sql :: mysql get table column names and data types 
Sql :: delete account in flask and sqlalchemy 
Sql :: Host ' is not allowed to connect to this MySQL server 
Sql :: SELECT ALL TABLE INFO 
Sql :: mysql average from two table 
Sql :: triggers in mysql example 
Sql :: oracle all columns 
Sql :: mysql varchar length 
Sql :: mysql uuid 
Sql :: DIFFERENCE BETWEEN 2 COLN IN SQL 
Sql :: oracle convert hours to minutes 
Sql :: truncate table sql server foreign key 
Sql :: take sql dump in to file 
Sql :: difference between left outer join and left join in sql 
Sql :: REMOVE DATE FROM DATE TIME SQL SERVER 
Sql :: convert .mdf to .bak 
Sql :: select top values sql 
Sql :: sql full outer join 
Sql :: drop table oracle 
Sql :: postgresql fastapi sqlmodel example 
Sql :: mysql large import 
Sql :: where name ends in SQL 
Sql :: add column sql 
Sql :: sql remove duplicates based on column 
Sql :: one to many sql 
Sql :: datatables server side filter where clause 
Sql :: ORA-06502: PL/SQL: numeric or value error: character string buffer too small 
Sql :: lumen 
Sql :: sql offfset 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =