Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Bom dia!
Postei algumas dúvidas aqui e na maioria da vezes consegui a resposta desejada, e por isso sempre sou grato a vocês. Bom, mas o que me levou aqui novamente, é que assumi a responsabilidade de dar manutenção em um novo sistema, onde existe uma consulta, que possui alguns parâmetros, porém estes não estão funcionando como deveriam, aliás, nem estão funcionando...
Trata-se de um relatório de crítica de processos jurídicos, onde usuário tem a opção de escolher por:
-Agência
-Matéria
-Relatório(Tipo de crítica ou inconsistência)
O usuário escolhe essas opções em uma página ASP, que passa somente números para stored procedure (sp_rptCriticaInconsistencias), aí começam meus problemas...
Em suma, a SP quando é executada traz a base toda e não consegue filtrar pelos parâmetros repassados pelo usuário... Desta forma, peço a vocês um apoio técnico para tentar decifrar onde existe o erro no script...
Vou postar o script para vocês darem uma olhada e sugestões...
Agradeço a todos que poderem ajudar, pois meu prazo tá para expirar!!!
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER procedure dbo.sp_rptCriticaInconsistencias
-- exec sp_rptCriticaInconsistencias 0,5,0,0,0,0,0
@numSup_Cod integer = null -- GEREG
,@numAgn_Cod integer = null -- Agencia
,@numAdv_Cod integer = null -- Advogado
,@numMat_Cod integer = null -- Materia
,@numCom_Cod integer = null -- Comarca
,@numAct_Cod integer = null -- Acao
,@sinRel_Cod integer = null -- Tipo de relatorio
as
create table #tbl01
(
[vchtbl_incon] [varchar] (50)
,[sintbl_gercod] [smallint]
,[vchtbl_gerdes] [varchar] (100)
,[sintbl_agncod] [smallint]
,[vchtbl_agndes] [varchar] (100)
,[numtbl_ficha] [numeric] (10)
,[sintbl_piseq] [smallint]
,[vchtbl_proce] [varchar] (50)
-- ,[chrtbl_colname] [varchar] (1000)
-- ,[vchtbl_name] [varchar] (1000)
,[sintbl_matcod] [smallint]
,[vchtbl_matdes] [varchar] (100)
)
declare
@vchSQL varchar(8000)
,@vchCab varchar(1000)
,@vchPar varchar(1000)
,@vchGrp varchar(1000)
,@vchStr varchar(8000)
set @vchSQL = ''
set @vchCab = ''
set @vchPar = ''
set @vchGrp = ''
set nocount on
if @numSup_Cod is null or @numSup_Cod = 0 --or @numSup_Cod = '' -- GEREG
set @numSup_Cod = 999
if @numAgn_Cod is null or @numAgn_Cod = 0 --or @numAgn_Cod = '' -- Agencia
set @numAgn_Cod = 999
if @numAdv_Cod is null or @numAdv_Cod = 0 --or @numAdv_Cod = '' -- Advogado
set @numAdv_Cod = 999
if @numMat_Cod is null or @numMat_Cod = 0 --or @numMat_Cod = '' -- Materia
set @numMat_Cod = 999
if @numCom_Cod is null or @numCom_Cod = 0 --or @numCom_Cod = '' -- Comarca
set @numCom_Cod = 999
if @numAct_Cod is null or @numAct_Cod = 0 --or @numAct_Cod = '' -- Acao
set @numAct_Cod = 999
if @sinRel_Cod is null or @sinRel_Cod = 0 --or @sinRel_Cod = '' -- Tipo de relatorio
set @sinRel_Cod = 999
-- Cabecalho do Select
set @vchCab = ' a.tsi_codigo, d.tst_nome, a.tagi_codigo, c.tagi_nome, a.pl_ficha, a.pi_seq, a.pt_processo, a.tmi_codigo, e.tmt_nome '
-- Parametros de Pesquisa
set @vchPar = ' and a.tagi_codigo between case when ' + cast(@numAgn_Cod as char(3)) + ' <> 999 then ' + cast(@numAgn_Cod as char(3)) + ' else 0 end and ' + cast(@numAgn_Cod as char(3))
+ ' and a.tsi_codigo between case when ' + cast(@numSup_Cod as char(3)) + ' <> 999 then ' + cast(@numSup_Cod as char(3)) + ' else 0 end and ' + cast(@numSup_Cod as char(3))
+ ' and a.tmi_codigo between case when ' + cast(@numMat_Cod as char(3)) + ' <> 999 then ' + cast(@numMat_Cod as char(3)) + ' else 0 end and ' + cast(@numMat_Cod as char(3))
+ ' and a.tmi_codigo between case when ' + cast(@numCom_Cod as char(3)) + ' <> 999 then ' + cast(@numCom_Cod as char(3)) + ' else 0 end and ' + cast(@numCom_Cod as char(3))
-- Agrupamento
set @vchGrp = ' group by a.tsi_codigo, d.tst_nome, a.tagi_codigo, c.tagi_nome, a.pl_ficha, a.pi_seq, a.pt_processo, a.tmi_codigo, e.tmt_nome '
-- Relatorios
-- Sem Envolvidos
if @sinRel_Cod = 4 or @sinRel_Cod = 999
begin
set @vchSQL = @vchSQL + ' select ''Envolvidos'' as Inconsistência,' + @vchCab
set @vchSQL = @vchSQL + ' from [processos] a full outer join [envolvidos] b on a.pl_ficha=b.pl_ficha and a.pi_seq=b.pi_seq '
+ ' full outer join agencias c on a.tagi_codigo = c.tagi_codigo '
+ ' full outer join superintendencias d on a.tsi_codigo = d.tsi_codigo '
+ ' full outer join materias e on a.tmi_codigo = e.tmi_codigo '
+ ' where b.pl_ficha is null and a.tagi_codigo > 0 '
set @vchSQL = @vchSQL + @vchPar
set @vchSQL = @vchSQL + @vchGrp
end
-- Sem Advog Vinculado
if @sinRel_Cod = 1 or @sinRel_Cod = 999
begin
if @sinRel_Cod = 999
begin
set @vchSQL = @vchSQL + ' union '
end
set @vchSQL = @vchSQL + ' select ''Advogado Vinculado'' as Inconsistência,' + @vchCab
set @vchSQL = @vchSQL + ' from [processos] a full outer join [advog vinculados] b on a.pl_ficha=b.pl_ficha and a.pi_seq=b.pi_seq '
+ ' full outer join agencias c on a.tagi_codigo = c.tagi_codigo '
+ ' full outer join superintendencias d on a.tsi_codigo = d.tsi_codigo '
+ ' full outer join materias e on a.tmi_codigo = e.tmi_codigo '
+ ' where b.pl_ficha is null and a.tagi_codigo > 0 '
set @vchSQL = @vchSQL + @vchPar
set @vchSQL = @vchSQL + @vchGrp
end
-- Cadastro Duplicado
if @sinRel_Cod = 2 or @sinRel_Cod = 999
begin
if @sinRel_Cod = 999
begin
set @vchSQL = @vchSQL + ' union '
end
set @vchSQL = @vchSQL + ' select ''Cadastro Duplicado'' as Inconsistência,' + @vchCab
set @vchSQL = @vchSQL + ' from [processos] a full outer join [processos] b on a.pt_processo=b.pt_processo '
+ ' full outer join agencias c on a.tagi_codigo = c.tagi_codigo '
+ ' full outer join superintendencias d on a.tsi_codigo = d.tsi_codigo '
+ ' full outer join materias e on a.tmi_codigo = e.tmi_codigo '
+ ' where a.pl_ficha > 0 and a.pi_seq=0 and a.tagi_codigo > 0 or a.pt_processo is null or a.pt_processo = '''''
-- + ' where a.pl_ficha <> b.pl_ficha and a.pi_seq=0 and a.tagi_codigo > 0 '
set @vchSQL = @vchSQL + @vchPar
set @vchSQL = @vchSQL + @vchGrp + ' having count(b.pl_ficha) > 1 '
end
-- Sem CPF_CNPJ
if @sinRel_Cod = 3 or @sinRel_Cod = 999
begin
if @sinRel_Cod = 999
begin
set @vchSQL = @vchSQL + ' union '
end
set @vchSQL = @vchSQL + ' select ''Cpf/Cnpj'' as Inconsistência,' + @vchCab
set @vchSQL = @vchSQL + ' from [processos] a full outer join [envolvidos] b on a.pl_ficha=b.pl_ficha and a.pi_seq=b.pi_seq '
+ ' full outer join agencias c on a.tagi_codigo = c.tagi_codigo '
+ ' full outer join superintendencias d on a.tsi_codigo = d.tsi_codigo '
+ ' full outer join materias e on a.tmi_codigo = e.tmi_codigo '
+ ' where (b.et_cpf_cnpj is null or b.et_cpf_cnpj = 0) and a.tagi_codigo > 0 and b.et_nome <> ''Banco da Amazônia S.A.'''
set @vchSQL = @vchSQL + @vchPar
set @vchSQL = @vchSQL + @vchGrp
end
-- Sem Tramite
if @sinRel_Cod = 6 or @sinRel_Cod = 999
begin
if @sinRel_Cod = 999
begin
set @vchSQL = @vchSQL + ' union '
end
set @vchSQL = @vchSQL + ' select ''Tramite'' as Inconsistência,' + @vchCab
set @vchSQL = @vchSQL + ' from [processos] a full outer join [tramitacao] b on a.pl_ficha=b.pl_ficha and a.pi_seq=b.pi_seq '
+ ' full outer join agencias c on a.tagi_codigo = c.tagi_codigo '
+ ' full outer join superintendencias d on a.tsi_codigo = d.tsi_codigo '
+ ' full outer join materias e on a.tmi_codigo = e.tmi_codigo '
+ ' where b.pl_ficha is null and a.tagi_codigo > 0 '
set @vchSQL = @vchSQL + @vchPar
set @vchSQL = @vchSQL + @vchGrp
end
-- Cadastro Inconsistente
if @sinRel_Cod = 5 or @sinRel_Cod = 999
begin
set @vchStr = ' insert into #tbl01 exec sp_CriticaIncProcessos '
+ cast(@numSup_Cod as char(3)) + ', ' + cast(@numAgn_Cod as char(3)) + ', '
+ cast(@numAdv_Cod as char(3)) + ', ' + cast(@numMat_Cod as char(3))+ ', '
+ cast(@numCom_Cod as char(3)) + ', ' + cast(@numAct_Cod as char(3))+ ', '
+ cast(@sinRel_Cod as char(3))
exec(@vchStr)
end
if @vchSQL <> ''
set @vchSQL = @vchSQL + ' order by a.tsi_codigo, a.tagi_codigo, a.pt_processo,a.pl_ficha '
--select @vchSQL
if @vchSQL <> ''
begin
set @vchSQL = ' insert into #tbl01 ' + @vchSQL
exec(@vchSQL)
end
select * from #tbl01order by
sintbl_gercod
--,vchtbl_gerdes
,sintbl_agncod
--,vchtbl_agndes
--,sintbl_matcod
,vchtbl_matdes
,numtbl_ficha
,sintbl_piseq
,vchtbl_proce
,vchtbl_incon
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[#tbl01]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[#tbl01]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
Carregando comentários...