Cursor in procedure returning more values than query

前端 未结 2 1900
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 08:25

I am using a simple cursor in a procedure that receives a couple of parameters. I then make a cursor on a select query with a where clause with multiple conditions, which ar

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 08:43

    You have a name conflict. You have called your local variables the same as your column names, and the column names are taking precedence, as noted in the documentation:

    If a SQL statement references a name that belongs to both a column and either a local variable or formal parameter, then the column name takes precedence.

    Caution:
    When a variable or parameter name is interpreted as a column name, data can be deleted, changed, or inserted unintentionally.

    The first four checks are always going to be true (unless you have null values), so you'll get every row that has done = 'N'.

    Change your local variable names to something else; it's fairly common to use a prefix to distinguish between local variables, parameters, and columns, something like:

    Cursor linija IS 
    SELECT *
    FROM table_x X
    where x.mjt = l_mjt
    and   x.salesman = l_salesman
    and x.kind = l_kind
    and x.kolo1 = l_kolo1
    and x.done = 'N';
    

    If this is in a stored procedure, rather than an anonymous block, you could use the procedure/function name as a prefix, which some people prefer. If your procedure was called myproc, for example, you could do:

    Cursor linija IS 
    SELECT *
    FROM table_x X
    where x.mjt = myproc.mjt
    and   x.salesman = myproc.salesman
    and x.kind = myproc.kind
    and x.kolo1 = myproc.kolo1
    and x.done = 'N';
    

提交回复
热议问题