How can I use the IF in Firebird with a stored procedure? - sql

I'm trying to write an IF statement in my stored procedure, but I have no idea how.
TIPO_DOCUMENTO can be 'FAC' or 'DEV', if TIPO_DOCUMENTO is 'FAC', I will use the total_neto, total_impuesto, total, costo and precio as is. But if I have 'DEV', I will need the negative values of those fields.
begin
/*FILTRO RANGO DE FECHA*/
IF (P_FECHA_DESDE='0' OR (P_FECHA_DESDE IS NULL) OR (P_FECHA_DESDE='')) THEN
V_FECHA_DESDE = CAST('01/01/1900' AS DATE);
ELSE
V_FECHA_DESDE = CAST(:P_FECHA_DESDE AS DATE);
IF (P_FECHA_HASTA='0' OR (P_FECHA_HASTA IS NULL)OR (P_FECHA_HASTA='')) THEN
V_FECHA_HASTA = CAST('12/31/3999' AS DATE);
ELSE
V_FECHA_HASTA = CAST(:P_FECHA_HASTA AS DATE);
/*FILTRO RANGO DE VENDEDORES*/
IF (P_VENDEDOR_DESDE IS NULL) THEN P_VENDEDOR_DESDE = '';
IF (P_VENDEDOR_HASTA IS NULL) THEN P_VENDEDOR_HASTA = LOWER('ZZZZZZZZZZZZZZZ');
IF (P_VENDEDOR_HASTA = '') THEN P_VENDEDOR_HASTA = LOWER('ZZZZZZZZZZZZZZZ');
IF (NOT ((P_VENDEDOR_DESDE = '') AND (P_VENDEDOR_HASTA = LOWER('ZZZZZZZZZZZZZZZ')))) THEN
IF (P_VENDEDOR_DESDE = P_VENDEDOR_HASTA) THEN
V_WHERE = V_WHERE || ' AND (VEN.VENDEDOR_CODIGO = '''||P_VENDEDOR_DESDE||''')';
ELSE
V_WHERE = V_WHERE || ' AND (VEN.VENDEDOR_CODIGO BETWEEN '''||P_VENDEDOR_DESDE||''' AND '''||P_VENDEDOR_HASTA||''')';
/*CICLO DE LA CONSULTA SQL*/
FOR SELECT
a.tipo_documento,
a.documento,
b.nombre as vendedor,
c.producto_codigo,
c.producto_nombre as descripcion,
c.cantidad,
d.departamento_codigo,
c.deposito_codigo,
(c.descuento_unitario * c.cantidad) + (c.descuento_unitario_2 * c.cantidad) + (c.descuento_unitario_3 * c.cantidad) +(c.descuento_unitario_4 * c.cantidad) as total_descuento,
c.total_neto,
c.total_impuesto,
c.total,
a.cliente_codigo,
a.cliente_nombre,
e.direccion,
estado.nombre,
ciudad.nombre,
c.costo_unitario * c.cantidad as costo,
c.precio_unitario * c.cantidad as precio
from ventas a
join vendedores b
on a.vendedor_codigo = b.codigo
join ventas_detalles c
on a.correlativo = c.correlativo_principal
join productos_terminados d
on d.codigo_producto = c.producto_codigo
join clientes e
on a.cliente_codigo = e.codigo
join ubicacion_geografica estado
on estado.codigo = e.estado
and estado.tipo = 'E'
join ubicacion_geografica ciudad
on ciudad.codigo = e.ciudad
and ciudad.tipo = 'C'
where a.fecha_emision between :V_FECHA_DESDE and :V_FECHA_HASTA
INTO
:TIPO_DOC,
:NUM_DOC,
:VENDEDOR_NOMBRE,
:PRODUCTO_CODIGO,
:PRODUCTO_NOMBRE,
:CANTIDAD,
:DEPARTAMENTO,
:DEPOSITO,
:DESCUENTO,
:TOTAL_NETO,
:TOTAL_IMPUESTO,
:TOTAL,
:CLIENTE_CODIGO,
:CLIENTE_NOMBRE,
:CLIENTE_DIRECCION,
:ESTADO,
:CIUDAD,
:COSTO,
:PRECIO
do
begin
if (TIPO_DOC = 'DEV') then
total_neto = -total_neto ;
total_impuesto = -total_impuesto ;
total = -total ;
costo = -costo ;
precio = -precio;
suspend;
end
end
Now I can only get the negative values, regardless of whether I have FAC or DEV.

Your if is syntactically wrong for what I think you want to do. The indentation of the following suggests that all those statements should depend on the if condition:
if (TIPO_DOC = 'DEV') then
total_neto = -total_neto ;
total_impuesto = -total_impuesto ;
total = -total ;
costo = -costo ;
precio = -precio;
In reality only the first statement after the if belongs to the if condition. In other words, it is actually
if (TIPO_DOC = 'DEV') then
total_neto = -total_neto ;
total_impuesto = -total_impuesto ;
total = -total ;
costo = -costo ;
precio = -precio;
If you want all those statements to depend on the if, you need to define a block using begin and end:
if (TIPO_DOC = 'DEV') then
begin
total_neto = -total_neto ;
total_impuesto = -total_impuesto ;
total = -total ;
costo = -costo ;
precio = -precio;
end
See also IF ... THEN ... ELSE in the Firebird 2.5 Language Reference.

Not an answer, just an advice, using syntactic highlighting.
FOR SELECT
....
do
if (TIPO_DOC = 'DEV') then
total_neto = -total_neto ;
total_impuesto = -total_impuesto ;
total = -total ;
costo = -costo ;
precio = -precio;
This code has few problems with me.
If within loop - often it may slow down execution. So if IF condition is not dependent upon loop data - if it is invariant - it might make sense to move If outside of loop.
Future compatibility - what would you procedure do if for example TIPO_DOCUMENTO = 'BE-BE-BE'? Should not happen today? But it can happen tomorrow as new functions would be added to the program. It even can happen today because of some bug.
Personally i would do it different way:
DECLARE VARIABLE COEFF SMALLINT;
....
COEFF = CASE TIPO_DOC
WHEN 'DEV' THEN -1
WHEN 'FAC' THEN +1
ELSE :COEFF / 0 /* generate error if prohibited value */
END;
-- shorthand
-- COEFF = DECODE( TIPO_DOC, 'DEV',-1, 'FAC',+1, :COEFF / 0 );
FOR SELECT....
DO
total_neto = COEFF*total_neto ;
total_impuesto = COEFF*total_impuesto;
....
More strange things: P_VENDEDOR_DESDE = ''; - thus this variable is some text type, like VarChar or CHAR or BLOB SUB_TYPE TEXT.
Then why VEN.VENDEDOR_CODIGO BETWEEN '''||P_VENDEDOR_DESDE||''' AND '''||P_VENDEDOR_HASTA||''' ???
Why not just VEN.VENDEDOR_CODIGO BETWEEN P_VENDEDOR_DESDE AND P_VENDEDOR_HASTA ?

Related

MaxDB SAP SQL Missing keyword WITH on update

Hi there I'm using MaxDB and trying to update a table and I get the error Missing keyword:WITH. Code: -5015.
Here is my SQL:
UPDATE agm
SET agm_confirm_stat = 'C',
agm_confirm_usr = 'MEDICWARE',
agm_confirm_obs = 'Wellon: Confirmado',
agm_obs = NVL(agm_obs, '') || '\/\/Wellon: Confirmar',
agm_confirm_dthr = TIMESTAMP
WHERE (agm_pac = '319900')
AND (
agm_obs NOT LIKE '%Wellon: Confirmar%'
OR agm_obs IS NULL
)
AND (agm_str_cod = 'FBC')
AND (agm_hini = '2022-12-07 08:00:00')
AND (agm_confirm_stat != 'C')
AND (agm_stat = 'A')

PL/SQL Procedure to create XML

I'm very inexperienced with PL/SQL, and am tasked with creating a procedure that use need two parameters and create XML for my current select statement. I have bits and pieces of it, but am having trouble finding how to put it all together.
So I know I will need to start with the following:
PROCEDURE markviewimport_interface c_markviewimport.invoice_id NUMBER(10) = NULL c_markviewimport.filename nvarchar(30) = NULL
And I know that I will need a cursor in order to gather the data line by line.
CURSOR c_markviewimport IS SELECT DISTINCT inv.invoice_id,
vendor.segment1 vendor_num,
vendor.vendor_name,
poh.segment1 PO_NUMBER,
inv.invoice_date,
inv.invoice_num,
terms.name TERMS_NAME,
inv.invoice_amount,
inv.amount_applicable_to_discount,
inv.amount_paid,
pmt.check_date PAYMENT_DATE,
path.filename,
path.complete_filename,
path.document_id,
stamps.text,
stamps.tool_name
FROM apps.ap_invoices_all inv,
apps.ap_invoice_distributions_all dist,
apps.po_distributions_all podi,
apps.ap_invoice_payment_history_v pmt,
apps.fnd_attached_docs_form_vl fnd,
markview.mv_page_image_paths path,
apps.po_vendors vendor,
apps.po_headers_all poh,
apps.ap_terms terms,
(SELECT mp.document_id,
moi.markup_object_id,
moi.page_markups_view_id,
moi.text,
mvt.tool_name,
mp.page_id
FROM markview.mv_markup_object moi,
markview.mv_tool mvt,
markview.mv_page_markups_view mpmv,
markview.mv_page mp
WHERE moi.tool_id = mvt.tool_id
AND mp.page_id = mpmv.page_id
AND mpmv.page_markups_view_id = moi.page_markups_view_id
AND mvt.tool_id IN (SELECT mvt.tool_id
FROM markview.mv_tool
WHERE mvt.tool_name IN (
'Green Text', 'Blue Sticky Note' )
)) stamps
WHERE inv.invoice_id = To_number(fnd.pk1_value)
AND inv.invoice_id = dist.invoice_id
AND poh.po_header_id(+) = podi.po_header_id
AND podi.po_distribution_id(+) = dist.po_distribution_id
AND fnd.file_name = To_char(path.document_id)
AND inv.invoice_id = pmt.invoice_id
AND path.document_id = stamps.document_id(+)
AND path.page_id = stamps.page_id(+)
AND fnd.category_description = 'MarkView Document'
AND fnd.entity_name = 'AP_INVOICES'
AND INV.vendor_id = poh.vendor_id(+)
AND INV.terms_id = TERMS.term_id
AND inv.vendor_id = vendor.vendor_id
AND path.platform_name = 'UNIX_FS_TO_DOC_SERVER'
AND pmt.void = 'N') r_markviewimport
And lastly this is what I'm using for XML that contains every column I need.
SELECT XMLELEMENT("Item", Xmlforest(r_markviewimport.invoice_id,
r_markviewimport.vendor_num, r_markviewimport.vendor_name,
r_markviewimport.po_number,
r_markviewimport.invoice_date, r_markviewimport.invoice_num,
r_markviewimport.terms_name, r_markviewimport.invoice_amount,
r_markviewimport.amount_applicable_to_discount,
r_markviewimport.amount_paid,
r_markviewimport.payment_date,
r_markviewimport.filename, r_markviewimport.complete_filename,
r_markviewimport.document_id, r_markviewimport.text,
r_markviewimport.tool_name
)) "Item Element"
I guess I just need help, or pointed in the right direction for creating this procedure and putting it all together.
There are several ways to generate XML from tables in Oracle.
The main benefit to using the SQL functions (like XMLELEMENT and XMLFOREST) is that you don't have to iterate over a cursor line-by-line - you can do all the work in one query. Conversely, if you want to do it row-by-row with a cursor like that, using DBMS_XMLGEN would probably make more sense. You might want to do that if you have a lot of changes to make to the data before writing it to XML.
Here's a procedure to do it with XML SQL functions.
create or replace procedure markviewimport_interface (p_invoice_id number(10), p_filename nvarchar(30))
is
v_output clob;
begin
SELECT XMLELEMENT("Items", XMLAGG(XMLELEMENT("Item", Xmlforest(r_markviewimport.invoice_id,
r_markviewimport.vendor_num, r_markviewimport.vendor_name,
r_markviewimport.po_number,
r_markviewimport.invoice_date, r_markviewimport.invoice_num,
r_markviewimport.terms_name, r_markviewimport.invoice_amount,
r_markviewimport.amount_applicable_to_discount,
r_markviewimport.amount_paid,
r_markviewimport.payment_date,
r_markviewimport.filename, r_markviewimport.complete_filename,
r_markviewimport.document_id, r_markviewimport.text,
r_markviewimport.tool_name
)))).getclobval() into v_output
FROM
(SELECT DISTINCT inv.invoice_id,
vendor.segment1 vendor_num,
vendor.vendor_name,
poh.segment1 PO_NUMBER,
inv.invoice_date,
inv.invoice_num,
terms.name TERMS_NAME,
inv.invoice_amount,
inv.amount_applicable_to_discount,
inv.amount_paid,
pmt.check_date PAYMENT_DATE,
path.filename,
path.complete_filename,
path.document_id,
stamps.text,
stamps.tool_name
FROM apps.ap_invoices_all inv,
apps.ap_invoice_distributions_all dist,
apps.po_distributions_all podi,
apps.ap_invoice_payment_history_v pmt,
apps.fnd_attached_docs_form_vl fnd,
markview.mv_page_image_paths path,
apps.po_vendors vendor,
apps.po_headers_all poh,
apps.ap_terms terms,
(SELECT mp.document_id,
moi.markup_object_id,
moi.page_markups_view_id,
moi.text,
mvt.tool_name,
mp.page_id
FROM markview.mv_markup_object moi,
markview.mv_tool mvt,
markview.mv_page_markups_view mpmv,
markview.mv_page mp
WHERE moi.tool_id = mvt.tool_id
AND mp.page_id = mpmv.page_id
AND mpmv.page_markups_view_id = moi.page_markups_view_id
AND mvt.tool_id IN (SELECT mvt.tool_id
FROM markview.mv_tool
WHERE mvt.tool_name IN (
'Green Text', 'Blue Sticky Note' )
)) stamps
WHERE inv.invoice_id = To_number(fnd.pk1_value)
AND inv.invoice_id = dist.invoice_id
AND poh.po_header_id(+) = podi.po_header_id
AND podi.po_distribution_id(+) = dist.po_distribution_id
AND fnd.file_name = To_char(path.document_id)
AND inv.invoice_id = pmt.invoice_id
AND path.document_id = stamps.document_id(+)
AND path.page_id = stamps.page_id(+)
AND fnd.category_description = 'MarkView Document'
AND fnd.entity_name = 'AP_INVOICES'
AND INV.vendor_id = poh.vendor_id(+)
AND INV.terms_id = TERMS.term_id
AND inv.vendor_id = vendor.vendor_id
AND path.platform_name = 'UNIX_FS_TO_DOC_SERVER'
AND pmt.void = 'N') r_markviewimport;
DBMS_XSLPROCESSOR.clob2file(v_output, 'MY_DIRECTORY', 'output.xml');
--return v_output;
end markviewimport_interface;
/

GAMS: Why is the value of the parameter pp(ss,ii) coming out as zero?

I have written the following code below in GAMS and it may not be the best in the world but I don't see why it keeps returning the value of the parameter pp(ss,ii) as all zeros when it should be a matrix of exactly 2 ones in each row.
Could you please help me?
set
ii set of bays /1*10/
ss set of solutions /1*45/
Parameters
H Number of bays /10/
C Number of cranes /2/
r Safety Margin /1/
p(ii)
pp(ss,ii)
;
p(ii) = 0;
Scalar i; i=1;
Scalar j; j=i;
Scalar k; k=C;
Scalar qq;
Scalar q;
Scalar kk;
Scalar m;
Scalar n;
Scalar stop /0/;
Scalar stop1 /0/;
Scalar stop2 /0/;
Scalar F /1/;
Scalar k1 /0/;
Scalar k2 /0/;
while ((i <= H-(C-1)*r-1*(C-1)),
while((j<=H and stop=0),
if (sum(ii, p(ii)) = C,
stop = 1;
);
loop(ii$(ord(ii)=j),
p(ii) = 1;
);
j = j+r+1;
);
if (sum(ii, p(ii)) = C,
loop((ss,ii)$(ord(ss)=F),
pp(ss,ii) = p(ii);
);
);
F = F+1;
qq=0;
for (q = 1 to H-(C-1)*r-1*(C-1)-1,
loop (ii$((ord(ii) = q) and (stop1 = 0)),
if ((p(ii) = 1) ,
qq=1;
stop1 = 1;
);
);
);
* if (qq=0,
* if (p(ii)$(ord(ii)= H-(C-1)*r-1*(C-1))=1 and p(ii)$(ord(ii)=H) = 1,
* abort "finished";
* );
*
* );
k1 = 0;
k2 = 0;
if(qq=0,
loop(ii$(ord(ii)= H-(C-1)*r-1*(C-1)),
if (p(ii) = 1,
k1 = 1;
);
);
loop(ii$(ord(ii)=H),
if (p(ii) = 1,
k2 = 1;
);
);
abort$(k1 + k2 = 2) "Finished";
);
if (j-r-1 = H,
loop(ii$(ord(ii)=H-(C-k)*r-(C-k)) ,
if(k = C,
k = k-1;
elseif (p(ii)= 1),
k = k-1;
);
);
if(k=1,
loop(ii,
p(ii)=0;
);
i = i+1;
j = i;
k = C;
else
loop(ii$(ord(ii)=H),
p(ii) = 0;
);
m=1;
n=0;
while((n<k),
loop(ii$(ord(ii)=m),
if(p(ii) = 1,
n = n+1;
);
m = m+1;
);
);
loop(ii$(ord(ii) = m-1),
p(ii) = 0;
);
loop(ii$(ord(ii) = m),
p(ii) = 1;
);
j = m+r+1;
);
else
loop(ii$(ord(ii) = j-r-1),
p(ii)=0;
);
loop(ii$(ord(ii) = j-r),
p(ii)= 1;
);
j=j+1;
);
);
Display pp;
On how to hunt errors in rather complex code:
first look up where you assigne values to your variable. You set pp = p; one time.
look up if p is right. Display says p is zero too.
during the pre solve process you can add aditional "display" statements in the middle of you code - to get the values at this points.
This gives you the hint that there are some values in p - but when you put the display statement after if (sum(ii, p(ii)) = C, you wont get any display calls -> sum(p) <> C ?
A closer Look into the p before the if and you'll see that there are three values inside which are more than two.
At the last step its simple to see the error in you code. It's the position of you stop = 1; criteria. Its placed at the beginning of the while loop. The loop will still continue in the current iteration and create a third value.
Solution:
Change the criteria to if (sum(ii, p(ii)) = C-1, or put the criteria at the end of the while loop.

Updating multiple tables inside cursor not working

I have two records in a table A. This helps me to update table 2 however the update is not been applied. This is the SQL code where I added a validation section that allow me to determinate the problem.
DECLARE abc CURSOR FOR
select d.cod_suc, d.cod_ramo_comercial, d.nro_pol, d.cod_item, d.cod_ramo_tecnico, d.cod_tarifa,
d.id_stro, d.nro_stro, d.fec_hora_reclamo, d.fec_aviso, d.fec_registro, d.fec_ingreso_contable,
d.txt_place_of_accident, d.id_substro, d.txt_nombre_cober, d.txt_direccion_bien_siniestrado, d.txt_descripcion_perdida, d.txt_cheque_a_nom, d.cod_manager_code, d.importe_pago_eq
from table1 d
where d.fec_hora_reclamo between '20140801' and '20150731'
and (d.cod_suc = 2 and d.cod_ramo_comercial = 255 and d.nro_pol = 1000001 and d.cod_item = 5)
OPEN abc
FETCH abc INTO #cod_suc, #cod_ramo_comercial, #nro_pol, #cod_item, #cod_ramo_tecnico, #cod_tarifa,
#id_stro, #nro_stro, #fec_hora_reclamo, #fec_aviso, #fec_registro, #fec_ingreso_contable,
#txt_place_of_accident, #id_substro, #txt_nombre_cober, #txt_direccion_bien_siniestrado, #txt_descripcion_perdida, #txt_cheque_a_nom, #cod_manager_code, #importe_pago_eq
WHILE (##FETCH_STATUS = 0)
BEGIN
select #varIDPV = min(id_pv), #varCodTarifa = min(cod_tarifa)
from #portfolio p
where p.cod_suc = #cod_suc and p.cod_ramo_comercial = #cod_ramo_comercial and p.Poliza = #nro_pol and p.Item = #cod_item and p.cod_ramo_tecnico = #cod_ramo_tecnico
and p.[ID Incident] IS NULL
/**************************************************************************************************************
-- Validation section
-- First record:
mid(id_pv) = 100, min(cod_tarifa) = 1
-- Second record:
mid(id_pv) = 100, min(cod_tarifa) = 1
--> Should be mid(id_pv) = 100, min(cod_tarifa) = 2
*/
select min(id_pv), min(cod_tarifa)
from #portfolio p
where p.cod_suc = #cod_suc and p.cod_ramo_comercial = #cod_ramo_comercial and p.Poliza = #nro_pol and p.Item = #cod_item and p.cod_ramo_tecnico = #cod_ramo_tecnico
and p.[ID Incident] IS NULL
/**************************************************************************************************************/
update p set p.[ID Incident] = #id_stro, p.[No. Incident] = #nro_stro,
p.[Fecha Accidente] = #fec_hora_reclamo, p.[Fecha Notificacion] = #fec_aviso, p.[Fecha Registro] = #fec_registro, p.[Fecha Pago] = #fec_ingreso_contable,
p.[Lugar Accidente] = #txt_place_of_accident, p.[ID Subsiniestro] = #id_substro, p.[Cobertura Amparo] = #txt_nombre_cober, p.[Direccion Bien Siniestrado] = #txt_direccion_bien_siniestrado, p.[Descripcion Perdida] = #txt_descripcion_perdida, p.[Pago A] = #txt_cheque_a_nom, p.[Referida] = #cod_manager_code,
[Incurrido R12] = #importe_pago_eq, [Incurrido Cerrados R12] = #importe_pago_eq
from #portfolio p
where p.cod_suc = #cod_suc and p.cod_ramo_comercial = #cod_ramo_comercial and p.Poliza = #nro_pol and p.Item = #cod_item and p.cod_ramo_tecnico = #cod_ramo_tecnico
and p.[ID Incident] IS NULL and p.id_pv = #varIDPV and p.cod_tarifa = #varCodTarifa
FETCH abc INTO #cod_suc, #cod_ramo_comercial, #nro_pol, #cod_item, #cod_ramo_tecnico, #cod_tarifa,
#id_stro, #nro_stro, #fec_hora_reclamo, #fec_aviso, #fec_registro, #fec_ingreso_contable,
#txt_place_of_accident, #id_substro, #txt_nombre_cober, #txt_direccion_bien_siniestrado, #txt_descripcion_perdida, #txt_cheque_a_nom, #cod_manager_code, #importe_pago_eq
END
CLOSE abc
DEALLOCATE abc
When I see the result, only the first record is updated.
I found the problem. I forgot to add one condition more in update (inside where). Thanks for the support

How to join three tables?

SELECT
PC_SL_ACNO, -- DB ITEM
SLNAME, -- ACCOUNT NAME:
SL_TOTAL_AMOUNT -- TOTAL AMOUNT:
FROM GLAS_PDC_CHEQUES
WHERE PC_COMP_CODE=:parameter.COMP_CODE
AND pc_bank_from = :block02.pb_bank_code
AND pc_due_date between :block01.date_from
AND :block01.date_to
AND nvl(pc_discd,'X') IN(‘X’, 'R')
GROUP BY
pc_comp_code, pc_sl_ldgr_code, pc_sl_acno
ORDER BY pc_sl_acno
ACCOUNT NAME:
BEGIN
SELECT COAD_PTY_FULL_NAME INTO :BLOCK03.SLNAME
FROM GLAS_PTY_ADDRESS,GLAS_SBLGR_MASTERS
WHERE SLMA_COMP_CODE = :PARAMETER.COMP_CODE
AND SLMA_ADDR_ID = COAD_ADDR_ID
AND SLMA_ADDR_TYPE = COAD_ADDR_TYPE
AND SLMA_ACNO = :BLOCK03.PC_SL_ACNO
AND SLMA_COMP_CODE = COAD_COMP_CODE;
EXCEPTION WHEN OTHERS THEN NULL;
END;
TOTAL AMOUNT:
BEGIN
SELECT SUM(PC_AMOUNT) INTO :SL_TOTAL_AMOUNT
FROM GLAS_PDC_CHEQUES
WHERE PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
AND PC_BANK_FROM = :block02.PB_BANK_CODE
AND PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND NVL(PC_DISCD,'X') = 'R'
AND PC_COMP_CODE = :PARAMETER.COMP_CODE;
EXCEPTION WHEN OTHERS THEN :block03.SL_TOTAL_AMOUNT := 0;
END;
How can I join the three tables?
You'll have to adjust depending on precisely what criteria and required fields you have for your query or queries.
SELECT
c.PC_SL_ACNO,
a.COAD_PTY_FULL_NAME,
SUM(c.PC_AMOUNT)
FROM
GLAS_PDC_CHEQUES c
LEFT JOIN
GLAS_SBLGR_MASTERS m
ON ( c.PC_SL_ACNO = m.SLMA_ACNO
AND c.PC_COMP_CODE = m.SLMA_COMP_CODE
)
LEFT JOIN
GLAS_PTY_ADDRESS a
ON ( m.SLMA_ADDR_ID = a.COAD_ADDR_ID
AND m.SLMA_COMP_CODE = a.COAD_COMP_CODE
AND m.SLMA_ADDR_TYPE = a.COAD_ADDR_TYPE
)
WHERE
c.PC_COMP_CODE = :PARAMETER.COMP_CODE
AND c.PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND c.PC_BANK_FROM = :BLOCK02.PB_BANK_CODE
AND NVL(c.PC_DISCD,'X') IN (‘X’, 'R')
AND c.PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
GROUP BY
c.PC_SL_ACNO, -- not sure which grouping exactly you need.
a.COAD_PTY_FULL_NAME
ORDER BY
c.PC_SL_ACNO
I notice that in the first query you have pc_comp_code as a search criterion, and on the leading edge of your grouping - is that what you need?
This is a bit of an 'estimate' due to the enigmatic nature of your question!