I have a problem with the cursor part: cursor with the name 'BankBank' 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