2
I have a problem with the cursor part: cursor with the name 'Cursobanco' does not exist.
Sql code:
if @Opcao='1'
begin
set @tipoPagamento='20'
set @LayOut='040'
declare CursoBanco Cursor LOCAL STATIC for
Select distinct ct.BcoNum AS BANCO,
(case when left(ct.BcoNum,3) = '341' then '01'
THEN (case WHEN left(empcpfcgc,8)=left(entcpfcgc,8) then '43' else
'41' end)
else (case WHEN left(empcpfcgc,8)=left(entcpfcgc,8) then '07' else '03' end)
END)
END) AS TipoRemesa
From PARC_DOC_FIN PDF With(Nolock),ITEM_COB_BANC ICB
With(Nolock),conta_parc_doc_fin ct With(Nolock),
Empresa_filial ef With(Nolock), entidade ent with(nolock)
Where PDF.EmpCod = ICB.EmpCodparc
And PDF.DocFinChv = ICB.DocFinChv
And PDF.ParcDocFinSeq = ICB.ParcDocFinSeq
And PDF.ParcDocFinDesmPag = ICB.ParcDocFinDesmPag
And ICB.BcoNum = @Banco
And ICB.CobBancRemNum = @Remessa
And left(ICB.EmpCod,2) = left(@Empresa,2)
And ICB.CobBancTipo = @Tipo
And ICB.AgNum = @Agencia
And left(ICB.EmpCod,2)=ef.empcod
And PDF.entcod=ent.entcod
and ct.empcod = icb.empcod
and ct.docfinchv = icb.docfinchv
and ct.ParcDocFinSeq = icb.ParcDocFinSeq
order by TipoRemesa
end
if @Opcao='2'
begin
set @tipoPagamento='20'
set @LayOut='030'
declare CursoBanco Cursor LOCAL STATIC for
Select distinct left(pdf.parcdocfincodleit,3) AS BANCO, (CASE left(pdf.parcdocfincodleit,3) WHEN '341' THEN '30' ELSE '31' END) AS TipoRemesa
From PARC_DOC_FIN PDF With(Nolock),ITEM_COB_BANC ICB With(Nolock)
Where PDF.EmpCod = ICB.EmpCodparc
And PDF.DocFinChv = ICB.DocFinChv
And PDF.ParcDocFinSeq = ICB.ParcDocFinSeq
And PDF.ParcDocFinDesmPag = ICB.ParcDocFinDesmPag
And ICB.BcoNum = @Banco
And ICB.CobBancRemNum = @Remessa
And left(ICB.EmpCod,2) = left(@Empresa,2)
And ICB.CobBancTipo = @Tipo
And ICB.AgNum = @Agencia
order by TipoRemesa
end
if @Opcao='3'
begin
-- DARF = 22 - 16 GPS = 22 - 17 FGTS/GFIP = 22 - 35
set @tipoPagamento='22'
set @LayOut='030'
declare CursoBanco Cursor LOCAL STATIC for
Select distinct left(pdf.parcdocfincodleit,3) AS BANCO,
(Case when (docfinespec = 'FGTS') or (pdf.entcod = '0000069') or
(pdf.entcod = '0002924') then '35'
when (docfinespec = 'GPS') or (pdf.entcod = '0002481') then '17'
when docfinespec = 'DARF' then '16' end) as TipoRemesa
From PARC_DOC_FIN PDF With(Nolock),ITEM_COB_BANC ICB With(Nolock),
Doc_fin DF With(noLock)
Where PDF.EmpCod = ICB.EmpCodparc
And PDF.DocFinChv = df.DocFinChv
And PDF.EmpCod = Df.EmpCod
And PDF.DocFinChv = ICB.DocFinChv
And PDF.ParcDocFinSeq = ICB.ParcDocFinSeq
And PDF.ParcDocFinDesmPag = ICB.ParcDocFinDesmPag
And ICB.BcoNum = @Banco
And ICB.CobBancRemNum = @Remessa
And ICB.EmpCod = @Empresa
And ICB.CobBancTipo = @Tipo
And ICB.AgNum = @Agencia
order by BANCO
end
if @Opcao='4'
begin
set @tipoPagamento='20' -- Diversos - FP- 13 Pagto Concessionarias;
set @LayOut='030'
declare CursoBanco Cursor LOCAL STATIC for
Select distinct substring(pdf.parcdocfincodleit,2,1) AS Banco, '13' as
TipoRemessa
From PARC_DOC_FIN PDF With(Nolock),ITEM_COB_BANC ICB With(Nolock)
Where PDF.EmpCod = ICB.EmpCodparc
And PDF.DocFinChv = ICB.DocFinChv
And PDF.ParcDocFinSeq = ICB.ParcDocFinSeq
And PDF.ParcDocFinDesmPag = ICB.ParcDocFinDesmPag
And ICB.BcoNum = @Banco
And ICB.CobBancRemNum = @Remessa
And left(ICB.EmpCod,2) = left(@Empresa,2)
And ICB.CobBancTipo = @Tipo
And ICB.AgNum = @Agencia
order by Banco -- Banco = Segmento = 1-Prefeituras;2-Saneamento;3-Energia Eletrica e Gas;4-Telecomunicações; 5- DARF/GPS com codigo
end
Open CursoBanco
You can put what you do inside the
cursor
to see if it is possible to replace with another command?– Sorack
I didn’t understand it very well. Would you have some example for me to have as reference?
– Felipe Michael da Fonseca
I didn’t actually make this code. But there are 4 ifs. I’ll edit the question to see
– Felipe Michael da Fonseca
Felipe, ignore my comment on variable scope; it was my fault. At first what occurred is that the cursor was not declared at the time of Open execution.
– José Diz
Only for testing purposes, before
Open CursoBanco
placeSELECT @Opcao
and repeat processing. Check that the value of "@Option" is one of the 4 values previously treated.– José Diz