Oracle Update row with minimum value -


i'm trying give kind of unique random code users without skipping number in range. table this

randomcode | isused | rnum -------------------------- 002        | y      | 1 004        | y      | 2 003        | null   | 3 005        | y      | 4 001        | null   | 5 

so next randomcode use should 003 , 001. problem user can return code , should give number someonelse. in case i'm setting isused null.

i've tried selecting minimum rnum isused column null. , updading table. encountered race condition , users got duplicate randomcode.

any to

update table set isused = n'y' isused = null , rnum = min(rnum) returning

you made 2 little mistakes in code:

  1. you did isused = null.

    this condition returns false. nothing equates null. use isused null instead.

  2. you used rnum = min(rnum). use subquery instead.

here's query these simple modifications.

update table set isused = n'y' isused null , rnum = (select min(rnum) table isused null) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -