Preciso gerar uma consulta para excel diretamente de um form, porém o arquivo é gerado somente com o cabeçalho e nenhum dado.
Testei a rotina da procedure no PLSQL passando os parâmetros e consigo os dados.
Será que é algum erro na rotina do form?
PROCEDURE PR_GERA_EXCEL IS
warquivo Client_Text_IO.file_type;
lNm_Arquivo varchar2(500);
wlinha varchar2(5000);
wfl_tipo_exame varchar2(30);
wLaboratorio varchar2(30);
P_FL_INTERNACAO varchar2(03);
P_CD_MOTIVO_ATENDIMENTO varchar2(03);
P_FL_CIRURGIA varchar2(40);
P_FL_TIPO_EXAME varchar2(40);
P_TIPO_EXAME number(02);
BEGIN
If :bl_cad.nm_arquivo is null then
pr_alerta('Informe o nome do arquivo para geração da planilha.');
raise form_trigger_failure;
end if;
If :BL_CAD.P_DT_INICIAL is null or :BL_CAD.P_DT_FINAL is null then
pr_alerta('Data Invalída !');
raise form_trigger_failure;
end if;
P_FL_INTERNACAO := :BL_CAD.P_FL_INTERNO||','||:BL_CAD.P_FL_EXTERNO;
P_CD_MOTIVO_ATENDIMENTO:= :BL_CAD.P_MOTIVO_ELETIVO||','||:BL_CAD.P_MOTIVO_EMERGENCIA;
P_TIPO_EXAME := :BL_CAD.P_TIPO_EXAME;
-- Tipos de Exames (Solicitação: 54283)
If (:Bl_CAD.P_TIPO_EXAME IS NOT NULL) then
if (:BL_CAD.P_TIPO_EXAME = 99) then --Todos
P_FL_CIRURGIA := '2,1,0,3';
P_FL_TIPO_EXAME:= '0,1,2,3,4,5,6,7,8';
else
P_FL_CIRURGIA:= :BL_CAD.P_TIPO_EXAME;
If (:BL_CAD.P_TIPO_EXAME = 2) then
P_FL_TIPO_EXAME:= '1,0';
else
P_FL_TIPO_EXAME:= '2,3,4,5,6,7,8';
end if;
end if;
end if;
-- Janela para localizacao do arquivo
Begin
lNm_Arquivo:= CLIENT_GET_FILE_NAME('','','Todos os Arquivos do Microsoft Office Excel (*.csv)|*.csv',SAVE_FILE,SAVE_FILE);
Exception
when others then
null;
end;
If lNm_Arquivo IS NOT NULL then
:BL_CAD.NM_ARQUIVO:= lNm_Arquivo;
end if;
begin
warquivo:= Client_Text_IO.Fopen(:BL_CAD.NM_ARQUIVO, 'w');
exception
when others then
pr_alerta('Erro ao criar arquivo');
raise form_trigger_failure;
end;
Client_Text_IO.Put_Line(warquivo, wlinha);
wlinha:= 'CD_UNIDADE_ATENDIMENTO;NM_UNIDADE_ATENDIMENTO;CD_SETOR_ORIGEM;NM_SETOR;CD_GRUPO_PRODUTO;NM_GRUPO_PRODUTO;CD_PROCEDIMENTO;NR_PROCEDIMENTO;QT_PROCEDIMENTO;VL_TOTAL';
Client_Text_IO.Put_Line(warquivo, wlinha);
begin
for i in (select s.cd_unidade_atendimento,
u.nm_unidade_atendimento,
g.cd_setor_origem,
s.nm_setor,
-- nvl(gp.cd_grupo_produto, 0) cd_grupo_produto,
nvl(fn_grupo_excessao(a.cd_unidade_atendimento,p.cd_procedimento),0) cd_grupo_produto,
nvl(grp.nm_grupo_produto, 'SEM GRUPO') nm_grupo_produto,
pr.cd_procedimento,
p.nr_procedimento,
sum(pr.qt_procedimento) qt_procedimento,
sum(pr.vl_total) vl_total
from tb_grupo_produto grp,
tb_unidade_atendimento u,
tm_setor s,
-- tb_item_grupo_procedimento gp,
tb_procedimento p,
tm_atendimento a,
tb_pedido_exame pe,
tb_guia g,
tb_procedimento_realizado pr
where pr.dt_procedimento_realizado between
to_date(:BL_CAD.P_DT_INICIAL, 'dd/mm/yyyy') and
to_date(:BL_CAD.P_DT_FINAL, 'dd/mm/yyyy')
and g.cd_pessoa_realiza like :BL_CAD.P_CD_PESSOA
-- and nvl(gp.cd_grupo_produto, 0) like :BL_CAD.P_CD_GRUPO_PRODUTO
and nvl(fn_grupo_excessao(a.cd_unidade_atendimento,p.cd_procedimento),0) like :BL_CAD.P_CD_GRUPO_PRODUTO
and g.cd_setor_origem like :BL_CAD.P_CD_SETOR
and instr(P_CD_MOTIVO_ATENDIMENTO, to_char(a.cd_motivo_atendimento + 0)) > 0
and (instr(p_fl_cirurgia, to_char(p.fl_cirurgia+0)) > 0)
and (instr(p_fl_tipo_exame, to_char(p.fl_tipo_exame+0)) > 0)
and (instr(p_fl_internacao, a.fl_internacao) > 0)
and pr.cd_atendimento = g.cd_atendimento
and pr.cd_ocorrencia = g.cd_ocorrencia
and g.cd_atendimento = pe.cd_atendimento
and g.cd_ocorrencia_pedido = pe.cd_ocorrencia
and pe.cd_atendimento = a.cd_atendimento
and pr.cd_procedimento = p.cd_procedimento
and g.cd_setor_origem = s.cd_setor
and s.cd_unidade_atendimento = u.cd_unidade_atendimento
-- and p.cd_procedimento = gp.cd_procedimento(+)
and fn_grupo_excessao(a.cd_unidade_atendimento,p.cd_procedimento) = grp.cd_grupo_produto --gp.cd_grupo_produto = grp.cd_grupo_produto(+)
group by s.cd_unidade_atendimento,
u.nm_unidade_atendimento,
g.cd_setor_origem,
s.nm_setor,
a.cd_unidade_atendimento,
p.cd_procedimento,
cd_grupo_produto,
grp.nm_grupo_produto,
pr.cd_procedimento,
p.nr_procedimento
order by u.nm_unidade_atendimento,
s.nm_setor,
grp.nm_grupo_produto,
p.nr_procedimento) loop
Client_Text_IO.Put_Line(warquivo,trim(wlinha));
-- begin
wlinha:= i.cd_unidade_atendimento ||';'||
i.nm_unidade_atendimento ||';'||
i.cd_setor_origem ||';'||
i.nm_setor ||';'||
i.cd_grupo_produto ||';'||
i.nm_grupo_produto ||';'||
i.cd_procedimento ||';'||
i.nr_procedimento ||';'||
i.qt_procedimento ||';'||
i.vl_total;
Client_Text_IO.Put_Line(warquivo,trim(wlinha));
end loop;
exception
when others then
pr_alerta(SQLERRM);
raise form_trigger_failure;
end;
Client_Text_IO.Fclose(warquivo);
pr_alerta('Arquivo gerado com sucesso !' || chr(13) || :BL_CAD.NM_ARQUIVO);
END;