Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong count(*) in pl/sql procedure

Oracle 11g Express Edition 11.2.0

Table "RIGHT" data (contains only 1 row with name = Test):

Id | Name

1 Test

2 New2

3 New14

...

SELECT COUNT(*) FROM RIGHT WHERE name = 'Test';

Result = 1

I have procedure in package TEST:

    create or replace
    PACKAGE BODY TEST
    AS
    PROCEDURE FIND_RIGHT(rightName IN VARCHAR2)
    IS
      countrows       NUMBER;
    BEGIN
      SELECT COUNT(*) INTO countrows FROM RIGHT WHERE name = rightName;

    /* DEBUG POINT !!! in this point countrows = 212 !!!! */
    ...
    END;
    END TEST;

And run it from other procedure:

    DECLARE
      rightName VARCHAR2(200);
    BEGIN
      rightName := 'Test';
      TEST.FIND_RIGHT(rightName);
    END;

I run debugger (in Oracle SQL Developer) and debug point after select. I see countrows = 212.

Why countrows != 1 ???

UPDATED: All transaction is commited. Open only 1 session (from SQL Developer). Table Rights has 3 indexes (table is big, I don't write all colums in the post). Procedures have many input params (custom objects), but I drop extra information.

UPDATED #2: I change code to

    create or replace
    PACKAGE BODY TEST
    AS
    PROCEDURE FIND_RIGHT(rightName IN VARCHAR2)
    IS
      countrows       NUMBER;
      testVar       VARCHAR2(200);
    BEGIN
      testVar := 'Test';
      SELECT COUNT(*) INTO countrows FROM RIGHT WHERE name = testVar;

    /* in this point countrows = 1  */
    ...
    END;
    END TEST;
like image 944
Andrey.Pushin Avatar asked Aug 08 '26 08:08

Andrey.Pushin


1 Answers

I guess your table contains a column called "rightName"?

if this is the case your orginal query would compare the "name" and "rightName" columns instead of using the procedure argument.

Try changing the argument name.

like image 165
kirotnes Avatar answered Aug 10 '26 02:08

kirotnes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!