Subject | Strange variable assignment - a bug or not? |
---|---|
Author | ionutz gorea |
Post date | 2010-03-18T14:11:12Z |
Environment: Firebird 2.1.3 Classic - Linux 64 bit
DDL:
/* first table */
CREATE TABLE TEST_A (
CODE CHAR(2) DEFAULT '' NOT NULL,
VAL NUMERIC(12,2) DEFAULT 0 NOT NULL
);
/* second table */
CREATE TABLE TEST_B (
CODE CHAR(2) DEFAULT '' NOT NULL,
VAL NUMERIC(12,2) DEFAULT 0 NOT NULL
);
/* test procedure*/
CREATE OR ALTER PROCEDURE TEST_PROC
RETURNS (
CODE CHAR(2),
RESULT NUMERIC(12,2))
AS
DECLARE VARIABLE M_CODE CHAR(2) = '';
DECLARE VARIABLE M_VAL_A NUMERIC(12,2) = 0;
DECLARE VARIABLE M_VAL_B NUMERIC(12,2) = 0;
DECLARE CURSOR_A CURSOR FOR (
SELECT CODE, VAL
FROM TEST_A);
DECLARE CURSOR_B CURSOR FOR (
SELECT COALESCE(SUM(VAL), 0) AS TOTAL_B
FROM TEST_B
WHERE CODE = :M_CODE
GROUP BY CODE);
BEGIN
OPEN CURSOR_A;
WHILE (1=1) DO
BEGIN
FETCH CURSOR_A INTO :M_CODE, :M_VAL_A;
M_VAL_A=COALESCE(M_VAL_A,0);
IF(ROW_COUNT=0) THEN
LEAVE;
ELSE
BEGIN
OPEN CURSOR_B;
WHILE (1=1) DO
BEGIN
FETCH CURSOR_B INTO :M_VAL_B;
M_VAL_B=COALESCE(M_VAL_B,0);
IF(ROW_COUNT=0) THEN
LEAVE;
END
CLOSE CURSOR_B;
CODE = M_CODE;
RESULT = M_VAL_A-M_VAL_B;
SUSPEND;
END
END
CLOSE CURSOR_A;
END
DML for the test case:
INSERT INTO TEST_A (CODE, VAL) VALUES ('01', 100);
INSERT INTO TEST_A (CODE, VAL) VALUES ('02', 90);
INSERT INTO TEST_B (CODE, VAL) VALUES ('01', 80);
Running the procedure:
SELECT * FROM TEST_PROC;
from ISQL I get the following output:
CODE RESULT
====== =====================
01 20.00
02 10.00
Shouldn't be instead:
CODE RESULT
====== =====================
01 20.00
02 90.00
Also running the procedure from IBExpert I get the wrong output (as expected) but if I choose to "debug procedure" I get the correct output.
Is this a normal behavior or should I report it to the bug tracker?
Ionut
[Non-text portions of this message have been removed]
DDL:
/* first table */
CREATE TABLE TEST_A (
CODE CHAR(2) DEFAULT '' NOT NULL,
VAL NUMERIC(12,2) DEFAULT 0 NOT NULL
);
/* second table */
CREATE TABLE TEST_B (
CODE CHAR(2) DEFAULT '' NOT NULL,
VAL NUMERIC(12,2) DEFAULT 0 NOT NULL
);
/* test procedure*/
CREATE OR ALTER PROCEDURE TEST_PROC
RETURNS (
CODE CHAR(2),
RESULT NUMERIC(12,2))
AS
DECLARE VARIABLE M_CODE CHAR(2) = '';
DECLARE VARIABLE M_VAL_A NUMERIC(12,2) = 0;
DECLARE VARIABLE M_VAL_B NUMERIC(12,2) = 0;
DECLARE CURSOR_A CURSOR FOR (
SELECT CODE, VAL
FROM TEST_A);
DECLARE CURSOR_B CURSOR FOR (
SELECT COALESCE(SUM(VAL), 0) AS TOTAL_B
FROM TEST_B
WHERE CODE = :M_CODE
GROUP BY CODE);
BEGIN
OPEN CURSOR_A;
WHILE (1=1) DO
BEGIN
FETCH CURSOR_A INTO :M_CODE, :M_VAL_A;
M_VAL_A=COALESCE(M_VAL_A,0);
IF(ROW_COUNT=0) THEN
LEAVE;
ELSE
BEGIN
OPEN CURSOR_B;
WHILE (1=1) DO
BEGIN
FETCH CURSOR_B INTO :M_VAL_B;
M_VAL_B=COALESCE(M_VAL_B,0);
IF(ROW_COUNT=0) THEN
LEAVE;
END
CLOSE CURSOR_B;
CODE = M_CODE;
RESULT = M_VAL_A-M_VAL_B;
SUSPEND;
END
END
CLOSE CURSOR_A;
END
DML for the test case:
INSERT INTO TEST_A (CODE, VAL) VALUES ('01', 100);
INSERT INTO TEST_A (CODE, VAL) VALUES ('02', 90);
INSERT INTO TEST_B (CODE, VAL) VALUES ('01', 80);
Running the procedure:
SELECT * FROM TEST_PROC;
from ISQL I get the following output:
CODE RESULT
====== =====================
01 20.00
02 10.00
Shouldn't be instead:
CODE RESULT
====== =====================
01 20.00
02 90.00
Also running the procedure from IBExpert I get the wrong output (as expected) but if I choose to "debug procedure" I get the correct output.
Is this a normal behavior or should I report it to the bug tracker?
Ionut
[Non-text portions of this message have been removed]