Currval
Hello,
I get following error when i do the following
select seq1.currval "value" from dual
ORA-08002: sequence SEQ1.CURRVAL is not yet defined in this
session (DBD ERROR: OCIStmtExecute)
from within the browser via CGI. but i can do the same from
within SQL/PLUS and i get correct result.
i can also do
select seq1.nextval from dual
from within Browser via CGI but not seq1.currval.
any help would be appreciated.
alex
null
A Sequence is just like a stack of numbers. When you issue the
statement, select seq1.nextval from dual, you'll get the first
number from the top of the stack. The moment your statement gets
executed that number is gone for good. Nobody else will see that
number. Sequences are primarly used for populating primary keys
as you insert records into a table. Regrding 1 user vs 500
users, the notion is that each user is selecting a value from
the sequence for a purpose! Assume all these 500 guys are
inserting records into a single table and they are using a
sequence to synchronize the promary key. Each of them must have
a different number.
Bijoy
Alex Korneyev (guest) wrote:
: Jim,
: let's say i issue a statement(from within VB program )
: insert into some_table values(seq1.nextval)
: how would i get THAT seq1.nextval ??? every time i do
: seq1.nextval i increment nextval so if i
: "select seq1.nextval from dual" i will get an answer all
right,
: but that will be the next value not current!!! that would not
be
: a problem if i had one user connecting to the database, but
what
: if i have 500 !!!! that means my numbers will grow at e^(some
: power) REAL FAST!!! have any ideas?
: alex
: : Sequences are defined in your session once you select the
: : "nextval". If you log into SQL/Plus and try a: Select
: : xxx.currval..., you will get the error messsage you stated
: : above. The current value only refers to the value of YOUR
last
: : selected "nextval" statement. It does not show you the
current
: : value of the sequence.
: : example:
: : Select XXX."nextval" returns a 23
: : Select XXX."currval" returns a 23 (even though the
sequence
: : may be at 10,000 by now).
: : I am using sequences in Pro*C CGI scripts and have not had
any
: : issues yet. Although, I usually select the "nextval" into a
: : variable. Because of this, I do not need to query the
: "currval".
: : Hope this helps.
: : -Jim
null
Similar Messages
-
Problem with Union and Currval/NextVal
Hello,
I think I may have found a bug with 8.1.5 but I am not sure. Here is the SQL I am trying to execute:
insert into LFI_TEST_OWNER.CURRVAL_TEST_tb
SELECT lfi_seq_num_sg.currval
FROM DUAL
UNION ALL
SELECT lfi_seq_num_sg.currval
FROM DUAL
When I execute this SQL statement the value inserted into the table is NULL. However when I remove the union and one of the queries then it works as expected.
Any information on if this is being worked on by Oracle or if it has been fixed would be greatly appreciated.
Thanks
Jeff MHi Jeff,
Nice one. I feel it may not be a bug but as NEXTVAL and CURRVAL are pseudo columns they might not be able to get store in the memory to sort the 2 queries.
Infact, if you use anyother function in the dual select statment it will return only one row despite of the 2 select statments in the SQL statement. I tried like this,
SQL> select sysdate from dual
2 union
3 select sysdate from dual;
SYSDATE
19-JUL-01
I got only one record. Then I tried like this,
SQL> select s1.nextval from dual
2 union
3 select s1.currval from dual;
NEXTVAL
I have given NEXTVAL in the first stmt, still it is returning NULL value only. So I feel it might be PSEUDOCOLUMN effect.
Any guys who knows better answer are appreciable.
Thanks
Srinivas -
Issue with sequence.currval in INSERT ALL...
Hi Gurus,
I am having an issue with using sequence.currval in INSERT ALL with WHEN clause.Please check following example
Let say I have 4 tables
1) SOURCE_PARENT
PARENT_ID
========
10
20
2) SOURCE_CHILD
PARENT_ID CHILD_ID
========= ========
10 1
10 2
20 1
20 2
Now i have another 2 tables same as above definition(DEST_PARENT, DEST_CHILD),which i want to populate with above source data with new parent_id's from a sequence.
So I have used INSERT ALL as follows.
=====================================================
INSERT ALL
WHEN (prev_parent_id = 0)THEN
INTO dest_parent
values(test_s.nextval)
when 1 = 1 then
INTO dest_child
values(test_s.currval,child_id)
select
p.parent_id
,c.child_id
,LAG(c.parent_id, 1, 0) OVER (PARTITION BY c.parent_id ORDER BY c.child_id) prev_parent_id
from
source_parent p
,source_child c
where
p.parent_id = c.parent_id
order by p.parent_id
========================================================
Above INSERT ALL created following rows in DEST tables
1) DEST_PARENT
PARENT_ID
=========
75
77
2)DEST_CHILD
PARENT_ID CHILD_ID
========= =======
75 1
76 2 --- this should be same as above record
77 1
78 2 --this should be same as above record
As you can see test_s.nextval is executed 4 times even we do have WHEN Clause in INSERT ALL
Can you please check what's wrong in this INSERT ALL statement?
(or)
Any otherway of doing these INSERTS in a single SQL statement?
Any help would be appreciated!!
Thanks
srinivas.LSorry, but this solution does not work. I had to remove the constraints on the two tables dest_parent and dest_child just to show you, how bad your solution is
SQL> select * from source_parent;
PARENT_ID
10
20
2 rows selected.
SQL> select * from source_child;
PARENT_ID CHILD_ID
10 1
10 2
20 1
20 2
20 3
5 rows selected.
SQL> select * from dest_parent;
no rows selected
SQL> select * from dest_child;
no rows selected
SQL> insert all when
2 (PREV_PARENT_ID = 0) then into DEST_PARENT
3 values
4 (TEST_S.nextval) when 1 = 1 then into DEST_CHILD
5 values
6 (case when PREV_PARENT_ID != 0 then TEST_S.currval - 1 else TEST_S.currval end,
7 CHILD_ID)
8 select P.PARENT_ID,
9 C.CHILD_ID,
10 LAG(C.PARENT_ID, 1, 0) OVER(partition by C.PARENT_ID order by C.CHILD_ID) PREV_PARENT_ID
11 from SOURCE_PARENT P, SOURCE_CHILD C
12 where P.PARENT_ID = C.PARENT_ID
13 order by P.PARENT_ID
14 /
7 rows created.
SQL> select * from dest_parent;
PARENT_ID
23
26
2 rows selected.
SQL> select * from dest_child;
PARENT_ID CHILD_ID
21 2 -- ??? !!!
23 1
23 2
24 3 -- !!!
26 1
5 rows selected.
SQL> -
Sqlldr using Nextval and Currval to load 3 databases
I am trying to load 3 tables from a flat file and when i use the Nextval on a sequence field it loads the table fine, the issue is with the 3rd check below when i try to sequence the records on that table using the same sequence field from the first check. I get the records loaded to the table on the 3rd check but the sequence number is repeating and not staying insynce with the records loaded from the first check. and yes, i have reset all sequence counters.
INTO TABLE "STAFFING"."WB_TEMP_PUNCH_IN"
TRUNCATE
WHEN (80:80) = '1'
se "temp_punch_in_seq.nextval",
Badge_Id POSITION(7:16),
Payroll_Date POSITION(68:71),
Start_Punch POSITION(72:75),
PUNCH_IN_1 POSITION(72:75),
PUNCH_OUT_1 POSITION(76:79)
INTO TABLE "STAFFING"."WB_TEMP_PUNCH_HRS"
TRUNCATE
WHEN (80:80) = '2'
se1 "temp_punch_hrs_seq.nextval",
Badge_Id POSITION(7:16),
Actual_hrs POSITION(26:30),
PUNCH_IN_2 POSITION(17:20),
PUNCH_OUT_2 POSITION(21:24),
PUNCH_IN_3 POSITION(42:45),
PUNCH_OUT_3 POSITION(46:49),
PUNCH_IN_4 POSITION(50:53),
PUNCH_OUT_4 POSITION(54:57),
PUNCH_IN_5 POSITION(58:61),
PUNCH_OUT_5 POSITION(62:65),
PUNCH_IN_6 POSITION(66:69),
PUNCH_OUT_6 POSITION(70:73)
INTO TABLE "STAFFING"."WB_TEMP_PUNCH"
TRUNCATE
WHEN (80:80) = '3'
se2 "temp_punch_in_seq.currval",
Badge_Id POSITION(7:16),
PUNCH_IN_7 POSITION(17:20),
PUNCH_OUT_7 POSITION(21:24),
PUNCH_IN_8 POSITION(29:32),
PUNCH_OUT_8 POSITION(29:32),
PUNCH_IN_9 POSITION(33:36),
PUNCH_OUT_9 POSITION(37:40),
PUNCH_IN_10 POSITION(41:44),
PUNCH_OUT_10 POSITION(45:48)
)Try with "rows=1" when you call sqlldr.
$ more data.ctl
load data
into table t1
col1 "t1_seq.nextval",
col2 position(1:1) integer external
into table t2
col1 "t1_seq.currval",
col2 position(3:3) integer external
$ more data.txt
1,2
3,4
5,6
7,8
$ sqlldr scott/tiger control=data.ctl data=data.txt rows=1
Commit point reached - logical record count 1
Commit point reached - logical record count 2
Commit point reached - logical record count 3
Commit point reached - logical record count 4
SCOTT@orcl> select * from t1;
COL1 COL2
39 1
40 3
41 5
42 7
SCOTT@orcl> select * from t2;
COL1 COL2
39 2
40 4
41 6
42 8HTH
Enrique
P.S. If your problem was solved, consider marking the question as answered -
Behaviour of CurrVal - can anyone explain this?
Hi All,
I am expiriencing some "strange" behaviour of CurrVal.
I will try to represent it with simple expample.
Let's have one table and one sequence:
create table TestCurrVal (A number, b number);
create sequence SeqTestCurrVal start with 1;and some sample data in the table
insert into TestCurrVal(A, B) VALUES (1,1);
insert into TestCurrVal(A, B) VALUES (1,2);
insert into TestCurrVal(A, B) VALUES (2,1);What I want to do is to select all rows from TestCurrVal table ordered by column A. If the value in this coulmn is different by previous value I want to get next sequence value and if it is the same - to use last sequence value. So expected result is the following one:
A B A_PREV PROBLEMATIC_VALUE
1 1 1
1 2 1 1
2 1 1 2I've tried with this statement:
select a, b, a_prev,
case
when a_prev is null or a_prev <> a then SeqTestCurrVal.NextVal
else SeqTestCurrVal.CurrVal
end as problematic_value
from (
select a,
lag(a) over (order by a) AS a_Prev,
b
from TestCurrVal
order by a
);but the result is
A B A_PREV PROBLEMATIC_VALUE
1 1 1
1 2 1 2
2 1 1 3So it seems that on every row it update the sequence even if case clause is not performed and regardless NexVal is not reached.
Do you have any idea why this happens? ...or/and if you have any workaroud? (but without loosing sequence numbers) (...the only workaround that I have in mind is joining the statement with distinct values and generated sequences for each of those values...but maybe something simple can be introduced)
And finally some "cleanup" statements:
drop table TestCurrVal;
drop sequence SeqTestCurrVal; Best Regards
Message was edited by:
MichailThis is how SEQUENCE works. When it is in the SELECT statement, it is always incremented. http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/pseudocolumns002.htm#i1006157
"Within a single SQL statement containing a reference to NEXTVAL, Oracle increments the sequence once: <...> For each row returned by the outer query block of a SELECT statement."
A simple example:
SQL> CREATE SEQUENCE Test;
Sequence created.
SQL>
SQL> SELECT
2 Level,
3 CASE
4 WHEN 1 < 3 THEN Test.NextVal
5 ELSE Test.CurrVal
6 END
7 A
8 FROM
9 Dual
10 CONNECT BY
11 Level <=4;
LEVEL A
1 1
2 2
3 3
4 4
SQL>
SQL> DROP SEQUENCE Test;
Sequence dropped.Basically, Oracle detected the NEXTVAL and will now increment the record for each record. Note, that NEXTVAL and CURRVAL are equivalent in a single statement, because even NEXTVAL stays the same for each record:
SQL> CREATE SEQUENCE Test;
Sequence created.
SQL>
SQL> SELECT
2 Level,
3 Test.NextVal,
4 Test.NextVal
5 FROM
6 Dual
7 CONNECT BY
8 Level <=4;
LEVEL NEXTVAL NEXTVAL
1 1 1
2 2 2
3 3 3
4 4 4
SQL>
SQL> DROP SEQUENCE Test;
Sequence dropped.Therefore, it is probably better to decrement the NEXTVAL by 1 (or whatever the INCREMENT BY value is, which can be queried) and exclude the first record.
SQL> CREATE SEQUENCE Test;
Sequence created.
SQL>
SQL> WITH
2 TestCurrVal
3 AS
4 (
5 SELECT 1 A, 1 B FROM Dual UNION ALL
6 SELECT 1 A, 2 B FROM Dual UNION ALL
7 SELECT 2 A, 1 B FROM Dual
8 )
9 SELECT
10 A,
11 B,
12 A_Prev,
13 Test.NextVal
14 - CASE
15 WHEN A_Prev IS NULL AND Test.NextVal > 1 THEN 1
16 ELSE 0
17 END Problematic_Value
18 FROM
19 (
20 SELECT
21 A,
22 Lag(A) OVER (ORDER BY A) a_Prev,
23 B
24 FROM
25 TestCurrVal
26 ORDER BY
27 A
28 );
A B A_PREV PROBLEMATIC_VALUE
1 1 1
1 2 1 2
2 1 1 3
SQL>
SQL>
SQL> DROP SEQUENCE Test;
Sequence dropped. -
Sequence Last Number doesn't match with currval
Hi,
I have a sequence and i am using that in a procedure to insert records.Often the procedure throws error like primary key violated.
When i checked the last number used by the sequence it gave me as 27331 and the currval of the sequence as 27312.
Please advice us to rectify this.746379 wrote:
Hi,
I have a sequence and i am using that in a procedure to insert records.Often the procedure throws error like primary key violated.
When i checked the last number used by the sequence it gave me as 27331 and the currval of the sequence as 27312.
Please advice us to rectify this.First make sure that you understand the difference between a primary key and a sequence.
A sequence is just a number generator. Oracle guarantees that the sequence will give you a new unique number each time that you call NEXTVAL. If the sequence is cycling then it can even generate the same number again after some time but this is rarely the case.
A primary key is a constaint on your table that requires all values in the column to be unique and not null.
When you get an primary key violated error then you just tried to insert a value that was already there in the table.
Some scenarios might be the reason for this.
<li>You insert a row using the sequence. You insert a new row using the same number again without fetching it from NEXTVAL (e.g. using currval instead of nextval, or not updating a variable that holds the new ID,...)</li>
<li>Somebody else already inserted an ID without using the sequence</li>
<li>A variation is when you import data from another database and resetted the sequences</li>
<li>There is some test data in your tables. The test data was inserted in a high ID range to separate it from the real data. Your sequence just reached this range.</li> -
Sequence : .CURRVAL is not yet defined in this session
Hi ,
I had created a sequence with the name 'myseq' and used next function to retrieve the value.
But when used currval function
I am getting the following error:
sequence MYSEQ.CURRVAL is not yet defined in this session898763 wrote:
Hi ,
I had created a sequence with the name 'myseq' and used next function to retrieve the value.
But when used currval function
I am getting the following error:
sequence MYSEQ.CURRVAL is not yet defined in this sessionYou don't need currval. The typical scenario is either to use the sequence value and store it in a variable. Then reuse this variable. Or better insert into a table that has a db trigger that fills the ID column from the sequence. In the latter case you would use the returning clause to fetch the new ID value.
example pl/sql code
declare
v_empno ename%empno%type;
begin
/* the empno column is filled by a DB-Trigger from a seqeunce */
insert into emp (ename)
values ('Ellison')
returning empno into v_empno;
/* do more stuff here, using the variable v_empno */
end;
/ -
Hello,
Can someone please explain to me the following behavior:
select
OBJECT_ID,
MOD(OBJECT_ID,2),
CASE WHEN MOD(OBJECT_ID,2)=1 then seq_general.nextval else seq_general.currval END
from user_OBJECTS;
returns something like:
OBJ_ID, MOD, SEQ
1,1,1
2,0,2
3,1,3
4,0,4
when instead I was aiming for:
1,1,1
2,0,1
3,1,2
4,0,2
Thank youHi,
The explanation is easy, see [How to Use Sequence Values|http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/sql_elements6a.htm#80564]:
Within a single SQL statement containing a reference to NEXTVAL, Oracle increments the sequence only once:
* For each row returned by the outer query block of a SELECT statement. Such a query block can appear in the following places:
o A top-level SELECT statement
o An INSERT ... SELECT statement (either single-table or multi-table). For a multi-table insert, the reference to NEXTVAL must appear in the VALUES clause, and the sequence is updated once for each row returned by the subquery, even though NEXTVAL may be referenced in multiple branches of the multi-table insert.
o A CREATE TABLE ... AS SELECT statement
o A CREATE MATERIALIZED VIEW ... AS SELECT statement
* For each row updated in an UPDATE statement
* For each INSERT statement containing a VALUES clause
* For row "merged" (either inserted or updated) in a MERGE statement. The reference to NEXTVAL can appear in the merge_insert_clause or the merge_update_clause.
If any of these locations contains more than one reference to NEXTVAL, then Oracle increments the sequence once and returns the same value for all occurrences of NEXTVAL.
If any of these locations contains references to both CURRVAL and NEXTVAL, then Oracle increments the sequence and returns the same value for both CURRVAL and NEXTVAL.
Also see a very good explanation in [Why is output of select seqname.currval,seqname.nextval,seqname.currval,seqname.nextval from dual like this?|http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:21115948632804]
Regards,
Edited by: Walter Fernández on Jul 7, 2009 7:45 PM -
It will be very beneficial if you help me solve this problem and greatly appreciated. Please focus on the problem I’m having not why I’m doing what I’m doing. I’m trying to avoid a cursor solution and I can’t accomplish this using any group by or aggregate/analytical functions. This approach is basically trying to apply a group by while forcing the group by to operate on the data in a specific order(not the group by order). Sounds confusing, but if we focus on the problem… why is currval giving me the nextval we should get somewhere? Thanks.
Query:
INSERT INTO WAREHOUSE_DEV.STG_COV_SUBPROGRAM_FUN_CS1 (
CASENUMBER, PREV_CASENUMBER, CLIENT_NUMBER,
PREV_CLIENT_NUMBER, PROGRAMNAME, PREV_PROGRAMNAME,
SUBPROGRAMNAME, PREV_SUBPROGRAMNAME, COVERAGE_YEARMONTH,
CLIENT_COVERAGE, PREV_CLIENT_COVERAGE, CS_COUNT)
select CASENUMBER, PREV_CASENUMBER, CLIENT_NUMBER,
PREV_CLIENT_NUMBER, PROGRAMNAME, PREV_PROGRAMNAME,
SUBPROGRAMNAME, PREV_SUBPROGRAMNAME, COVERAGE_YEARMONTH,
CLIENT_COVERAGE, PREV_CLIENT_COVERAGE,
case when
CS_COUNT = 0 then 0
else case when CS_COUNT = 1 then 1
else case when (CS_COUNT = -1 AND LAG (CS_COUNT ) OVER (ORDER BY CASENUMBER, CLIENT_NUMBER, PROGRAMNAME, SUBPROGRAMNAME, COVERAGE_YEARMONTH) <> 0 ) then -1
else 1 -- catches tricky restart.....
end
end
end CS_COUNT
from
(select CASENUMBER, PREV_CASENUMBER, CLIENT_NUMBER,
PREV_CLIENT_NUMBER, PROGRAMNAME, PREV_PROGRAMNAME,
SUBPROGRAMNAME, PREV_SUBPROGRAMNAME, COVERAGE_YEARMONTH,
CLIENT_COVERAGE, PREV_CLIENT_COVERAGE,
case when
(CASENUMBER = PREV_CASENUMBER AND CLIENT_NUMBER = PREV_CLIENT_NUMBER AND PROGRAMNAME = PREV_PROGRAMNAME AND SUBPROGRAMNAME = PREV_SUBPROGRAMNAME AND PREV_CLIENT_COVERAGE=0) then 0
else case when (CASENUMBER = PREV_CASENUMBER AND CLIENT_NUMBER = PREV_CLIENT_NUMBER AND PROGRAMNAME = PREV_PROGRAMNAME AND SUBPROGRAMNAME = PREV_SUBPROGRAMNAME AND PREV_CLIENT_COVERAGE=1) then -1 -- EXCEPT FOR WHEN PREV PREV_CLIENT_COVERAGE WAS ZERO THEN 1.
else case when ((CASENUMBER <> PREV_CASENUMBER OR CLIENT_NUMBER <> PREV_CLIENT_NUMBER OR PROGRAMNAME <> PREV_PROGRAMNAME OR SUBPROGRAMNAME <> PREV_SUBPROGRAMNAME) AND PREV_CLIENT_COVERAGE=1) then-1
else case when ((CASENUMBER <> PREV_CASENUMBER OR CLIENT_NUMBER <> PREV_CLIENT_NUMBER OR PROGRAMNAME <> PREV_PROGRAMNAME OR SUBPROGRAMNAME <> PREV_SUBPROGRAMNAME) AND PREV_CLIENT_COVERAGE=0) then 1
else 88
end
end
end
end CS_COUNT
from WAREHOUSE_DEV.STG_COVERAGE_SUBPROGRAM_FUN_CS
where CASENUMBER = '0042432' and client_number = '0000942256'
ORDER BY CASENUMBER, CLIENT_NUMBER, PROGRAMNAME, SUBPROGRAMNAME, COVERAGE_YEARMONTH, CLIENT_COVERAGE);
PRODUCES RESULTS:
0042432 0042419 0000942256 0001571135 Child Care Child Care Child Care Child Care 201008 1 0 1 nextval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201009 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201010 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201011 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201012 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201101 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201102 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201103 0 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201104 1 0 0 0
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201105 1 1 1 nextval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201106 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201107 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201108 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201109 1 1 -1 currval
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201110 1 1 -1 currval
The key is that when you have cs_count of 1… get nexval, when you have value of -1 get currval, zero is zero… The first row would get nexval…. So 1 the next seven rows should be 1, then next row 0, the next row 2 and the last 5 rows will also be 2.
That’s not what happens.
Query with currval and nextval:
Query:
INSERT INTO WAREHOUSE_DEV.STG_COV_SUBPROGRAM_FUN_CS1 (
CASENUMBER, PREV_CASENUMBER, CLIENT_NUMBER,
PREV_CLIENT_NUMBER, PROGRAMNAME, PREV_PROGRAMNAME,
SUBPROGRAMNAME, PREV_SUBPROGRAMNAME, COVERAGE_YEARMONTH,
CLIENT_COVERAGE, PREV_CLIENT_COVERAGE, CS_COUNT)
select CASENUMBER, PREV_CASENUMBER, CLIENT_NUMBER,
PREV_CLIENT_NUMBER, PROGRAMNAME, PREV_PROGRAMNAME,
SUBPROGRAMNAME, PREV_SUBPROGRAMNAME, COVERAGE_YEARMONTH,
CLIENT_COVERAGE, PREV_CLIENT_COVERAGE,
case when
(CS_COUNT = 0) then (0/warehouse_dev.cs_count_seq.nextval)
else case when (CS_COUNT = -1 AND LAG (CS_COUNT ) OVER (ORDER BY CASENUMBER, CLIENT_NUMBER, PROGRAMNAME, SUBPROGRAMNAME, COVERAGE_YEARMONTH) <> 0 ) then warehouse_dev.cs_count_seq.currval
else case when (CS_COUNT = 1) then warehouse_dev.cs_count_seq.nextval
else warehouse_dev.cs_count_seq.nextval
end
end
end CS_COUNT
from
(select CASENUMBER, PREV_CASENUMBER, CLIENT_NUMBER,
PREV_CLIENT_NUMBER, PROGRAMNAME, PREV_PROGRAMNAME,
SUBPROGRAMNAME, PREV_SUBPROGRAMNAME, COVERAGE_YEARMONTH,
CLIENT_COVERAGE, PREV_CLIENT_COVERAGE,
case when
(CASENUMBER = PREV_CASENUMBER AND CLIENT_NUMBER = PREV_CLIENT_NUMBER AND PROGRAMNAME = PREV_PROGRAMNAME AND SUBPROGRAMNAME = PREV_SUBPROGRAMNAME AND PREV_CLIENT_COVERAGE=0) then 0
else case when (CASENUMBER = PREV_CASENUMBER AND CLIENT_NUMBER = PREV_CLIENT_NUMBER AND PROGRAMNAME = PREV_PROGRAMNAME AND SUBPROGRAMNAME = PREV_SUBPROGRAMNAME AND PREV_CLIENT_COVERAGE=1) then -1 -- EXCEPT FOR WHEN PREV PREV_CLIENT_COVERAGE WAS ZERO THEN 1.
else case when ((CASENUMBER <> PREV_CASENUMBER OR CLIENT_NUMBER <> PREV_CLIENT_NUMBER OR PROGRAMNAME <> PREV_PROGRAMNAME OR SUBPROGRAMNAME <> PREV_SUBPROGRAMNAME) AND PREV_CLIENT_COVERAGE=1) then-1
else case when ((CASENUMBER <> PREV_CASENUMBER OR CLIENT_NUMBER <> PREV_CLIENT_NUMBER OR PROGRAMNAME <> PREV_PROGRAMNAME OR SUBPROGRAMNAME <> PREV_SUBPROGRAMNAME) AND PREV_CLIENT_COVERAGE=0) then 1
else 88
end
end
end
end CS_COUNT
from WAREHOUSE_DEV.STG_COVERAGE_SUBPROGRAM_FUN_CS
where CASENUMBER = '0042432' and client_number = '0000942256'
ORDER BY CASENUMBER, CLIENT_NUMBER, PROGRAMNAME, SUBPROGRAMNAME, COVERAGE_YEARMONTH, CLIENT_COVERAGE);
PRODUCES RESULTS:
0042432 0042419 0000942256 0001571135 Child Care Child Care Child Care Child Care 201008 1 0 1
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201009 1 1 2
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201010 1 1 3
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201011 1 1 4
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201012 1 1 5
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201101 1 1 6
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201102 1 1 7
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201103 0 1 8
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201104 1 0 0
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201105 1 1 10
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201106 1 1 11
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201107 1 1 12
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201108 1 1 13
0042432 0042432 0000942256 0000942256 Child Care Child Care Child Care Child Care 201109 1 1 14>
This approach is basically trying to apply a group by while forcing the group by to operate on the data in a specific order(not the group by order).
>
That is NOT a problem statement. That is you attempt at a solution.
You need to tell us what the problem is.
It is YOU that is trying to preach to us by telling us what we can, and cannot consider. No one is trying to preach to you.
Tell us what PROBLEM you are trying to solve; not what solution you want to use.
This is how preaching would read:
Read the FAQ for how to ask a question in the forums.
Use \ tags on the line before and after any code to preserver the formatting.
Don't post two sets of unformatted code and results.
When you do post code and results explain why those results are NOT correct and what results you are trying to produce.
ALWAYS provide your 4 digit Oracle version.
There: how'd I do? -
How handle currval if nextval fails
hi,
i have to insert the following rows in the table if
Insert into BOOK (BOOK_ID, CNT, ALT_CNT, ROW_INSERT_TMSTMP, ROW_LAST_UPDT_TMSTMP, BOOK_ID,VERSION)
SELECT SEQ_BOOK_ID.nextval, 50 , 500 ,sysdate,sysdate, '123456xyz' ,1 FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM BOOK B WHERE b.BOOK_ID = '123456xyz' );
Insert into BOOK_OWNER (BOOK_OWNER_ID, BOOK_ID, USER_ID,VERSION)
SELECT SEQ_BOOK_OWNER_ID.nextval, SEQ_BOOK_ID.currval, '30327def',1 FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM BOOK_OWNER AO WHERE AO.USER_ID= '30327def');
commit;
in above #1st statement checking if the value exists in book and inserting if the first insert fails the secound insert statement uses the "SEQ_BOOK_ID.currval" of the parent. if #1 insert fails how to handle the currval of the secound statement?
using oralce 10gHi,
[email protected] wrote:
also would like to know in anyof the condition the nextval will fail? i mean for some of the records insertion in getting the primery key violation.
like i am doing one time migration with huge data in to the database around 200 to 300 similar insert statements and some times i am getting the primery key voilation i suspect nextval is failing not able to figure it out.That sounds like some rows were entered with a primary key that was not generated by the sequence.
You should change the sequence so that it is generating numbers higher than anything already in the table.
First, find out what the highest id in the table is. For example:
SELECT MAX (book_id)
FROM book;Say this tells you that book_id=12345 is already in the table. Now find out how high the sequence is. For example:
SELECT seq_book_id.NEXTVAL
FROM dual;Say this returns 12000, which means you have to add 345 to the sequence.
You don't want to drop and re-create the sequence; that might make procedures invalid, or lose grants.
You could reference .NEXTVAL 345 times, or you could say:
ALTER SEQUENCE seq_book_id INCREMENT BY 345;
SELECT seq_book_id.NEXTVAL
FROM dual;
ALTER SEQUENCE seq_book_id INCREMENT BY 1;This assumes the sequence had originally been incrementing by 1.
The problem is liable to keep happening as long as the table is incremented without using the sequence.
You can add a trigger to guarantee that the sequence is used. -
In the following SQL*Plus session, why might the CURRVAL be different from h_id?
SQL> VARIABLE h_id NUMBER
SQL> INSERT INTO emp (empno, ename)
2 VALUES (id_sequence.NEXTVAL, 'SPACEK') RETURNING empno INTO :h_id
1 row created
SQL> print h_i
H_ID
1
SQL> select id_sequence.CURRVAL FROM dual;
CURRVAL
3
1 row selected.
nullHi,
If there is an insert trigger on the table to select from the sequence into the empno field then the sequence will be in the state shown.
What is the value in the table?
If there is a trigger on the table you could change it to only set the empno field when it is null.
Regards Michael -
If I want to get the currval of a sequence, will it be OK to do it by saying:
select A_seq.nextval-1 into :new.B from dual; ?
Cause I've read that in order to get currval, nextval need to be called at least once. Will this work?
ThanksAs SomeoneElse wrote you can not do it but I would like to add some explanation.
The question you need to ask yourself is what it is you are trying to do: I think you are unclear. CURRVAL gives you the current value in your session. If you have not called NEXTVAL then no value exists.
If you are looking for the last value used by someone else then you need to understand that Oracle is not single-user software. It is possible that as many as 300,000+ users are simultaneously connected and using the same sequence. What value would you want? And why?
Oracle's quite rational response is to provide a mechanism that answers the only logically consistent question. -
Use the currval whenever I need it
Hello,
I defined a sequence : MySequence_seq
I defined 2 Procedures :
1/ Initproc encloses a MySequence_seq.nextval
2/ GetProc encloses a MySequence_seq.currval
But when I call GetProc I get a "ORA-08002 :sequence SEQ1.CURRVAL is not yet defined in this session".
I thought one nextval was enough and then I could invoke a currval whenever I want.
The same issue with SQL*PLUS : if I connect call a nextval and then a currval it works. If I close then re-connect and call currval it doesn't work.
My need : having an absolute unique ID I can get whenever I want without performing a nextval (which will increments the ID) anytime I want the last value generated. Perhaps it's not possible with a sequence...so should I manage on my own (PL/SQL + a given field of my table storing this last value) ?
Any help greatly appreciated.
Jerome.The manual says From: http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96536/ch2123.htm#1300856
"Table: USER_SEQUENCES/ALL_SEQUENCES
LAST_NUMBER : Last sequence number written to disk. If a sequence uses caching, the number written to disk is the last number placed in the sequence cache. This number is likely to be greater than the last sequence number that was used."
This mean that the numbers X obtained by the proposed method, even if apparently persistent through sessions, are NOT a replacement for the number Y obtained from sequence.CURRVAL, and therefore cannot fulfill the function requested in the thread, because:
1. they are not unique: you can get same X for different Y
2. they are not stable: you can get different X for same Y (when X is read more than one time)
You can see this from the following test:
CREATE OR REPLACE PROCEDURE test_seq(p_iters NUMBER,
p_sleep_time NUMBER := 0) AS
v_seq1 NUMBER;
v_seq10 NUMBER;
v_seq2 NUMBER;
v_seq20 NUMBER;
v_seqnos NUMBER := 0;
v_diffs1 NUMBER := 0;
v_diffs2 NUMBER := 0;
BEGIN
FOR i IN 1 .. p_iters LOOP
IF MOD(i, 10) = 1 THEN
SELECT mysequence_seq.NEXTVAL
INTO v_seq1
FROM dual;
v_seqnos := v_seqnos + 1;
END IF;
SELECT mysequence_seq.CURRVAL,
last_number - 1
INTO v_seq1,
v_seq2
FROM user_sequences
WHERE sequence_name = 'MYSEQUENCE_SEQ';
IF v_seq1 <> v_seq10 AND v_seq2 = v_seq20 THEN
v_diffs1 := v_diffs1 + 1;
END IF;
IF v_seq1 = v_seq10 AND v_seq2 <> v_seq20 THEN
v_diffs2 := v_diffs2 + 1;
END IF;
v_seq10 := v_seq1;
v_seq20 := v_seq2;
dbms_lock.sleep(p_sleep_time);
END LOOP;
dbms_output.put_line('Sequence.CURRVAL calls: ' || p_iters);
dbms_output.put_line('Generated sequence numbers: ' || v_seqnos);
dbms_output.put_line('Not unique numbers: ' || v_diffs1);
dbms_output.put_line('Not stable numbers: ' || v_diffs2);
END;
Running from session #1:
SET SERVEROUT ON
EXEC test_seq(200, 0.1);
you get:
Sequence.CURRVAL calls: 200
Generated sequence numbers: 20
Not unique numbers: 17 <<******
Not stable numbers: 15 <<******
While running in parallel from session #2 of the same user:
SET SERVEROUT OFF
EXEC test_seq(10000);
EXEC test_seq(10000); -
Porting Sybase @@identity to seq.currval
I am currently porting a Sybase app to Oracle.
I have a fairly mature app to port. Four years of history, several big customers, NT services, native browsers on Mac and NT and WEB stuff too.
One of the Sybase @@identity features is that transactions that make use of it do not need to know 'which sequence' to request a currval from. @@identity is always the value of the last identity column incremented. I have currently written code which generates Oracle sequence triggers for all my tables that need them. They follow the Oracle Migration Workbench approach, however, and create a different sequence for each table. If I stick with this, I will have to hunt down every usage of @@identity and make manual changes that reference the correct sequence. I cannot just introduce a brain dead substitution involving seq.currval whenever @@identity passes through my single execSQL() function... Since some of my SQL is generated (not hard coded) this means I have to tinker a good deal of source code to get this right.
First, is there some Oracle facility that I am unaware of that acts like the Sybase @@identity global variable and returns the last sequence value irrespective of which sequence produced it?
Second, if I 'share' a single sequence among all of my tables (I don't care about identity gaps) are there performance or other problems I should be aware of?
Many thanks in advance.
Al
nullIt would be possible to get each Trigger that generates the seq.currval to also update a variable defined within a global Package i.e. a Package that has EXECUTE priviledges granted to PUBLIC; for example, the following Package :
SQL> connect system/manager
Connected.
SQL> grant connect, resource to globalUser identified by oracle;
Grant succeeded.
SQL> CREATE OR REPLACE PACKAGE globalUser.identityPkg AS
2 oracleIdentity NUMBER;
3 END identityPkg;
4 /
Package created.
SQL> grant execute on identityPkg to public;
Grant succeeded.
SQL> -- The following code demonstrates how this package variable can be altered by any user.
SQL> connect scott/tiger;
Connected.
SQL> begin
2 globalUser.identityPkg.oracleIdentity := 100;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> variable testNum number;
SQL> begin
2 select globalUser.identityPkg.oracleIdentity into :testNum from dual;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> print testNum;
TESTNUM
100
In this fashion, even if multiple sequences were implemented, the last value of the last sequence used would always be assigned to this package variable, thus emulating the Sybase @@IDENTITY feature. -
I am SQL Loading a table with a view to using the it as part of a nightly update process. Further updates to the table will happen via a stored proc. When loading the table I'm using the SQL Loader sequence function to allocate a primary key. After loading the table I would like to update the sequence being used by the stored procedure so that the CURRVAL of the sequence is the same as the maximum value that has been allocated to the values in the table.
Is there any way to do this other than to drop the sequence and recreate it, setting the min value to the maximum value allocated by the SQL Load step ?
nullYou have 2 options here:
I would use a before insert trigger to generate the sequence numbers as the records are loaded. You would not have a problem then.
If you have to do it the way you described you will need to either drop/recreate the sequence or alter the sequence so that the increment is set to the difference between the sqlloader number and the next val for the sequence number, then issue a select on the sequence then reset the increment to 1.
Hope this helps
Ron
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Ian Dukes ([email protected]):
I am SQL Loading a table with a view to using the it as part of a nightly update process. Further updates to the table will happen via a stored proc. When loading the table I'm using the SQL Loader sequence function to allocate a primary key. After loading the table I would like to update the sequence being used by the stored procedure so that the CURRVAL of the sequence is the same as the maximum value that has been allocated to the values in the table.
Is there any way to do this other than to drop the sequence and recreate it, setting the min value to the maximum value allocated by the SQL Load step ?
<HR></BLOCKQUOTE>
null
Maybe you are looking for
-
How do I delete an e-mail from address books
I have an e-mail address in my address book that is frozen. I am having a problem editing it or deleting it. What to do?
-
Hello, I've been trying to copy my iTunes library from my old computer (a very slow eMac G4/700 running OS X 10.3.9) to a new one. I've been following the directions as shown here: http://docs.info.apple.com/article.html?artnum=300173#next and the pr
-
Malfunction error when I connect
Recently received an iPod from a friend. Everytime I try to connect the iPod [it's an 8g nano] I get the error message 'One of the USB devices attached to this computer has malfunctioned , and Windows does not recognize it.' Not sure what to do. Any
-
Hi. I need help changing text in browser tab.
Greetings, I have downloaded a template and have uploaded it successfully. However, the text in browser tabs still say "Temple Free blah blah". I'd like it to say my website title. Could someone please walk me through this elementry task? Best, Chadr
-
What is the Future for MAEMO 5 Ahead ?
Hello Everyone, Greetings from India !! I know for the fact that this forum is mostly for people who have already bought the N900 and who may have some technical queries about their product. But with the N900 just been launched in this part of the wo