Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Friday, November 7, 2014

Advance a Oracle Sequence to match the corresponding column maximum

Problem Statement :

You have a Oracle sequence that is lagging behind a columns value. That column is supposed to have unique values and when it tries to derive the value using the sequence it fails. Hence you would like to have a quick and dirty method to advance the value of the sequence to match the column's value.

Solution :

        declare
          i           INTEGER;
          max_row_id  INTEGER;
        BEGIN
          select  MAX(<COLUMN_NAME>) INTO max_row_id
          FROM <TABLE_NAME>;
         
          DBMS_OUTPUT.PUT_LINE ('Max row id :' || max_row_id);
         
          select <SEQUENCE_NAME>.nextval INTO i from dual;
         
          DBMS_OUTPUT.PUT_LINE ('Starting with Sequence value :' || i);
         
          loop
            if i>=max_row_id then
              EXIT;
            else
              select
<SEQUENCE_NAME>.nextval INTO i from dual;
            end if;     
          end loop;
         
          DBMS_OUTPUT.PUT_LINE ('Ending with Sequence value :' || i);
        END;

Tuesday, October 29, 2013

Oracle Automatic Segment Advisor - Read Advisor findings

Oracle Automatic Segment Advisor runs during maintenance window and collects information regarding various facets of your database, namely SQL optimization, table segment fragmentation, I/O wait reduction methods, ...

You can use a combination of DBA_ADVISOR_FINDINGS, DBA_ADVISOR_OBJECTS AND DBA_ADVISOR_LOG to read those findings.

The underlying query gives you the findings related to tables.
SELECT TO_CHAR(log.execution_start, 'YYYY-MM-DD') AS "ADVISOR DATE",
  af.task_name,
  ao.owner,
  ao.attr2 segname,
  ao.attr3 partition,
  ao.type,
  af.message
FROM dba_advisor_findings af,
  dba_advisor_objects ao,
  dba_advisor_log log
WHERE ao.task_id        = af.task_id
AND ao.task_id          = log.task_id
AND ao.object_id        = af.object_id
AND ao.type             = 'TABLE'
AND log.execution_start > SYSDATE-1
ORDER BY log.execution_start DESC, ao.attr2;

Wednesday, October 23, 2013

Connect to Oracle RAC Database server

Oracle RAC database uses service-name instead of service-id. Hence, you would need a connection string formatted for using service-name.

Lets say your non-RAC server was hosted on "abc.xyz.com" @ port 1521 and your SID is oradb01, then your connection string would have been :
  jdbc:oracle:thin:@abc.xyz.com:1521:oradb01

On a oracle RAC database server with service-name as oradb01, your connection string would be :
 jdbc:oracle:thin:@//abc.xyz.com:1521/oradb01

Note : I assume you are using Oracle thin driver for Java.

Friday, September 27, 2013

Oracle Convert CSV to ROW

Allow Only Numeric values in the CSV : 
WITH parameter_csv AS (select :1 as val from dual)
SELECT  REGEXP_REPLACE(REGEXP_SUBSTR( val, '[^,]+', 1, LEVEL ),'[^0-9]','')  AS id
           FROM    parameter_csv
           CONNECT BY LEVEL <=
                LENGTH( REGEXP_REPLACE(val, '[^,]+', '' ) ) + 1


If you can afford it not to be parametrized (no ? marks in SQL) Needs Oracle 11g:
select * from xmltable('"this","is","my","csv"');
or 
select * from xmltable('1,2,3,4');