OFFSET does not work on SQL Server

Asked

Viewed 37 times

0

I developed the sql side paging in my SQL Server 2014, when I played in production gave the problem with the OFFSET because the server has sql 2008.

I tried to implement otherwise but did not succeed.

Does anyone know any other method?

Follows the code

Alter PROCEDURE [dbo].[BGC_SP_LEAD_SEARCH_DAL_NOVOPARTNER] (    
 @ID_LEAD NUMERIC(18) = NULL,  
 @NR_CNPJ NUMERIC(18) = NULL,  
 @DS_NOME VARCHAR(100) = NULL,  
 @ST_STATUS CHAR(1) = NULL,  
 @DS_BSPDIS VARCHAR(100) = NULL,  
 @ID_PESSOA NUMERIC(18) = NULL,  
 @ID_GERENTE VARCHAR(50) = NULL,  
 @ID_TIPOPROJETO NUMERIC = NULL,  
 @ID_AREANEGOCIOS INT = NULL,  
 @ID_GERENTENEGOCIOS VARCHAR (50) = NULL,  
 @FL_NOMEADA INT = NULL,  
 @ID_USERLOGADO VARCHAR(50) = NULL ,
 @ID_AREANEGOCIOS_LEITOR INT = NULL,  
 @ID_GERENTENEGOCIOS_LEITOR VARCHAR(50) = NULL,
 @pageNumber int,
 @RowsPerPage int  
) AS  
BEGIN  
 /*  
  PROCEDURE BUSCA POR VALORES COMO LEAD_SEARCH porém se o status for null  
  ela irá mostrar apenas Leads com o status:  
   > 'D' - Designação de Responsável;  
   > 'A' - Em Análise; e   
   > 'L' - Liberado  
 */  
 DECLARE @ID_PESSOA_USERLOGADO VARCHAR(50) = NULL  
 , @MASTER_USERLOGADO VARCHAR(1) = NULL  
 , @ID_GRUPO_USERLOGADO VARCHAR(50) = NULL  

  SELECT @ID_PESSOA_USERLOGADO = U.ID_PESSOA,  
    @MASTER_USERLOGADO = U.ST_PERFIL_MASTER_BID,  
    @ID_GRUPO_USERLOGADO = G.ID_GRUPOACESSO  
  FROM BCS_USUARIO U  
  LEFT JOIN BCS_GRUPOUSUARIOPESSOA G ON U.ID_USUARIO = G.ID_USUARIO  
  WHERE U.ID_USUARIO = @ID_USERLOGADO  

 SELECT DISTINCT   
  LE.ID_LEAD,  
  LE.ID_TIPOPROJETO,  
  TP.DS_TIPOPROJETO,  
  LE.DT_PREVISTA,  
  LE.DT_WORKFLOW,  
  LE.DS_DESENHO,  
  LE.NR_VOLUMETRIA,  
  LE.ST_RECURSOS,  
  LE.ID_GERENTE_VERTICAL,  
  LE.ID_GERENTE_PROJETO,  
  ISNULL(LE.NR_CNPJ,PCLI.NR_CPFCNPJ) AS NR_CNPJ,  
  LE.ID_BSPDIS,  
  LE.DS_JUSTIFICATIVA,  
  LE.ST_STATUS,  
  ISNULL(CL.DS_NOME,PCLI.DS_NOME) AS DS_NOME,  
  ISNULL(CL.DS_RAZAOSOCIAL,PCLI.DS_RAZAOSOCIAL) AS DS_RAZAOSOCIAL,  
  ISNULL(CD.DS_NOME,CD.DS_RAZAOSOCIAL) AS DS_BSPDIS,  
  LE.ID_RESPONSAVEL,  
  US.DS_USUARIO,  
  LE.ID_AREANEGOCIOS,  
  LE.ID_GERENTENEGOCIOS,  

  LE.ID_AREANEGOCIOS_LEITOR,  
  LE.ID_GERENTENEGOCIOS_LEITOR, 


  CO.NR_CNPJ AS NR_CNPJ_CONTA,  
  CO.FL_NOMEADA,  
  null DS_CONFIDENCIAL_LEXMARK,  
  EST.ID_ESTADO DS_BSPDIS_ESTADO,  
  GN.DS_USUARIO GERENTE_NEGOCIOS,  
  AN.DS_NOME AREA_NEGOCIOS,
  GNL.DS_USUARIO GERENTE_NEGOCIOS_LEITOR,  
  ANL.DS_NOME AREA_NEGOCIOS_LEITOR    

 FROM BGC_LEAD LE  
 INNER JOIN BCS_USUARIO  US ON LE.ID_RESPONSAVEL = US.ID_USUARIO  
 LEFT JOIN BCS_GRUPOUSUARIOPESSOA GUP ON US.ID_USUARIO = GUP.ID_USUARIO  
 LEFT JOIN BGC_TIPOPROJETO TP ON LE.ID_TIPOPROJETO = TP.ID_TIPOPROJETO   
 INNER JOIN BCT_PESSOA  CD ON LE.ID_BSPDIS = CD.ID_PESSOA  
 LEFT JOIN BGC_TMP_CLIENTE CL ON CL.ID_CLIENTE = LE.ID_CLIENTE  
 LEFT JOIN BCT_PESSOA  PCLI ON PCLI.ID_PESSOA = LE.ID_PESSOA_CLIENTE  
 LEFT JOIN BGC_CONTA CO ON LE.NR_CNPJ = CO.NR_CNPJ   
 INNER JOIN BCT_PESSOA PE ON LE.ID_BSPDIS = PE.ID_PESSOA  
 INNER JOIN BCT_PESSOATIPO PET ON PE.ID_PESSOA = PET.ID_PESSOA  
 LEFT JOIN BGC_GRUPO GR ON PET.ST_TIPO = GR.ST_TIPO  
 LEFT JOIN BCT_GRUPOPESSOA GP ON GR.ID_GRUPO = GP.ID_GRUPO  
  AND PE.ID_PESSOA = GP.ID_PESSOA  
 LEFT JOIN BGC_GRUPO_GERENTE GG ON GR.ID_GRUPO = GG.ID_GRUPO    
 LEFT JOIN BSP_ADDRESSBOOK AB ON CD.ID_CUSTOMERNUMBER = AB.ID_CUSTOMERNUMBER    
 LEFT JOIN BCT_ESTADO EST ON AB.DS_ESTADO = EST.ID_ESTADO   
 LEFT JOIN BCS_USUARIO GN ON LE.ID_GERENTENEGOCIOS = GN.ID_USUARIO    
 LEFT JOIN BGC_AREANEGOCIOS AN ON LE.ID_AREANEGOCIOS = AN.ID_AREANEGOCIOS 

 LEFT JOIN BCS_USUARIO GNL ON LE.ID_GERENTENEGOCIOS_LEITOR = GNL.ID_USUARIO    
 LEFT JOIN BGC_AREANEGOCIOS ANL ON LE.ID_AREANEGOCIOS_LEITOR = ANL.ID_AREANEGOCIOS


 WHERE ---- @ID_LEAD ----  
  LE.ID_LEAD = ISNULL(@ID_LEAD,ID_LEAD)  
  ---- @ST_STATUS ----  
  AND (  
   ( @ST_STATUS IS NOT NULL AND LE.ST_STATUS = @ST_STATUS  )  
   OR ( @ST_STATUS IS NULL AND LE.ST_STATUS IN ('D', 'A', 'L') )  
  )  
  ---- @NR_CNPJ ----  
  AND ( CL.NR_CNPJ LIKE CAST(@NR_CNPJ AS VARCHAR(18)) + '%'
   OR PE.NR_CPFCNPJ LIKE CAST(@NR_CNPJ AS VARCHAR(18)) + '%'
   OR @NR_CNPJ IS NULL  
  )  
  ---- @DS_BSPDIS ----  
  AND  (  CD.DS_NOME LIKE '%' + @DS_BSPDIS +'%'  
   OR CD.DS_RAZAOSOCIAL LIKE '%' + @DS_BSPDIS +'%'  
   OR @DS_BSPDIS IS NULL  
  )  
  AND ID_BSPDIS = ISNULL(@ID_PESSOA,ID_BSPDIS)  
  AND ( CL.DS_RAZAOSOCIAL LIKE '%' + @DS_NOME +'%'  
   OR CL.DS_NOME LIKE '%' + @DS_NOME + '%'  
   OR PE.DS_RAZAOSOCIAL LIKE '%' + @DS_NOME +'%'  
   OR PE.DS_NOME LIKE '%' + @DS_NOME + '%'  
   OR @DS_NOME IS NULL  
  )  
  AND ( LE.ID_AREANEGOCIOS = @ID_AREANEGOCIOS  
   OR @ID_AREANEGOCIOS IS NULL   
  )  
  AND ( LE.ID_GERENTENEGOCIOS = @ID_GERENTENEGOCIOS  
   OR @ID_GERENTENEGOCIOS IS NULL   
  )  
  ------------------------------------------------------------------------Leitor
  AND ( LE.ID_AREANEGOCIOS_LEITOR = @ID_AREANEGOCIOS_LEITOR  
   OR @ID_AREANEGOCIOS_LEITOR IS NULL   
  )  
  AND ( LE.ID_GERENTENEGOCIOS_LEITOR = @ID_GERENTENEGOCIOS_LEITOR  
   OR @ID_GERENTENEGOCIOS_LEITOR IS NULL   
  )
  ---------------------------------------------------------------------------------
  AND (CO.FL_NOMEADA = @FL_NOMEADA  
   OR ( CO.FL_NOMEADA IS NULL  
    AND  @FL_NOMEADA =2   
   )    
   OR (@FL_NOMEADA IS NULL)  
  )   
  AND ( LE.ID_TIPOPROJETO = @ID_TIPOPROJETO  
   OR @ID_TIPOPROJETO  is null  
  )  
  AND ( @ID_USERLOGADO IS NULL   
   OR GG.ID_GERENTE = @ID_USERLOGADO -- GERENTE DO GRUPO ASSIGNACAO   
   OR  LE.ID_BSPDIS = @ID_PESSOA_USERLOGADO -- É DO MESMO CLIENTE QUE CRIOU O LEAD   
   OR  LE.ID_RESPONSAVEL like '%' + @ID_USERLOGADO + '%' -- É O CRIADOR DO LEAD   
   OR  LE.ID_GERENTENEGOCIOS = @ID_USERLOGADO -- GERENTE DE NOGOCIOS DO LEAD   
   OR  LE.ID_GERENTENEGOCIOS_LEITOR = @ID_USERLOGADO
   OR  @ID_USERLOGADO IN (SELECT ID_VERTICAL FROM BGC_AREANEGOCIOS WHERE ID_AREANEGOCIOS = LE.ID_AREANEGOCIOS) --GERENTE VERTICAL   
   OR  @ID_USERLOGADO IN (SELECT ID_GERENTE FROM BGC_TIPO_GERENTE_LEAD WHERE ID_TIPO_GERENTE LIKE 'C')   
   OR  @ID_USERLOGADO IN (  
    SELECT usr_prf.ID_USUARIO   
    FROM BCS_PERFIL prf, BCS_USUARIOPERFIL usr_prf   
    WHERE prf.ID_PERFIL = usr_prf.ID_PERFIL   
    AND prf.DS_PERFIL = 'Gerente Comercial Canal'   
    OR  prf.DS_PERFIL = 'Administrador - Gestão de Canais'  
   )   
   OR @MASTER_USERLOGADO = 'S'   
   -- faz o join do US pelo ID_RESPONSAVEL  
   -- ou seja, o grupo US.ID_GRUPOACESSO é referente ao grupo de acesso do ID_RESPONSAVEL  
   --   
   --OR US.ID_GRUPOACESSO = @ID_GRUPO_USERLOGADO   
   OR (GUP.ID_GRUPOACESSO = @ID_GRUPO_USERLOGADO )--OR @ID_GRUPO_USERLOGADO IS NULL )  
  )  
 ORDER BY    DS_BSPDIS, DS_NOME, DS_RAZAOSOCIAL, LE.DT_PREVISTA  


       OFFSET ( ( @pageNumber - 1 ) * @RowsPerPage ) ROWS
             FETCH NEXT @RowsPerPage ROWS ONLY;

end 

@PAGERNUMBER = Page number.

RowsPerPage = Records displayed per page.

  • SQL server 2014 or 2008? Your title is as 2008 and the text is 2014.

  • @Everson I did in 2014 but the server is 2008

  • @Sorack whenever I implement this results come duplicated

  • @Juniortorres: In the book Inside Microsoft SQL Server 2008: T-SQL Querying you find excerpt that deals with paging (paging). Pages 547 to 552.

  • @Juniortorres: If the production server is version 2008, the suggestion is that either you install an instance with the same version on the development computer or, in the development SQL Server 2014, you set the 100 compatibility level for the test databases (similar to those running in production in version 2008).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.