Why do I get error ORA-08002 when trying to get the currval of a sequence? - oracle

In TOAD I can execute this query:
select my_seq.currval from dual;
But when I try to execute it in my application I get this error:
java.sql.SQLException: ORA-08002: sequence MY_SEQ.CURRVAL is not yet defined in this session
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)

The answer is very simple: you have to read nextval at least once before you can use currval.

A workaround for this is to execute this sql:
select LAST_NUMBER-1 currval from all_sequences t where t.sequence_name = 'put here your sequence_name';

Related

java.sql.SQLException: ORA-01000: maximum open cursors exceeded. While generating jasper report

While generating report through jasper we are getting exception error as ...
Error filling print... net.sf.jasperreports.engine.JRException: Error executing SQL statement for : risk
net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : risk
at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:729)
Caused by: java.sql.SQLException: ORA-01000: maximum open cursors exceeded
How to resolve this?
Сonnect to the database and check the open_cursors limits:
select value from v$parameter where name='open_cursors'
So we list the top 20 sessions which are currently opening most cursors:
select * from ( select ss.value, sn.name, ss.sid from v$sesstat ss, v$statname sn where ss.statistic# = sn.statistic# and sn.name like '%opened cursors current%' order by value desc) where rownum < 21;
The solution is to increase the no. of the open_cursors parameter as :
alter system set open_cursors=400 scope=both

Getting Unknown Command error on IF-THEN-ELSE

I have the following query that I am using in Oracle 11g
IF EXISTS (SELECT * FROM EMPLOYEE_MASTER WHERE EMPID='ABCD32643')
THEN
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1' where EMPID='ABCD32643' ;
ELSE
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE) values('A62352','JOHN DOE','1') ;
END IF;
On running the statement I get the following output:
Error starting at line : 4 in command -
ELSE
Error report -
Unknown Command
1 row inserted.
Error starting at line : 6 in command -
END IF
Error report -
Unknown Command
The values get inserted with error when I run it directly. But when I try to execute this query through my application I get an oracle exception because of the error generated :
ORA-00900: invalid SQL statement
And hence the values are not inserted.
I am relatively new to Oracle. Please advise on what's wrong with the above query so that I could run this query error free.
If MERGE doesn't work for you, try the following:
begin
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1'
where EMPID='ABCD32643' ;
if SQL%ROWCOUNT=0 then
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE)
values('A62352','JOHN DOE','1') ;
end if;
end;
Here you you the update on spec, then check whether or not you found a matching row, and insert in case you didn't.
"what's wrong with the above query "
What's wrong with the query is that it is not a query (SQL). It should be a program snippet (PL/SQL) but it isn't written as PL/SQL block, framed by BEGIN and END; keywords.
But turning it into an anonymous PL/SQL block won't help. Oracle PL/SQL does not support IF EXISTS (select ... syntax.
Fortunately Oracle SQL does support MERGE statement which does the same thing as your code, with less typing.
merge into EMPLOYEE_MASTER em
using ( select 'A62352' as empid,
'JOHN DOE' as empname,
'1' as emptype
from dual ) q
on (q.empid = em.empid)
when not matched then
insert (EMPID,EMPNAME,EMPTYPE)
values (q.empid, q.empname, q.emptype)
when matched then
update
set em.empname = q.empname, em.emptype = q.emptype
/
Except that you're trying to update empid as well. That's not supported in MERGE. Why would you want to change the primary key?
"Does this query need me to add values to all columns in the table? "
The INSERT can have all the columns in the table. The UPDATE cannot change the columns used in the ON clause (usually the primary key) because that's a limitation of the way MERGE works. I think it's the same key preservation mechanism we see when updating views. Find out more.

How can i "Clean" output from sqlplus query?

i have got simple query and i need just one value from it = VALID
The query is:
select 'VALUE('||status||')' as value from user_indexes where index_name = '&1';
But i hve got in out:
C:\Program Files\zabbix\bin\win64\oracle>sqlplus -s #"C:\Program Files\zabbix\bi
n\win64\oracle\conn2.sql" OLAPTABLEVELSID
old 1: select status from user_indexes where index_name = '&1'
new 1: select status from user_indexes where index_name = 'OLAPTABLEVELSID'
VALID
What are this OLD and NEW strings? How can i dismiss it?
Thank you.
SET VERIFY OFF should help you. Please add such line in your script before query.
in sqplus you have substition variables. they are referenced by & or &&
when you run your script and pass it 'OLAPTABLEVELSID' - the query takes the '&1' and replaces it with ''OLAPTABLEVELSID'
SQL*Plus is telling you that in the output
SQL> set verify off
SQL> select '&1' from dual;
Enter value for 1: hello stackoverflow
'HELLOSTACKOVERFLOW
-------------------
hello stackoverflow
SQL>

How to specify "no update" in an oracle select statement?

In a simple oracle statement, I have to fetch rows with no locks on it. I think this can be done through select with no update statement. Correct me if I'm wrong ? If not can anyone pls let me know the format for it ?
Yes, if you don't specify for update in a select then you haven't locked any rows.
If your code is only doing a select (and not a "select for update"), no locks would be acquired on the record. If you have DML (Update,select,delete) on the rows or a "Select for Update", then you have the possibility of locking.
**Old Answer
You are probably looking for the "FOR UPDATE NOWAIT" clause in Oracle, but it's behavior is not as you described.
It will try to acquire a lock on the row/rows in the select and return an error if the lock is already acquired by someone else.
Session1: (No Commit yet.. Row Locked by this statement)
SQL> update scott_emp
2 set sal = sal + 100
3 where empno = 7839;
1 row updated.
Session2 :
SQL> select * from scott_emp
2 for update nowait;
select * from scott_emp
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified

How to set minvalue of sequence to result of a select query?

I am trying to create a sequence in oracle sql, using the "minvalue" as the result of a select query.
The select query I'm trying to run is:
SELECT
MAX(customer_id) + 1
FROM
customer
I know it's easy with an anonymous pl/sql, but I'd like to find a way which doesn't use pl/sql. Some ideas I've had include the COLUMN command to set a substitution variable, but I'm a little lost as to how to do that.
Thanks in advance!
Like this:
column startval new_value v_startval
SELECT
MAX(customer_id) + 1
FROM
customer;
create sequence customer_seq start with &v_startval.;

Resources