store procedure sql server 2005

alfinete

Power Member
Código:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
-- Author:        <Author,,Name>
-- Create date: <Create Date,,>
-- Description:    <Description,,>
-- =============================================

ALTER PROCEDURE [dbo].[test_limit] 

@tptribunal  int = null,
@tpprocesso  int = null,
@tpassunto  int = null,
@datainicio  varchar(6) = null,
@datafim  varchar(6) = null,
@palavrachave varchar(256)=null,
@inicio int,
@fim int
--@res int OUT

AS
declare @QUERY NVARCHAR(1200)
declare @arg smallint

--SET @res=0
set @arg = 0

BEGIN

SET NOCOUNT ON;

SET @QUERY ='

SELECT NumeroProcesso,SiglaTP,SiglaTT,SiglaA,DataDecisao,Documento  
  FROM(SELECT  ROW_NUMBER() OVER (ORDER BY p.DataDecisao asc)
       AS Row, p.NumeroProcesso,tp.Sigla as SiglaTP,tt.Sigla as SiglaTT,
       a.Sigla as SiglaA,p.DataDecisao,p.Documento FROM dbo.TabProcesso p

       JOIN dbo.TabTipoProcesso tp ON tp.IdTipoProcesso = p.TipoProcesso    
       JOIN dbo.TabTribunal tt ON tt.IdTribunal = p.Tribunal
       JOIN dbo.TabAssunto a ON a.IdAssunto = p.Assunto

  ' 
---------------------------------DDL'S -----------------------------------
IF @tptribunal != 0
    if @arg = 0
        begin
            SET @QUERY = @QUERY + 'where tribunal = @tptribunal '
            set @arg = @arg + 1
        end
    else
        begin
            SET @QUERY = @QUERY + 'and tribunal = @tptribunal '
        end 

IF @tpprocesso != 0
    if @arg = 0
        begin
            SET @QUERY = @QUERY + 'where tipoprocesso = @tpprocesso '
            set @arg = @arg + 1
        end
    else
        begin
            SET @QUERY = @QUERY + 'and tipoprocesso = @tpprocesso '
        end 


IF @tpassunto != 0
    if @arg = 0
        begin
            SET @QUERY = @QUERY + 'where assunto = @tpassunto '
            set @arg = @arg + 1
        end
    else
        begin
            SET @QUERY = @QUERY + 'and assunto = @tpassunto '
        end 
------------------------------DDL DATAS -----------------------------------


if (@datainicio !='') and (@datafim !='')

    if @arg = 0
        begin
            SET @QUERY = @QUERY + 'where ( substring(convert(varchar(20), datadecisao, 112),1,6) >= @datainicio and
                                          substring(convert(varchar(20), datadecisao, 112),1,6) <= @datafim
                                         )'
            set @arg = @arg + 1
        end
    else
        begin
            SET @QUERY = @QUERY + 'and (substring(convert(varchar(20), datadecisao, 112),1,6) >= @datainicio)
                                   and (substring(convert(varchar(20), datadecisao, 112),1,6) <= @datafim)'
        end 


----------------------------- palavra chave ----------------------------------
if (@palavrachave !='')

if @arg = 0
        begin
            SET @QUERY = @QUERY + 'where palavrachave like @palavrachave'
            set @arg = @arg + 1
        end
    else
        begin
            SET @QUERY = @QUERY + 'and palavrachave like @palavrachave'
        end 


SET @QUERY = @QUERY + ')AS LogWithRowNumbers WHERE Row >= @inicio AND Row <= @fim'


EXECUTE sp_executesql  @QUERY, N' @tptribunal int,@tpprocesso int,@tpassunto int,@datainicio varchar(6),@datafim varchar(6),@palavrachave varchar(256),@inicio int,@fim int ',
               @tptribunal,@tpprocesso,@tpassunto,@datainicio,@datafim,@palavrachave,@inicio,@fim

select @query
--SET @res=@@ROWCOUNT

END

tenho esta store , mas quando fasso pesquiza quer geral quer não geral devolve-me sempre o numero total de registos que existem na tabela, e eu queria que me devolvesse apaenas o numero de registos que estão a ser pesquisados

agradecia uma ajuda
 
Back
Topo