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 ?
I have a query that requires ONE HUNDRED fields to be joined from one table to another. instead of manually going through the query designer, i decided to build the join using excel. When i put the code in to the SQL side of the query, then clicked designer, it choked on all 100 field comparisons and removed them. i proceeded to manually click drag the entire set of 100 field to join them all. somehow it worked. now when i look at the sql code for how access did it, it looks almost identical to my code. the only difference that i can identify is that instead of it going from A-Z, it goes Z-A.
Original Query
PARAMETERS pn TEXT (255)
,tt TEXT (255)
,sns Long
,sne Long
,ds DATETIME
,de DATETIME;
SELECT y.Short_Description1
,y.Data1
,y.Short_Description2
,y.Data2
,y.Short_Description3
,y.Data3
,y.Short_Description4
,y.Data4
,y.Short_Description5
,y.Data5
,y.Short_Description6
,y.Data6
,y.Short_Description7
,y.Data7
,y.Short_Description8
,y.Data8
,y.Short_Description9
,y.Data9
,y.Short_Description10
,y.Data10
,y.Short_Description11
,y.Data11
,y.Short_Description12
,y.Data12
,y.Short_Description13
,y.Data13
,y.Short_Description14
,y.Data14
,y.Short_Description15
,y.Data15
,y.Short_Description16
,y.Data16
,y.Short_Description17
,y.Data17
,y.Short_Description18
,y.Data18
,y.Short_Description19
,y.Data19
,y.Short_Description20
,y.Data20
,y.Short_Description21
,y.Data21
,y.Short_Description22
,y.Data22
,y.Short_Description23
,y.Data23
,y.Short_Description24
,y.Data24
,y.Short_Description25
,y.Data25
,y.Short_Description26
,y.Data26
,y.Short_Description27
,y.Data27
,y.Short_Description28
,y.Data28
,y.Short_Description29
,y.Data29
,y.Short_Description30
,y.Data30
,y.Short_Description31
,y.Data31
,y.Short_Description32
,y.Data32
,y.Short_Description33
,y.Data33
,y.Short_Description34
,y.Data34
,y.Short_Description35
,y.Data35
,y.Short_Description36
,y.Data36
,y.Short_Description37
,y.Data37
,y.Short_Description38
,y.Data38
,y.Short_Description39
,y.Data39
,y.Short_Description40
,y.Data40
,y.Short_Description41
,y.Data41
,y.Short_Description42
,y.Data42
,y.Short_Description43
,y.Data43
,y.Short_Description44
,y.Data44
,y.Short_Description45
,y.Data45
,y.Short_Description46
,y.Data46
,y.Short_Description47
,y.Data47
,y.Short_Description48
,y.Data48
,y.Short_Description49
,y.Data49
,y.Short_Description50
,y.Data50
,y.Short_Description51
,y.Data51
,y.Short_Description52
,y.Data52
,y.Short_Description53
,y.Data53
,y.Short_Description54
,y.Data54
,y.Short_Description55
,y.Data55
,y.Short_Description56
,y.Data56
,y.Short_Description57
,y.Data57
,y.Short_Description58
,y.Data58
,y.Short_Description59
,y.Data59
,y.Short_Description60
,y.Data60
,y.Short_Description61
,y.Data61
,y.Short_Description62
,y.Data62
,y.Short_Description63
,y.Data63
,y.Short_Description64
,y.Data64
,y.Short_Description65
,y.Data65
,y.Short_Description66
,y.Data66
,y.Short_Description67
,y.Data67
,y.Short_Description68
,y.Data68
,y.Short_Description69
,y.Data69
,y.Short_Description70
,y.Data70
,y.Short_Description71
,y.Data71
,y.Short_Description72
,y.Data72
,y.Short_Description73
,y.Data73
,y.Short_Description74
,y.Data74
,y.Short_Description75
,y.Data75
,y.Short_Description76
,y.Data76
,y.Short_Description77
,y.Data77
,y.Short_Description78
,y.Data78
,y.Short_Description79
,y.Data79
,y.Short_Description80
,y.Data80
,y.Short_Description81
,y.Data81
,y.Short_Description82
,y.Data82
,y.Short_Description83
,y.Data83
,y.Short_Description84
,y.Data84
,y.Short_Description85
,y.Data85
,y.Short_Description86
,y.Data86
,y.Short_Description87
,y.Data87
,y.Short_Description88
,y.Data88
,y.Short_Description89
,y.Data89
,y.Short_Description90
,y.Data90
,y.Short_Description91
,y.Data91
,y.Short_Description92
,y.Data92
,y.Short_Description93
,y.Data93
,y.Short_Description94
,y.Data94
,y.Short_Description95
,y.Data95
,y.Short_Description96
,y.Data96
,y.Short_Description97
,y.Data97
,y.Short_Description98
,y.Data98
,y.Short_Description99
,y.Data99
,y.Short_Description100
,y.Data100
,x.rownum
FROM qryIndividualTestData100Descriptions AS x
INNER JOIN IndividualTestData100 AS y ON (x.Short_Description1 =
y.Short_Description1)
AND (x.Short_Description2 = y.Short_Description2)
AND (x.Short_Description3 = y.Short_Description3)
AND (x.Short_Description4 = y.Short_Description4)
AND (x.Short_Description5 = y.Short_Description5)
AND (x.Short_Description6 = y.Short_Description6)
AND (x.Short_Description7 = y.Short_Description7)
AND (x.Short_Description8 = y.Short_Description8)
AND (x.Short_Description9 = y.Short_Description9)
AND (x.Short_Description10 = y.Short_Description10)
AND (x.Short_Description11 = y.Short_Description11)
AND (x.Short_Description12 = y.Short_Description12)
AND (x.Short_Description13 = y.Short_Description13)
AND (x.Short_Description14 = y.Short_Description14)
AND (x.Short_Description15 = y.Short_Description15)
AND (x.Short_Description16 = y.Short_Description16)
AND (x.Short_Description17 = y.Short_Description17)
AND (x.Short_Description18 = y.Short_Description18)
AND (x.Short_Description19 = y.Short_Description19)
AND (x.Short_Description20 = y.Short_Description20)
AND (x.Short_Description21 = y.Short_Description21)
AND (x.Short_Description22 = y.Short_Description22)
AND (x.Short_Description23 = y.Short_Description23)
AND (x.Short_Description24 = y.Short_Description24)
AND (x.Short_Description25 = y.Short_Description25)
AND (x.Short_Description26 = y.Short_Description26)
AND (x.Short_Description27 = y.Short_Description27)
AND (x.Short_Description28 = y.Short_Description28)
AND (x.Short_Description29 = y.Short_Description29)
AND (x.Short_Description30 = y.Short_Description30)
AND (x.Short_Description31 = y.Short_Description31)
AND (x.Short_Description32 = y.Short_Description32)
AND (x.Short_Description33 = y.Short_Description33)
AND (x.Short_Description34 = y.Short_Description34)
AND (x.Short_Description35 = y.Short_Description35)
AND (x.Short_Description36 = y.Short_Description36)
AND (x.Short_Description37 = y.Short_Description37)
AND (x.Short_Description38 = y.Short_Description38)
AND (x.Short_Description39 = y.Short_Description39)
AND (x.Short_Description40 = y.Short_Description40)
AND (x.Short_Description41 = y.Short_Description41)
AND (x.Short_Description42 = y.Short_Description42)
AND (x.Short_Description43 = y.Short_Description43)
AND (x.Short_Description44 = y.Short_Description44)
AND (x.Short_Description45 = y.Short_Description45)
AND (x.Short_Description46 = y.Short_Description46)
AND (x.Short_Description47 = y.Short_Description47)
AND (x.Short_Description48 = y.Short_Description48)
AND (x.Short_Description49 = y.Short_Description49)
AND (x.Short_Description50 = y.Short_Description50)
AND (x.Short_Description51 = y.Short_Description51)
AND (x.Short_Description52 = y.Short_Description52)
AND (x.Short_Description53 = y.Short_Description53)
AND (x.Short_Description54 = y.Short_Description54)
AND (x.Short_Description55 = y.Short_Description55)
AND (x.Short_Description56 = y.Short_Description56)
AND (x.Short_Description57 = y.Short_Description57)
AND (x.Short_Description58 = y.Short_Description58)
AND (x.Short_Description59 = y.Short_Description59)
AND (x.Short_Description60 = y.Short_Description60)
AND (x.Short_Description61 = y.Short_Description61)
AND (x.Short_Description62 = y.Short_Description62)
AND (x.Short_Description63 = y.Short_Description63)
AND (x.Short_Description64 = y.Short_Description64)
AND (x.Short_Description65 = y.Short_Description65)
AND (x.Short_Description66 = y.Short_Description66)
AND (x.Short_Description67 = y.Short_Description67)
AND (x.Short_Description68 = y.Short_Description68)
AND (x.Short_Description69 = y.Short_Description69)
AND (x.Short_Description70 = y.Short_Description70)
AND (x.Short_Description71 = y.Short_Description71)
AND (x.Short_Description72 = y.Short_Description72)
AND (x.Short_Description73 = y.Short_Description73)
AND (x.Short_Description74 = y.Short_Description74)
AND (x.Short_Description75 = y.Short_Description75)
AND (x.Short_Description76 = y.Short_Description76)
AND (x.Short_Description77 = y.Short_Description77)
AND (x.Short_Description78 = y.Short_Description78)
AND (x.Short_Description79 = y.Short_Description79)
AND (x.Short_Description80 = y.Short_Description80)
AND (x.Short_Description81 = y.Short_Description81)
AND (x.Short_Description82 = y.Short_Description82)
AND (x.Short_Description83 = y.Short_Description83)
AND (x.Short_Description84 = y.Short_Description84)
AND (x.Short_Description85 = y.Short_Description85)
AND (x.Short_Description86 = y.Short_Description86)
AND (x.Short_Description87 = y.Short_Description87)
AND (x.Short_Description88 = y.Short_Description88)
AND (x.Short_Description89 = y.Short_Description89)
AND (x.Short_Description90 = y.Short_Description90)
AND (x.Short_Description91 = y.Short_Description91)
AND (x.Short_Description92 = y.Short_Description92)
AND (x.Short_Description93 = y.Short_Description93)
AND (x.Short_Description94 = y.Short_Description94)
AND (x.Short_Description95 = y.Short_Description95)
AND (x.Short_Description96 = y.Short_Description96)
AND (x.Short_Description97 = y.Short_Description97)
AND (x.Short_Description98 = y.Short_Description98)
AND (x.Short_Description99 = y.Short_Description99)
AND (x.Short_Description100 = y.Short_Description100)
WHERE (
((y.[Part_Number]) LIKE [pn] & "*" & [tt])
AND (
(y.[Serial_Number]) >= [sns]
AND (y.[Serial_Number]) <= [sne]
)
AND (
(y.[Test_Date]) >= [ds]
AND (y.[Test_Date]) <= [de]
)
);
Access Generated version
PARAMETERS pn TEXT (255)
,tt TEXT (255)
,sns Long
,sne Long
,ds DATETIME
,de DATETIME;
SELECT y.Short_Description1
,y.Data1
,y.Short_Description2
,y.Data2
,y.Short_Description3
,y.Data3
,y.Short_Description4
,y.Data4
,y.Short_Description5
,y.Data5
,y.Short_Description6
,y.Data6
,y.Short_Description7
,y.Data7
,y.Short_Description8
,y.Data8
,y.Short_Description9
,y.Data9
,y.Short_Description10
,y.Data10
,y.Short_Description11
,y.Data11
,y.Short_Description12
,y.Data12
,y.Short_Description13
,y.Data13
,y.Short_Description14
,y.Data14
,y.Short_Description15
,y.Data15
,y.Short_Description16
,y.Data16
,y.Short_Description17
,y.Data17
,y.Short_Description18
,y.Data18
,y.Short_Description19
,y.Data19
,y.Short_Description20
,y.Data20
,y.Short_Description21
,y.Data21
,y.Short_Description22
,y.Data22
,y.Short_Description23
,y.Data23
,y.Short_Description24
,y.Data24
,y.Short_Description25
,y.Data25
,y.Short_Description26
,y.Data26
,y.Short_Description27
,y.Data27
,y.Short_Description28
,y.Data28
,y.Short_Description29
,y.Data29
,y.Short_Description30
,y.Data30
,y.Short_Description31
,y.Data31
,y.Short_Description32
,y.Data32
,y.Short_Description33
,y.Data33
,y.Short_Description34
,y.Data34
,y.Short_Description35
,y.Data35
,y.Short_Description36
,y.Data36
,y.Short_Description37
,y.Data37
,y.Short_Description38
,y.Data38
,y.Short_Description39
,y.Data39
,y.Short_Description40
,y.Data40
,y.Short_Description41
,y.Data41
,y.Short_Description42
,y.Data42
,y.Short_Description43
,y.Data43
,y.Short_Description44
,y.Data44
,y.Short_Description45
,y.Data45
,y.Short_Description46
,y.Data46
,y.Short_Description47
,y.Data47
,y.Short_Description48
,y.Data48
,y.Short_Description49
,y.Data49
,y.Short_Description50
,y.Data50
,y.Short_Description51
,y.Data51
,y.Short_Description52
,y.Data52
,y.Short_Description53
,y.Data53
,y.Short_Description54
,y.Data54
,y.Short_Description55
,y.Data55
,y.Short_Description56
,y.Data56
,y.Short_Description57
,y.Data57
,y.Short_Description58
,y.Data58
,y.Short_Description59
,y.Data59
,y.Short_Description60
,y.Data60
,y.Short_Description61
,y.Data61
,y.Short_Description62
,y.Data62
,y.Short_Description63
,y.Data63
,y.Short_Description64
,y.Data64
,y.Short_Description65
,y.Data65
,y.Short_Description66
,y.Data66
,y.Short_Description67
,y.Data67
,y.Short_Description68
,y.Data68
,y.Short_Description69
,y.Data69
,y.Short_Description70
,y.Data70
,y.Short_Description71
,y.Data71
,y.Short_Description72
,y.Data72
,y.Short_Description73
,y.Data73
,y.Short_Description74
,y.Data74
,y.Short_Description75
,y.Data75
,y.Short_Description76
,y.Data76
,y.Short_Description77
,y.Data77
,y.Short_Description78
,y.Data78
,y.Short_Description79
,y.Data79
,y.Short_Description80
,y.Data80
,y.Short_Description81
,y.Data81
,y.Short_Description82
,y.Data82
,y.Short_Description83
,y.Data83
,y.Short_Description84
,y.Data84
,y.Short_Description85
,y.Data85
,y.Short_Description86
,y.Data86
,y.Short_Description87
,y.Data87
,y.Short_Description88
,y.Data88
,y.Short_Description89
,y.Data89
,y.Short_Description90
,y.Data90
,y.Short_Description91
,y.Data91
,y.Short_Description92
,y.Data92
,y.Short_Description93
,y.Data93
,y.Short_Description94
,y.Data94
,y.Short_Description95
,y.Data95
,y.Short_Description96
,y.Data96
,y.Short_Description97
,y.Data97
,y.Short_Description98
,y.Data98
,y.Short_Description99
,y.Data99
,y.Short_Description100
,y.Data100
,x.rownum
FROM qryIndividualTestData100Descriptions AS x
INNER JOIN individualtestdata100 AS y ON (x.Short_Description100 =
y.Short_Description100)
AND (x.Short_Description99 = y.Short_Description99)
AND (x.Short_Description98 = y.Short_Description98)
AND (x.Short_Description97 = y.Short_Description97)
AND (x.Short_Description96 = y.Short_Description96)
AND (x.Short_Description95 = y.Short_Description95)
AND (x.Short_Description94 = y.Short_Description94)
AND (x.Short_Description93 = y.Short_Description93)
AND (x.Short_Description92 = y.Short_Description92)
AND (x.Short_Description91 = y.Short_Description91)
AND (x.Short_Description90 = y.Short_Description90)
AND (x.Short_Description89 = y.Short_Description89)
AND (x.Short_Description88 = y.Short_Description88)
AND (x.Short_Description87 = y.Short_Description87)
AND (x.Short_Description86 = y.Short_Description86)
AND (x.Short_Description85 = y.Short_Description85)
AND (x.Short_Description84 = y.Short_Description84)
AND (x.Short_Description83 = y.Short_Description83)
AND (x.Short_Description82 = y.Short_Description82)
AND (x.Short_Description81 = y.Short_Description81)
AND (x.Short_Description80 = y.Short_Description80)
AND (x.Short_Description79 = y.Short_Description79)
AND (x.Short_Description78 = y.Short_Description78)
AND (x.Short_Description77 = y.Short_Description77)
AND (x.Short_Description76 = y.Short_Description76)
AND (x.Short_Description75 = y.Short_Description75)
AND (x.Short_Description74 = y.Short_Description74)
AND (x.Short_Description73 = y.Short_Description73)
AND (x.Short_Description72 = y.Short_Description72)
AND (x.Short_Description71 = y.Short_Description71)
AND (x.Short_Description70 = y.Short_Description70)
AND (x.Short_Description69 = y.Short_Description69)
AND (x.Short_Description68 = y.Short_Description68)
AND (x.Short_Description67 = y.Short_Description67)
AND (x.Short_Description66 = y.Short_Description66)
AND (x.Short_Description65 = y.Short_Description65)
AND (x.Short_Description64 = y.Short_Description64)
AND (x.Short_Description63 = y.Short_Description63)
AND (x.Short_Description62 = y.Short_Description62)
AND (x.Short_Description61 = y.Short_Description61)
AND (x.Short_Description60 = y.Short_Description60)
AND (x.Short_Description59 = y.Short_Description59)
AND (x.Short_Description58 = y.Short_Description58)
AND (x.Short_Description57 = y.Short_Description57)
AND (x.Short_Description56 = y.Short_Description56)
AND (x.Short_Description55 = y.Short_Description55)
AND (x.Short_Description54 = y.Short_Description54)
AND (x.Short_Description53 = y.Short_Description53)
AND (x.Short_Description52 = y.Short_Description52)
AND (x.Short_Description51 = y.Short_Description51)
AND (x.Short_Description50 = y.Short_Description50)
AND (x.Short_Description49 = y.Short_Description49)
AND (x.Short_Description48 = y.Short_Description48)
AND (x.Short_Description47 = y.Short_Description47)
AND (x.Short_Description46 = y.Short_Description46)
AND (x.Short_Description45 = y.Short_Description45)
AND (x.Short_Description44 = y.Short_Description44)
AND (x.Short_Description43 = y.Short_Description43)
AND (x.Short_Description42 = y.Short_Description42)
AND (x.Short_Description41 = y.Short_Description41)
AND (x.Short_Description40 = y.Short_Description40)
AND (x.Short_Description39 = y.Short_Description39)
AND (x.Short_Description38 = y.Short_Description38)
AND (x.Short_Description37 = y.Short_Description37)
AND (x.Short_Description36 = y.Short_Description36)
AND (x.Short_Description35 = y.Short_Description35)
AND (x.Short_Description34 = y.Short_Description34)
AND (x.Short_Description33 = y.Short_Description33)
AND (x.Short_Description32 = y.Short_Description32)
AND (x.Short_Description31 = y.Short_Description31)
AND (x.Short_Description30 = y.Short_Description30)
AND (x.Short_Description29 = y.Short_Description29)
AND (x.Short_Description28 = y.Short_Description28)
AND (x.Short_Description27 = y.Short_Description27)
AND (x.Short_Description26 = y.Short_Description26)
AND (x.Short_Description25 = y.Short_Description25)
AND (x.Short_Description24 = y.Short_Description24)
AND (x.Short_Description23 = y.Short_Description23)
AND (x.Short_Description22 = y.Short_Description22)
AND (x.Short_Description21 = y.Short_Description21)
AND (x.Short_Description20 = y.Short_Description20)
AND (x.Short_Description19 = y.Short_Description19)
AND (x.Short_Description18 = y.Short_Description18)
AND (x.Short_Description17 = y.Short_Description17)
AND (x.Short_Description16 = y.Short_Description16)
AND (x.Short_Description15 = y.Short_Description15)
AND (x.Short_Description14 = y.Short_Description14)
AND (x.Short_Description13 = y.Short_Description13)
AND (x.Short_Description12 = y.Short_Description12)
AND (x.Short_Description11 = y.Short_Description11)
AND (x.Short_Description10 = y.Short_Description10)
AND (x.Short_Description9 = y.Short_Description9)
AND (x.Short_Description8 = y.Short_Description8)
AND (x.Short_Description7 = y.Short_Description7)
AND (x.Short_Description6 = y.Short_Description6)
AND (x.Short_Description5 = y.Short_Description5)
AND (x.Short_Description4 = y.Short_Description4)
AND (x.Short_Description3 = y.Short_Description3)
AND (x.Short_Description2 = y.Short_Description2)
AND (x.Short_Description1 = y.Short_Description1)
WHERE (
((y.Part_Number) LIKE [pn] & "*" & [tt])
AND (
(y.Serial_Number) >= [sns]
AND (y.Serial_Number) <= [sne]
)
AND (
(y.Test_Date) >= [ds]
AND (y.Test_Date) <= [de]
)
);
Why does access choke on the query?
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