Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

acfelipesilva

Parâmetros não funcionam

Recommended Posts

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:

 

- Gerência

-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 #tbl01
group by 
sintbl_gercod
,vchtbl_gerdes
,sintbl_agncod
,vchtbl_agndes
,sintbl_matcod
,vchtbl_matdes
,numtbl_ficha
,sintbl_piseq
,vchtbl_proce
,vchtbl_incon

order 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

Compartilhar este post


Link para o post
Compartilhar em outros sites

"aí começam meus problemas..."

 

 

Entao vamos piora-los (brincadeira) rs http://forum.imasters.com.br/public/style_emoticons/default/natal_tongue.gif

 

 

seguinte.

como você msm pode reparar, sua proc esta estrurada de modo que um select gera outro.

Tenta rodar pegando os resultados de item a item para filtrar os gargalos.

Exemplo:

qual o resultado do "Cabecalho do Select"?

-- Parametros de Pesquisa

-- Agrupamento

-- Relatorios

 

Tenta fazer parte a parte para você msm filtrar o resultado e ver onde exatamente nao esta sendo filtrado. Isso facilita sua interpretacao e a de quem le tb, pq sua proc tem muitas linhas e muitas pessoas nao tem ambiente para testar (meu caso por exemplo)

 

Tenta ai e posta tah http://forum.imasters.com.br/public/style_emoticons/default/natal_biggrin.gif

 

abs

Compartilhar este post


Link para o post
Compartilhar em outros sites

Muito obrigado amigo por se interessar em me ajudar, bom com base no que você falou, achei os seguintes detalhes:

Se eu mandar rodar a SP do jeito que tá recebo uma msg que o comando está execuatdo com sucesso, o que sabemos que não é bem verdade...

Fiz vários testes, e constatei que a consulta funciona para todos os combos, desde que o combo (ou melhor o parâmetro) @sinRel_Cod não venha vazio... Isto é, hj a consulta funciona 100% desde que este campo o usuário preencha com alguma coisa, e ele nunca pode deixar vazio...

Não posso tornar esse campo obrigatório infelizmente...

Então tenho que dar um jeito de fazê-lo funcionar, rs, bom esse combo no ASP está assim:

 

<OPTION value=0>TODOS</option>

<OPTION value=1>ADVOGADO VINCULADO</option>

<OPTION value=2>CADASTRO DUPLICADO</option>

<OPTION value=3>CPF/CNPJ</option>

<OPTION value=4>ENVOLVIDOS</option>

<OPTION value=5>INFORMAÇÕES GERAIS</option>

<OPTION value=6>TRAMITE</option>

Quando faço o teste no sqlserver "exec sp_rptCriticaInconsistencias 0,0,0,0,0,0,4", ele executa legalzinho os comandos, mas quando deixo o campo que recebe @sinRel_Cod vazio, tipo "exec sp_rptCriticaInconsistencias 1,5,0,0,0,0,0"... Pronto, não funciona mais nenhum parâmetro, queria saber pq isso ocorre? E onde devo dar a "martelada" milagrosa?

 

A grande dificuldade tb, é q o código não fui eu que fiz, e to tendo dificuldade para entender a lógica do carinha que montou...

No código ele cria uma tabela temporária, onde dados são adicionados a ela, e por fim ele gera a consulta final...

 

Cara, se tu poder me ajudar , agardeço muito... Na verdade, se podermos mudar um pouco a lógica na hora de fazer o filtro utilizando os parâmetros já ajuda tb... O que importa é conseguir fazer isso funcionar, hehehe... Já to na minha 2ª noite e não saiu nada...

 

Grato,

 

Augusto

 

</SELECT>

Compartilhar este post


Link para o post
Compartilhar em outros sites

acfelipesilva

 

mexer em lógica eh bem complicado, ainda mais que nao foi você que desenvolveu e como seu problema eh somente um parametro, nao sei se vale a pena mexer.

você pode colocar um "if" antes para verificar se o @sinRel_Cod for vazio.

se for vazio você iguala a zero, nao pode ser feito?! ou você poe no asp ele como obrigatorio. Pelo que pude perceber o cara tem q escolher este campo, jah que eh um tipo de relatorio. Pode nao ser o modo correto, mas você nao vai ter mais uma noite ai, e pode ganhar tempo para pensar em alterar esta procedure.

 

Abs

Compartilhar este post


Link para o post
Compartilhar em outros sites

Quando executado a SP " exec sp_rptCriticaInconsistencias 2,0,0,0,0,0,0 " os parametros não funcionam e ainda recebeo a msg abaixo:

 

Warning: Null value eliminated from aggregate.

Porém se vocês notarem, eu faço um tratamento para isso não acontecer, que não está funcionando e ainda não sei o pq?

 

Se executo exec sp_rptCriticaInconsistencias 2,0,0,0,0,0,4 , a consulta funciona, e os filtros voltam a funcionar normalmente, trata-se do ultimo campo (onde tá 4), trata-se do parametro @sinRel_Cod , que indica qual tipo de inconsistencia acontece nos processos juridicos, ainda não consegui decifrar esse enigama, caso possam me ajudar, eu agradeço muito!

 

Augusto

Compartilhar este post


Link para o post
Compartilhar em outros sites

felipe,

qdo você poe 999 ele te retorna oq?

o estranho eh ele filtrar com o valor 4, pq como vi sua proc, ele nao tem este valor para ser passado pelo @sinRel_Cod.

Roda com 1, 2, 3, 5, 6 e 999 para ver se retorna correto. se nao retornar, informe kais destes parametros nao retornou.

 

abs

Compartilhar este post


Link para o post
Compartilhar em outros sites

Junior,

 

tens um email para que eu possa te repassar o script, e um doc com as querys resultantes e um texto mais explicativo...?

 

grato,

 

Augusto

Compartilhar este post


Link para o post
Compartilhar em outros sites

te mandei em pvt.

Assim que mandar o email ou se conseguir resolver antes o tópico nos informe tah :)

 

abs

Compartilhar este post


Link para o post
Compartilhar em outros sites

Junior,

 

Cara não sei que droga o cara tava usando quando fez esse script, rapaz é bem complexo né...

O que eu entendi é o seguinte:

1 - O cara segregou todo o script, montou um cabeçalho do select, montou parte só para passar parâmetros, e por ultimo a parte do agrupamento;

2 - Depois, o doido, separou uma parte em especifico onde ele colhe as inconsistencias através de varios selects, sempre alinhados com o cabeçalho lá de cima, certo?;

3 - Tudo funciona perfeitamente, desde que sempre seja escolhido o parâmetro @sinRel_Cod não seja nulo ou diferente de zero, tipo se eu mandar executar assim: exec sp_rptCriticaInconsistencias 2,5,0,5,0,0,4, ele vai cair no select onde a inconsistencia é "Envolvidos", e obedecendo os outros critérios, ou seja tudo funciona normalmente...

4 - Quando o parâmetro @sinRel_Co vem nulo ou zero, e executo assim: exec sp_rptCriticaInconsistencias 2,5,0,5,0,0,0 , nada funciona e tudo indica que a procedure nao entra em laço nenhum, quando na verdade ela deveria entrar no laço sim, trazendo como resultados todas as inconsistencias através do union;

 

Mesmo assim tá muito complexo pra mim, e tenho que dar a martelada no código sem mexer na estrutura...

 

caso alguém possa ajudar, vou postar os códigos da procedure:

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



ALTER		procedure dbo.sp_rptCriticaInconsistencias 

-- exec sp_rptCriticaInconsistencias 2,5,0,5,0,0,4

 @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)
 ,[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))

	-- 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
--select @vchStr
print @vchSQL
--print @vchStr

if @vchSQL <> '' 
   begin
	 set @vchSQL = 'insert into #tbl01 ' + @vchSQL
	 exec(@vchSQL)
   end


select * from #tbl01
group by 
 sintbl_gercod
,vchtbl_gerdes
,sintbl_agncod
,vchtbl_agndes
,sintbl_matcod
,vchtbl_matdes
,numtbl_ficha
,sintbl_piseq
,vchtbl_proce
,vchtbl_incon

order by
 sintbl_gercod
,sintbl_agncod
,vchtbl_matdes
,numtbl_ficha
,sintbl_piseq
,vchtbl_proce
,vchtbl_incon

print @vchSQL

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

Ele chama uma outra procedure que é:

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO




ALTER		 procedure [dbo].sp_CriticaIncProcessos 
-- exec sp_CriticaIncProcessos 2,0,0,0,0,0,0

	 @numSup_Cod numeric(3)=999
	,@numAgn_Cod numeric(3)=999
	,@numAdv_Cod numeric(3)=999
	,@numMat_Cod numeric(3)=999
	,@numCom_Cod numeric(3)=999
	,@numAct_Cod numeric(3)=999
	,@sinRel_Cod numeric(3)=999

as
declare  @vchcol_name varchar(1000)
	,@xtype int
		,@cmd  varchar(8000) 
		,@rows int
		,@cont int
	,@str  varchar (20)
		,@aux  varchar(50)
		,@vchtbl_name varchar(1000)

create table #tbl
(

  [numtbl_ficha]	 [numeric]  (10)
 ,[sintbl_piseq]	 [smallint]
 ,[vchtbl_proce]	 [varchar]  (50)
 ,[chrtbl_colname]   [varchar]  (1000)
 ,[vchtbl_name]	  [varchar]  (1000)
 ,[sintbl_gercod]	[smallint]
 ,[vchtbl_gerdes]	[varchar]  (100)
 ,[sintbl_agncod]	[smallint]
 ,[vchtbl_agndes]	[varchar]  (100)
 ,[sintbl_matcod]	[smallint]
 ,[vchtbl_matdes]	[varchar]  (100)

 
)

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


set @cmd = ''
set @cont = 0
set @aux = ''

declare coluna cursor for
	select syscolumns.name, syscolumns.xtype, sysobjects.name from syscolumns inner join sysobjects 
			on syscolumns.id = sysobjects.id 
	where sysobjects.xtype = 'U' and sysobjects.name = 'processos'

 set @rows = @@rowcount

open coluna
fetch next from coluna 
into @vchcol_name, @xtype, @vchtbl_name

while @@fetch_status = 0
begin

-- 48  = tinyint
-- 52  = smallint
-- 56  = int
-- 60  = money
-- 99  = ntext
-- 231 = nvarchar

	set @str = ''
	set @aux = ''	

	set @vchcol_name = 'a.' + @vchcol_name
	set @aux = '''' + @vchcol_name + ''''

	if @xtype in (48,52,56)
	   set @str = ' in (null) ' 
	
	if @xtype in (60)
	   set @str = ' < 100 ' 
	
	if @xtype in (99,231)
	   begin
		   set @str = ' in ('''',''.'',null) '
		   if @xtype = 99
			  set @vchcol_name = ' cast(' + @vchcol_name + ' as varchar(8000)) '
	   end

--select 	@vchcol_name,@xtype, @str

		set @cmd =   ' insert into #tbl '
				   + ' select distinct(a.pl_ficha), a.pi_seq, a.pt_processo, ' + @aux + ', ''' + @vchtbl_name + ''', '
				   	   + ' a.tsi_codigo, d.tst_nome, a.tagi_codigo, c.tagi_nome, a.tmi_codigo, e.tmt_nome '
				   + ' from processos a '
				   	   + ' 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 ' + @vchcol_name + @str
				   	   + ' 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))
			   + ' group by pl_ficha, pi_seq, pt_processo, ' + @vchcol_name + ', '
						 + ' a.tsi_codigo, d.tst_nome, a.tagi_codigo, c.tagi_nome, a.tmi_codigo, e.tmt_nome '
		
			exec(@cmd)

	fetch next from coluna 
	into @vchcol_name, @xtype, @vchtbl_name
end
close coluna
deallocate coluna

select 
		case right(chrtbl_colname, len(chrtbl_colname)-2)
							 when 'pt_processo'	   then 'No. do Processo'
							 when 'tai_codigo'		then 'Ação' 
							 when 'tp_codigo'		 then 'Tipo de Pedido'
							 when 'pi_num_orgao_jul'  then 'Orgão Julgador'
							 when 'pc_val_orig_causa' then 'Valor da Causa'	
							 when 'pt_assunto'		then 'Objeto da Ação'
							 when 'pt_contrato'	   then 'No. do Contrato' 
							 when 'pt_nome_advogado'  then 'Advogado Vinculado'
							 when 'pt_nome_autor'	 then 'Autor'
							 when 'pt_nome_reu'	   then 'Réu'
		 end 'vchtbl_incon'
		,sintbl_gercod
		,vchtbl_gerdes
		,sintbl_agncod
		,vchtbl_agndes
	,numtbl_ficha
	,sintbl_piseq
	,vchtbl_proce
	,sintbl_matcod
		,vchtbl_matdes
from #tbl
where right(chrtbl_colname, len(chrtbl_colname)-2) in ('pt_processo','tai_codigo','tp_codigo','pi_num_orgao_jul',
						 'pc_val_orig_causa','pt_assunto','pt_contrato','pt_nome_advogado',
						 'pt_nome_autor','pt_nome_reu')
order by 1,2,3

drop table #tbl


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

 

Quem poder ajudar eu agradeço!!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.