CREATE PROCEDURE SP_AGENDADOS( PDATA DATE, PAGENDA INTEGER, PDIASEMANA INTEGER) RETURNS( DATA DATE, HORA TIME, CODHORA INTEGER, QTATD INTEGER, IDAGENDAMENTO INTEGER, IDEMPRESA INTEGER, IDPACIENTE INTEGER, PACIENTE VARCHAR(70) CHARACTER SET NONE, STATUS CHAR(1) CHARACTER SET NONE, RECEITUARIO VARCHAR(300) CHARACTER SET NONE, ENCAIXE VARCHAR(1) CHARACTER SET NONE, PRONT INTEGER) AS declare variable wdtag Date; declare variable whrag Time; declare variable wdtpac Date; declare variable whrpac Time; declare variable wempresa Varchar(60); declare variable wrecexame Integer; begin for SELECT SP_AGENDA.DATA, SP_AGENDA.HORA, SP_AGENDA.CODHORA, SP_AGENDA.QTATD, "agendamento"."idagendamento", "agendamento"."data", "agendamento"."hora", "agendamento"."statusagendamento", "paciente"."idpaciente", "paciente"."nome", "empresa"."idempresa", "empresa"."nomefantasia","agendamento"."encaixe", "agendamento"."prontuario" FROM SP_AGENDA(:PDATA, :PAGENDA, :PDIASEMANA) LEFT OUTER JOIN "agendamento" ON ((SP_AGENDA.CODHORA = "agendamento"."codhora") AND (SP_AGENDA.DATA = "agendamento"."data") AND (SP_AGENDA.HORA = "agendamento"."hora") AND (SP_AGENDA.AGENDA = "agendamento"."agenda")) LEFT JOIN "paciente" ON ("agendamento"."paciente" = "paciente"."idpaciente") LEFT JOIN "empresa" ON ("agendamento"."empresa" = "empresa"."idempresa") WHERE ((SP_AGENDA.DATA = :PDATA) OR ("agendamento"."data" = :PDATA) AND ("agendamento"."agenda" = :PAGENDA) AND ("agendamento"."encaixe" = 'S')) UNION SELECT SP_AGENDA.DATA, SP_AGENDA.HORA, SP_AGENDA.CODHORA, SP_AGENDA.QTATD, "agendamento"."idagendamento", "agendamento"."data", "agendamento"."hora", "agendamento"."statusagendamento", "paciente"."idpaciente", "paciente"."nome", "empresa"."idempresa", "empresa"."nomefantasia","agendamento"."encaixe", "agendamento"."prontuario" FROM SP_AGENDA(:PDATA, :PAGENDA, :PDIASEMANA) RIGHT OUTER JOIN "agendamento" ON ((SP_AGENDA.CODHORA = "agendamento"."codhora") AND (SP_AGENDA.DATA = "agendamento"."data") AND (SP_AGENDA.HORA = "agendamento"."hora") AND (SP_AGENDA.AGENDA = "agendamento"."agenda")) LEFT JOIN "paciente" ON ("agendamento"."paciente" = "paciente"."idpaciente") LEFT JOIN "empresa" ON ("agendamento"."empresa" = "empresa"."idempresa") WHERE (("agendamento"."data" = :PDATA) AND ("agendamento"."agenda" = :PAGENDA) AND ("agendamento"."encaixe" = 'S')) INTO :wdtag, :whrag, :codhora, :qtatd, :idagendamento, :wdtpac, :whrpac, :status, :idpaciente, :paciente, :idempresa, :wempresa, :encaixe, :PRONT do begin if (PRONT IS NULL) then begin PRONT = 0; end receituario = ''; for SELECT "receituario"."exame" FROM "receituario" WHERE "receituario"."agendamento" = :idagendamento AND "receituario"."dispensar" = 'N' INTO :wrecexame do begin receituario = :receituario || '@' || :wrecexame; end if (wdtpac is not null) then data = :wdtpac; if (whrpac is not null) then hora = :whrpac; if (wdtag is not null) then data = :wdtag; if (whrag is not null) then hora = :whrag; if ((paciente is null) and (idagendamento is not null)) then paciente = 'RESERVADO - ' || :wempresa; Suspend; end end;