Generating Sequence with conditions [message #653483] |
Thu, 07 July 2016 02:13  |
 |
udaykuditipudi
Messages: 8 Registered: July 2016 Location: hyderabad
|
Junior Member |
|
|
Hii
---------------
IN ORACLE FORMS
---------------
My requirement is
In front end FORM if i add a data in lines it should generate line numbers a sequence as
1
2
3
4
After saving the form
if i re-query the form and open it
the line numbers should be
1
2
3
4
So here i have the main requirement.
After saving and querying then
IF I DON'T NEED THE LINE 2 IF I DELETE IT AND I WANT TO ADD A NEW LINE TO THAT FORM IT SHOULD GENERATE THE LINE NO AS
1
3
4
5
[Updated on: Thu, 07 July 2016 02:23] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: Generating Sequence with conditions [message #653509 is a reply to message #653501] |
Fri, 08 July 2016 15:13   |
 |
Littlefoot
Messages: 21823 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Here's what I meant: I was thinking of another table which would contain MAX number used so far. The next value would then be "stored value + 1", and - once it is fetched - the table would be updated to reflect that new MAX value. For example, if it was 5, the new value would be 6, and 6 would be then stored in that table.
In order to prevent two (or more) users to select the same value, a function would calculate the next value, store it into a table, COMMIT, and return that value to the caller. Something like this:SQL> CREATE TABLE test_1 (id NUMBER);
Table created.
SQL>
SQL> CREATE OR REPLACE FUNCTION f_max_1
2 RETURN NUMBER
3 AS
4 PRAGMA AUTONOMOUS_TRANSACTION;
5 l_next NUMBER;
6 BEGIN
7 UPDATE test_1
8 SET id = NVL (id, 0) + 1;
9
10 IF SQL%NOTFOUND
11 THEN
12 INSERT INTO test_1 (id)
13 VALUES (1);
14 END IF;
15
16 COMMIT;
17
18 SELECT id INTO l_next FROM test_1;
19
20 RETURN l_next;
21 END;
22 /
Function created.
SQL>
SQL> SELECT f_max_1 FROM DUAL;
F_MAX_1
----------
1
SQL> SELECT f_max_1 FROM DUAL;
F_MAX_1
----------
2
SQL> SELECT f_max_1 FROM DUAL;
F_MAX_1
----------
3
SQL>
|
|
|
Re: Generating Sequence with conditions [message #653556 is a reply to message #653509] |
Mon, 11 July 2016 03:27   |
cookiemonster
Messages: 13965 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
You might as well use a sequence. As soon as a user calls that function and then doesn't save the main insert it'll stop reflecting the data in the main table.
The only way max works is if you take a lock that prevents more than one user at a time from inserting into the main table.
|
|
|
|