Monday, March 18, 2019

WHERE CURRENT OF CLAUSE in Oracle

SQL> create table table_w(name varchar2(100),sal number);

Table created.

SQL> insert into table_w values('A',5000);

1 row created.

SQL> insert into table_w values('B',4000);

1 row created.

SQL> insert into table_w values('A',3000);

1 row created.

SQL> insert into table_w values('C',3000);

1 row created.

SQL> insert into table_w values('B',2000);

1 row created.

SQL> COMMIT;

Commit complete.


SQL> CREATE TABLE TABLE_W_BKP AS SELECT * FROM TABLE_W;

Table created.

                      
SET SERVEROUTPUT ON;
DECLARE
CURSOR C1 IS SELECT * FROM TABLE_W;
BEGIN
FOR I IN C1
LOOP
UPDATE TABLE_W SET SAL=SAL+1000 WHERE NAME=I.NAME;
COMMIT;
END LOOP;
END;
/                          

BEFORE :

SQL> SELECT * FROM TABLE_W;

NAME
                            SAL
--------------------------------------------------------------------
A
                           5000
B
                           4000
A
                           3000
C
                           3000
B
                           2000
                          
AFTER :

SQL> SELECT * FROM TABLE_W;

NAME                                      SAL
------------------------------------- ----------
A                                        7000
B                                        6000
A                                        5000
C                                        4000
B                                        4000

SET SERVEROUTPUT ON;
DECLARE
CURSOR C1 IS SELECT * FROM TABLE_W_BKP FOR UPDATE OF SAL NOWAIT;
BEGIN
FOR I IN C1
LOOP
UPDATE TABLE_W_BKP SET SAL=SAL+1000 WHERE CURRENT OF C1;
--OORA-01002: fetch out of sequence.-->If the cursor has been opened with the FOR UPDATE clause, fetching after a COMMIT has been issued --will return the error. Do not issue a COMMIT inside a fetch loop for a cursor that has been opened FOR UPDATE.
--COMMIT;
END LOOP;
COMMIT;
END;
/   

BEFORE :

SQL>  SELECT * FROM TABLE_W_BKP;    

NAME                                      SAL
------------------------------------- ----------
A                                        6000
B                                        4000
A                                        3000
C                                        3000
B                                        2000

AFTER :

SQL>  SELECT * FROM TABLE_W_BKP;

NAME                                      SAL
------------------------------------- ----------
A                                        7000
B                                        5000
A                                        4000
C                                        4000
B                                        3000

Installing Digital Certificates in Oracle Wallet using Keytool

Microsoft Windows [Version 10.0.17134.590]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>cd C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>dir /s
 Volume in drive C is System
 Volume Serial Number is E28D-8D5E

 Directory of C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51

03/13/2019  11:28 AM    <DIR>          .
03/13/2019  11:28 AM    <DIR>          ..
03/09/2019  08:58 AM             1,360 DigiCertGlobalRootCA.crt
03/09/2019  08:58 AM             1,662 GeoTrustRSACA2018.crt
07/05/2018  02:32 PM             6,492 wsctt-wsvcoraclectt.p12
               3 File(s)          9,514 bytes

     Total Files Listed:
               3 File(s)          9,514 bytes
               2 Dir(s)  84,026,478,592 bytes free

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>keytool -v -importkeystore -srckeystore wsctt-wsvcoraclectt.p12 -srcstoretype PKCS12 -destkeystore wsctt.jks -deststoretype JKS
Importing keystore wsctt-wsvcoraclectt.p12 to wsctt.jks...
Enter destination keystore password:
Re-enter new password:
Enter source keystore password:
Entry for alias wsctt-wsvcoraclectt successfully imported.
Import command completed:  1 entries successfully imported, 0 entries failed or cancelled
[Storing wsctt.jks]

Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore wsctt.jks -destkeystore wsctt.jks -deststoretype pkcs12".

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>keytool -import -alias Root -keystore wsctt.jks -trustcacerts -file DigiCertGlobalRootCA.crt
Enter keystore password:
Certificate already exists in system-wide CA keystore under alias <digicertglobalrootca [jdk]>
Do you still want to add it to your own keystore? [no]:  no
Certificate was not added to keystore

Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore wsctt.jks -destkeystore wsctt.jks -deststoretype pkcs12".

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>keytool -import -alias Intermediate -keystore wsctt.jks -trustcacerts -file GeoTrustRSACA2018.crt
Enter keystore password:
Certificate was added to keystore

Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore wsctt.jks -destkeystore wsctt.jks -deststoretype pkcs12".

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>orapki wallet create -wallet . -pwd aaaaa
Oracle PKI Tool : Version 12.1.0.1
Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>orapki wallet jks_to_pkcs12 -wallet ./ -pwd "aaaaa" -keystore ./wsctt.jks -jkspwd "aaaaa"
Oracle PKI Tool : Version 12.1.0.1
Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>orapki wallet display -wallet . -pwd aaaaa
Oracle PKI Tool : Version 12.1.0.1
Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.

Requested Certificates:
User Certificates:
Subject:        EmailAddress=pvats@pearson.com,CN=wsctt-wsvcoraclectt,OU=VUE,O=NCS Pearson,L=Bloomington,ST=Minnesota,C=US
Trusted Certificates:
Subject:        OU=Class 2 Public Primary Certification Authority,O=VeriSign\, Inc.,C=US
Subject:        CN=GeoTrust RSA CA 2018,OU=www.digicert.com,O=DigiCert Inc,C=US
Subject:        CN=GTE CyberTrust Global Root,OU=GTE CyberTrust Solutions\, Inc.,O=GTE Corporation,C=US
Subject:        CN=Pearson VUE Root Certificate Authority
Subject:        CN=Pearson VUE CTT Certificate Authority,DC=pearsonvue,DC=com
Subject:        OU=Class 3 Public Primary Certification Authority,O=VeriSign\, Inc.,C=US
Subject:        OU=Class 1 Public Primary Certification Authority,O=VeriSign\, Inc.,C=US
C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>dir /s
 Volume in drive C is System
 Volume Serial Number is E28D-8D5E

 Directory of C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51

03/13/2019  11:30 AM    <DIR>          .
03/13/2019  11:30 AM    <DIR>          ..
03/09/2019  08:58 AM             1,360 DigiCertGlobalRootCA.crt
03/13/2019  11:30 AM            11,320 ewallet.p12
03/13/2019  11:30 AM                 0 ewallet.p12.lck
03/09/2019  08:58 AM             1,662 GeoTrustRSACA2018.crt
07/05/2018  02:32 PM             6,492 wsctt-wsvcoraclectt.p12
03/13/2019  11:30 AM             7,222 wsctt.jks
               6 File(s)         28,056 bytes

     Total Files Listed:
               6 File(s)         28,056 bytes
               2 Dir(s)  84,024,537,088 bytes free

C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51>sqlplus sys as sysdba

SQL*Plus: Release 12.1.0.1.0 Production on Wed Mar 13 11:34:22 2019

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Enter password:

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL>   SELECT UTL_HTTP.request (
  2            url               => 'https://wsctt.abc.com/cxfws2/services/Ping',
  3            proxy             =>  'http://www-proxy.com:80',
  4            wallet_path       => 'file:C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_51',
  5            wallet_password   => 'aaaaa')
  6    FROM DUAL;

UTL_HTTP.REQUEST(URL=>'HTTPS://WSCTT.abc.COM/CXFWS2/SERVICES/PING',PROXY=
--------------------------------------------------------------------------------
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soapen
v="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><soap:Code>
<soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:la
ng="en">Fault occurred while processing.</soap:Text></soap:Reason></soap:Fault><
/soap:Body></soap:Envelope>


SQL>

Generate Trace file in Oracle 12c (Windows Platform)

C:\WINDOWS\system32>sqlplus sys as sysdba

SQL*Plus: Release 12.1.0.1.0 Production on Mon Mar 18 17:01:38 2019

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Enter password:

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> alter session set tracefile_identifier='SRDC_10937';

Session altered.

SQL> alter session set events '10937 trace name context forever, level 4';

Session altered.

SQL> SELECT UTL_HTTP.request (
  2            url               => 'https://ws.xxx.com/cxfws2/services/Ping',
  3            proxy             => 'http://www-proxy.com:80',
  4            wallet_path       => 'file:C:\app\rajam\product\12.1.0\dbhome_1\owm\wallet_53',
  5            wallet_password   => 'xxxxxxxxxxx')
  6    FROM DUAL;
SELECT UTL_HTTP.request (
       *
ERROR at line 1:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1720
ORA-29263: HTTP protocol error
ORA-06512: at line 1


SQL> alter system set events '10937 trace name context off';

System altered.

SQL>

Below Trace files will be generated in the path C:\app\rajam\diag\rdbms\orcl\orcl\trace
orcl_ora_18972_SRDC_10937.trc
orcl_ora_18972_SRDC_10937.trm

Installing .p12/.pfx certificate in Oracle Database 12c in windows Platform

This summary is not available. Please click here to view the post.

OPatch lsinventory Command to Verify the Patches Applied to an Oracle Home

Microsoft Windows [Version 10.0.17134.590]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\app\rajam\product\12.1.0\dbhome_1\OPatch>opatch lsinventory -detail
Oracle Interim Patch Installer version 12.1.0.1.0
Copyright (c) 2012, Oracle Corporation.  All rights reserved.


Oracle Home       : C:\app\rajam\product\121~1.0\dbhome_1
Central Inventory : C:\Program Files\Oracle\Inventory
   from           : n/a
OPatch version    : 12.1.0.1.0
OUI version       : 12.1.0.1.0
Log file location : C:\app\rajam\product\121~1.0\dbhome_1\cfgtoollogs\opatch\opatch2019-03-18_15-30-39PM_1.log

Lsinventory Output file location : C:\app\rajam\product\121~1.0\dbhome_1\cfgtoollogs\opatch\lsinv\lsinventory2019-03-18_15-30-39PM.txt

--------------------------------------------------------------------------------
Installed Top-level Products (1):

Oracle Database 12c                                                  12.1.0.1.0
There are 1 products installed in this Oracle Home.


Installed Products (136):

Assistant Common Files                                               12.1.0.1.0
Buildtools Common Files                                              12.1.0.1.0
Cluster Verification Utility Common Files                            12.1.0.1.0
Database Configuration and Upgrade Assistants                        12.1.0.1.0
Database Migration Assistant for Unicode                             12.1.0.1.0
Database SQL Scripts                                                 12.1.0.1.0
Database Workspace Manager                                           12.1.0.1.0
Deinstallation Tool                                                  12.1.0.1.0
Enterprise Edition Options                                           12.1.0.1.0
Expat libraries                                                       2.0.1.0.2
Generic Connectivity Common Files                                    12.1.0.1.0
HAS Common Files                                                     12.1.0.1.0
HAS Files for DB                                                     12.1.0.1.0
Installation Common Files                                            12.1.0.1.0
Installation Plugin Files                                            12.1.0.1.0
Installer SDK Component                                              12.1.0.1.0
JAccelerator (COMPANION)                                             12.1.0.1.0
LDAP Required Support Files                                          12.1.0.1.0
OLAP SQL Scripts                                                     12.1.0.1.0
Oracle Administration Assistant for Windows                          12.1.0.1.0
Oracle Advanced Security                                             12.1.0.1.0
Oracle Application Express                                           12.1.0.1.0
Oracle Bali Share                                                    11.1.1.6.0
Oracle Call Interface (OCI)                                          12.1.0.1.0
Oracle Clusterware RDBMS Files                                       12.1.0.1.0
Oracle Configuration Manager                                         10.3.7.0.3
Oracle Configuration Manager Client                                  10.3.2.1.0
Oracle Configuration Manager Deconfiguration                         10.3.1.0.0
Oracle Containers for Java                                           12.1.0.1.0
Oracle Core Required Support Files                                   12.1.0.1.0
Oracle Core Required Support Files for Core DB                       12.1.0.1.0
Oracle Core XML Development Kit                                      12.1.0.1.0
Oracle Data Mining RDBMS Files                                       12.1.0.1.0
Oracle Data Provider for .NET                                        12.1.0.1.0
Oracle Database 12c                                                  12.1.0.1.0
Oracle Database 12c                                                  12.1.0.1.0
Oracle Database 12c Multimedia Files                                 12.1.0.1.0
Oracle Database Deconfiguration                                      12.1.0.1.0
Oracle Database Extensions for .NET                                  12.1.0.1.0
Oracle Database Gateway for ODBC                                     12.1.0.1.0
Oracle Database User Interface                                       11.0.0.0.0
Oracle Database Utilities                                            12.1.0.1.0
Oracle Database Vault option                                         12.1.0.1.0
Oracle DBCA Deconfiguration                                          12.1.0.1.0
Oracle Extended Windowing Toolkit                                    11.1.1.6.0
Oracle Globalization Support                                         12.1.0.1.0
Oracle Globalization Support                                         12.1.0.1.0
Oracle Globalization Support For Core                                12.1.0.1.0
Oracle Help for Java                                                 11.1.1.6.0
Oracle Help Share Library                                            11.1.1.6.0
Oracle Ice Browser                                                   11.1.1.6.0
Oracle Internet Directory Client                                     12.1.0.1.0
Oracle Java Client                                                   12.1.0.1.0
Oracle Java Layout Engine                                            11.0.0.0.0
Oracle JDBC Server Support Package                                   12.1.0.1.0
Oracle JDBC/OCI Instant Client                                       12.1.0.1.0
Oracle JDBC/THIN Interfaces                                          12.1.0.1.0
Oracle JFC Extended Windowing Toolkit                                11.1.1.6.0
Oracle JVM                                                           12.1.0.1.0
Oracle JVM For Core                                                  12.1.0.1.0
Oracle Label Security                                                12.1.0.1.0
Oracle LDAP administration                                           12.1.0.1.0
Oracle Locale Builder                                                12.1.0.1.0
Oracle Message Gateway Common Files                                  12.1.0.1.0
Oracle Multimedia                                                    12.1.0.1.0
Oracle Multimedia Client Option                                      12.1.0.1.0
Oracle Multimedia Java Advanced Imaging                              12.1.0.1.0
Oracle Multimedia Locator                                            12.1.0.1.0
Oracle Multimedia Locator Java Required Support Files                12.1.0.1.0
Oracle Multimedia Locator RDBMS Files                                12.1.0.1.0
Oracle Net                                                           12.1.0.1.0
Oracle Net Java Required Support Files                               12.1.0.1.0
Oracle Net Listener                                                  12.1.0.1.0
Oracle Net Required Support Files                                    12.1.0.1.0
Oracle Net Services                                                  12.1.0.1.0
Oracle Netca Client                                                  12.1.0.1.0
Oracle Notification Service                                          12.1.0.1.0
Oracle Notification Service (eONS)                                   12.1.0.1.0
Oracle Notification Service for Instant Client                       12.1.0.1.0
Oracle ODBC Driver                                                   12.1.0.1.0
Oracle ODBC Driverfor Instant Client                                 12.1.0.1.0
Oracle OLAP                                                          12.1.0.1.0
Oracle OLAP API                                                      12.1.0.1.0
Oracle OLAP RDBMS Files                                              12.1.0.1.0
Oracle One-Off Patch Installer                                       12.1.0.1.0
Oracle Partitioning                                                  12.1.0.1.0
Oracle Programmer                                                    12.1.0.1.0
Oracle Provider for OLE DB                                           12.1.0.1.0
Oracle Providers for ASP.NET                                         12.1.0.1.0
Oracle Quality of Service Management (Client)                        12.1.0.1.0
Oracle R Enterprise Server Files                                     12.1.0.1.0
Oracle RAC Deconfiguration                                           12.1.0.1.0
Oracle RAC Required Support Files-HAS                                12.1.0.1.0
Oracle Real Application Testing                                      12.1.0.1.0
Oracle Recovery Manager                                              12.1.0.1.0
Oracle Remote Configuration Agent                                    12.1.0.1.0
Oracle Security Developer Tools                                      12.1.0.1.0
Oracle Services For Microsoft Transaction Server                     12.1.0.1.0
Oracle Spatial and Graph                                             12.1.0.1.0
Oracle SQL Developer                                                 12.1.0.1.0
Oracle Starter Database                                              12.1.0.1.0
Oracle Text                                                          12.1.0.1.0
Oracle Text ATG Language Support Files                               12.1.0.1.0
Oracle Text for Core                                                 12.1.0.1.0
Oracle Universal Connection Pool                                     12.1.0.1.0
Oracle Universal Installer                                           12.1.0.1.0
Oracle USM Deconfiguration                                           12.1.0.1.0
Oracle Wallet Manager                                                12.1.0.1.0
Oracle Windows Interfaces                                            12.1.0.1.0
Oracle XML Development Kit                                           12.1.0.1.0
Oracle XML Query                                                     12.1.0.1.0
oracle.swd.oui.core.min                                              12.1.0.1.0
Parser Generator Required Support Files                              12.1.0.1.0
Perl Interpreter                                                     5.14.1.0.0
Perl Modules                                                         5.14.1.0.0
PL/SQL                                                               12.1.0.1.0
PL/SQL Embedded Gateway                                              12.1.0.1.0
Platform Required Support Files                                      12.1.0.1.0
Precompiler Common Files                                             12.1.0.1.0
Precompiler Common Files for Core                                    12.1.0.1.0
Precompiler Required Support Files                                   12.1.0.1.0
Precompilers                                                         12.1.0.1.0
RDBMS Required Support Files                                         12.1.0.1.0
RDBMS Required Support Files for Instant Client                      12.1.0.1.0
RDBMS Required Support Files Runtime                                 12.1.0.1.0
Required Support Files                                               12.1.0.1.0
Sample Schema Data                                                   12.1.0.1.0
Secure Socket Layer                                                  12.1.0.1.0
SQL*Plus                                                             12.1.0.1.0
SQL*Plus Files for Instant Client                                    12.1.0.1.0
SQLJ Runtime                                                         12.1.0.1.0
SSL Required Support Files for InstantClient                         12.1.0.1.0
Sun JDK                                                              1.6.0.37.0
XDK Required Support Files                                           12.1.0.1.0
XML Parser for Java                                                  12.1.0.1.0
XML Parser for Oracle JVM                                            12.1.0.1.0
There are 136 products installed in this Oracle Home.


There are no Interim patches installed in this Oracle Home.


--------------------------------------------------------------------------------

OPatch succeeded.

C:\app\rajam\product\12.1.0\dbhome_1\OPatch>

Inserting and retrieving ampersand(&) in Oracle

SQL> create table amp_test (no number,value varchar2(100));

Table created.

SQL> insert into amp_test values (1,'hyd');

1 row created.

SQL> insert into amp_test values (2,'at&t');
Enter value for t:
old   1: insert into amp_test values (2,'at&t')
new   1: insert into amp_test values (2,'at')

1 row created.

SQL> commit;

Commit complete.

SQL> select * from amp_test;

        NO VALUE
---------- ----------------------------------------------------------------------------------------------------
         1 hyd
         2 at

SQL>  insert into amp_test values (3,'at"&"t');

1 row created.

SQL> select * from amp_test;

        NO VALUE
---------------------------------------
         1 hyd
         2 at
         3 at"&"t

SQL> insert into amp_test values (4,'at'||'&'||'t');

1 row created.

SQL> select * from amp_test;

        NO VALUE
-----------------------------------------------------
         1 hyd
         2 at
         3 at"&"t
         4 at&t
SQL> SELECT NO, REPLACE (VALUE, '&', CHR (ASCII ('&')) || 'amp;') as value FROM amp_test;

   NO VALUE1

   2 at&amp;t
  
   PLSQL:
---------------------------------
BEGIN
  IF v_ats = 'AT'||CHR (ASCII ('&'))|| 'T'  THEN
   logic...
 END IF; 
END;
/

Sequence in Oracle

DROP SEQUENCE SCOTT.EMP_SEQ;

CREATE SEQUENCE SCOTT.EMP_SEQ
  START WITH 1
  MAXVALUE 99999999999999999999999999
  MINVALUE 1
  NOCYCLE
  NOCACHE
  NOORDER;


-- To know the sequence associated with 'EMP'

SELECT x.sequence_owner, x.SEQUENCE_NAME, B.TABLE_NAME
  FROM all_sequences x, all_tables B
 WHERE x.sequence_owner = B.owner AND B.TABLE_NAME = 'EMP';

--Example-1

SQL> CREATE TABLE my_numbers (my_number NUMBER NOT NULL PRIMARY KEY);

Table created.

SQL>
SQL> INSERT INTO my_numbers
  2     (SELECT ROWNUM FROM user_objects);

93 rows created.

SQL>
SQL> SELECT MAX (my_number) FROM my_numbers;

MAX(MY_NUMBER)
--------------
            93

SQL> CREATE SEQUENCE my_number_sn START WITH 93;

Sequence created.

SQL>
SQL> select my_number_sn.CURRVAL from dual;
select my_number_sn.CURRVAL from dual
       *
ERROR at line 1:
ORA-08002: sequence MY_NUMBER_SN.CURRVAL is not yet defined in this session


SQL> INSERT INTO my_numbers (my_number)
  2       VALUES (my_number_sn.NEXTVAL);
INSERT INTO my_numbers (my_number)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C006831) violated


SQL> DROP SEQUENCE my_number_sn;

Sequence dropped.

SQL>
SQL> CREATE SEQUENCE my_number_sn START WITH 94;

Sequence created.

SQL>
SQL> INSERT INTO my_numbers (my_number)
  2       VALUES (my_number_sn.NEXTVAL);

1 row created.


--Creating sequence on based on maxvalue of a table id

DECLARE
   l_new_seq   INTEGER;
BEGIN
   SELECT MAX (id) + 1 INTO l_new_seq FROM CERT_APPL_USERS;

   EXECUTE IMMEDIATE
         'Create sequence CERT_APPL_USERS_SEQ
                       start with '
      || l_new_seq
      || ' INCREMENT BY 1 MINVALUE 1
                                    MAXVALUE 99999999
                                    NOCYCLE
                                    CACHE 20
                                    NOORDER';
END;
/


-- How to Reset a Sequence in Oracle

create sequence reset_seq start with 1 increment by 1 minvalue 1 maxvalue 999 cache 20 nocycle;

wrong: alter sequence reset_seq restart with 0;

create or replace
procedure reset_sequence(p_seq in varchar2)
is
    l_value number;
begin
-- Select the next value of the sequence

    execute immediate
    'select ' || p_seq ||
    '.nextval from dual' INTO l_value;
   
    dbms_output.put_line('l_value1='||l_value);

-- Set a negative increment for the sequence,
-- with value = the current value of the sequence

    execute immediate
    'alter sequence ' || p_seq ||
    ' increment by -' || l_value || ' minvalue 0';
   
    dbms_output.put_line('l_value2='||l_value);

-- Select once from the sequence, to
-- take its current value back to 0

    execute immediate
    'select ' || p_seq ||
    '.nextval from dual' INTO l_value;
   
    dbms_output.put_line('l_value3='||l_value);

-- Set the increment back to 1

    execute immediate
    'alter sequence ' || p_seq ||
    ' increment by 1 minvalue 0';
   
    dbms_output.put_line('l_value4='||l_value);
end;
/

 select reset_seq.currval from dual;

 select reset_seq.nextval from dual;

begin
reset_sequence('reset_seq');
end;
/

--------------- OR -----------------

SQL> /

   NEXTVAL
----------
         5

SQL> alter sequence reset_seq increment by -5 minvalue 0;

Sequence altered.

SQL>  select reset_seq.nextval from dual;

   NEXTVAL
----------
         0

SQL> alter sequence reset_seq increment by 1 minvalue 0;

Sequence altered.

Why go through this rigmarole? Why not simply drop and recreate the sequence?

You could drop and recreate the sequence, but the disadvantage is this would invalidate all dependent objects (triggers/stored procedures etc.). Far better to alter and reset the sequence.


Caution!

Reset your sequence when others are not using it simultaneously, else they (or you) may get this error:

ORA-08004: sequence SEQ.NEXTVAL goes below MINVALUE and cannot be instantiated
 Link: http://www.oratable.com/reset-sequence-in-oracle/

Delete Row Button to all records of Interactive Report in Oracle Apex

 1. add 'Delete' Del column to Report Query 2. Set the Following Properties for the DEL Column Type: Link Heading: Delete Targ...