Pergunta

I have variables inside stored procedure has a behavior I dont understand, the variables inside a loop retain the previous loop value !!

Here is the stored procedure

v_no = 3;
v_counter = 1;
is_special = 1;

  while (:v_counter <= :v_no) do begin

    if (:is_special = 1) then begin

      select tmaster.axis, tdetail.dim_val
        from tdetail
        right outer join tmaster on (tdetail.sp_id = tmaster.sp_id)
        where (tmaster.sp_id = 17) And (tdetail.pos = :v_counter)
        into :v_axis, :v_dim_val;  /* variable retain previous loop values */

        /* ..... more code here */

    end

  end

I have here two tables, tmaster as a master table and tdetail as detail table. When executing the Select statement alone in the above code with v_counter = 1,2, and 3 should result in

axis    dim_val
----------------
X       14
null    null
X       14

but instead when v_counter = 2 I get 'X' for axis and 14 for dim_val !!, as a work around I set both variables to null in the first of each loop directly after the while statement, but why the variables :v_axis and :v_dim_val was not assigned to null when v_counter = 2 and why they retain previous loop values ??

Foi útil?

Solução

I suspect that you actually don't have a record in tdetail where pos = 2, right? Thus the result of the select query is empty resultset, not row where all fields are NULL. As there is no rows in the resultset, there is nothing to assign to the variables (:v_axis, :v_dim_val) and thus they retain their values.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top