Tuesday, January 23, 2018

Essential Oracle Queries for Database Professionals

Date / Time-related queries :
1. Get the first day of the month
     SELECT TRUNC (SYSDATE, 'MONTH') "First day of current month"
     FROM DUAL;

2. Get the last day of the month
      SELECT TRUNC (LAST_DAY (SYSDATE)) "Last day of current month"
      FROM DUAL;

3. Get the first day of the Year
     SELECT TRUNC (SYSDATE, 'YEAR') "Year First Day"
     FROM DUAL;

4. Get the last day of the year
     SELECT ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), 12) - 1 "Year Last Day"
     FROM DUAL;

5. Get the number of days in current month
     SELECT CAST (TO_CHAR (LAST_DAY (SYSDATE), 'dd') AS INT) number_of_days
     FROM DUAL;

6. Get the number of days left in current month
     SELECT SYSDATE,
       LAST_DAY (SYSDATE) "Last",
       LAST_DAY (SYSDATE) - SYSDATE "Days left"
      FROM DUAL;

7. Get the number of seconds passed since today (since 00:00 hr)
      SELECT (SYSDATE - TRUNC (SYSDATE)) * 24 * 60 * 60 num_of_sec_since_morning
      FROM DUAL;

8. Get the number of seconds left today (till 23:59:59 hr)
      SELECT (TRUNC (SYSDATE+1) - SYSDATE) * 24 * 60 * 60 num_of_sec_left
      FROM DUAL;
   
Database administration queries
1. Database version information
     SELECT * FROM v$version;

2. Database default information
     SELECT username,
       profile,
       default_tablespace,
       temporary_tablespace
     FROM dba_users;

3. Get the Oracle version
    SELECT VALUE
    FROM v$system_parameter
    WHERE name = 'compatible';

4. Find the Actual size of a Database
    SELECT SUM (bytes) / 1024 / 1024 / 1024 AS GB
    FROM dba_data_files;

5. Find the size of the SCHEMA/USER
    SELECT SUM (bytes / 1024 / 1024) "size"
    FROM dba_segments
    WHERE owner = '&owner';

Performance-related queries:
1. CPU usage of the USER
     SELECT ss.username, se.SID, VALUE / 100 cpu_usage_seconds
     FROM v$session ss, v$sesstat se, v$statname sn
     WHERE     se.STATISTIC# = sn.STATISTIC#
     AND NAME LIKE '%CPU used by this session%'
     AND se.SID = ss.SID
     AND ss.status = 'ACTIVE'
     AND ss.username IS NOT NULL
    ORDER BY VALUE DESC;

2. Get the current session id, process id, client process id
      SELECT b.sid,
       b.serial#,
       a.spid processid,
       b.process clientpid
     FROM v$process a, v$session b
     WHERE a.addr = b.paddr AND b.audsid = USERENV ('sessionid');
     

Monday, January 22, 2018

Oracle SQL Queries Every Developer Must Know

1. Query to Finding Columns and Tables :

     SELECT  table_name, column_name
     FROM   all_tab_columns
     WHERE column_name like 'PO_HEADER%’;

    select * from all_objects
   where object_name like '%PO_HEADERS_ALL%'
    and object_type = 'TABLE';

2. Query to Find Triggers on a Table:

     SELECT trigger_name
    FROM   all_triggers
    WHERE table_name = ‘<TABLE_NAME>”;

3. GL Batches Query:

   SELECT jb.NAME, gh.period_name,
   gh.je_source, gh.je_category,
   gh.currency_code, gc.concatenated_segments,
  gl.entered_cr,gl.entered_dr,
  gl.accounted_cr, gl.accounted_dr
  FROM gl_je_batches jb,
       gl_je_headers gh,
       gl_je_lines gl,
       gl_code_combinations_kfv gc
 WHERE jb.je_batch_id = gh.je_batch_id
   AND gh.je_header_id = gl.je_header_id
   AND gl.code_combination_id = gc.code_combination_id

4. Query to get Database Links:
      SELECT owner            "Owner",db_link          "Link Name",
      username         "Username",created          "Created",
     host             xonly_host
    FROM   all_db_links
    ORDER BY 1,2;

5. Query to get DBA Directories:
       SELECT owner            "Owner"
      ,directory_name   "Directory Name"
     , directory_path   "Directory Path"
    FROM   all_directories
    ORDER BY 1,2;

Join Order Management & Quoting Tables in Oracle | SQL Query

SELECT oha.header_id
             , oha.order_number
             , ola.line_Id
            , ola.ship_from_org_id organization_id
             , ola.inventory_item_id
            , ola.ordered_quantity
           , ola.order_quantity_uom
          , ola.line_type_id  xx_line_type_id
           , aql.order_line_type_id
         , ola.request_date
         , ola.creation_date
         , ola.ship_to_org_id
        , ola.sold_to_org_id
          ,ola.subinventory

    FROM   aso_quote_headers_all aqh
         , aso_quote_lines_all aql
         , aso_shipments shp
         , oe_order_headers_all oha
         , ont.oe_order_lines_all ola
    WHERE  aqh.quote_header_Id = '&quote_header_id'
    AND    aql.quote_header_id = aqh.quote_header_id
    AND    shp.quote_header_id = aql.quote_header_id
    AND    shp.quote_line_id = aql.quote_line_id
    AND    oha.source_document_id = aqh.quote_header_id
    AND    ola.header_id = oha.header_id
    AND    ola.source_document_line_Id = shp.shipment_id ;

Friday, January 12, 2018

API Error Handling in Oracle Apps

API Error Handling:
  •   Every API has 3 out parameters as x_return_status, x_msg_count and x_msg_data.
  • Using these 3 parameters, one can use the below code to log or debug errors in APIs.

dbms_output.put_line (SubStr('x_return_status = '||x_return_status, 1, 255));
dbms_output.put_line ('x_msg_count = '||TO_CHAR(x_msg_count));
dbms_output.put_line (SubStr('x_msg_data = '||x_msg_data, 1, 255));
IF x_msg_count >1 THEN
FOR I IN 1..x_msg_count
LOOP
dbms_output.put_line(I ||'.'|| SubStr(FND_MSG_PUB.Get(p_encoded =>FND_API.G_FALSE ), 1, 255));
END LOOP;
END IF;
END;

List of API in Oracle Apps TCA

1. Create a customer account – HZ_CUST_ACCOUNT_V2PUB.CREATE_CUST_ACCOUNT

2. Create a customer account relationship – HZ_CUST_ACCOUNT_V2PUB.CREATE_CUST_ACCT_RELATE

3. Update customer account relationship – HZ_CUST_ACCOUNT_V2PUB.UPDATE_CUST_ACCT_RELATE

4. Update customer account - HZ_CUST_ACCOUNT_V2PUB.UPDATE_CUST_ACCOUNT

5. Create customer profile – HZ_CUSTOMER_PROFILE_V2PUB.CREATE_CUSTOMER_PROFILE

6. Create a customer site – HZ_CUST_ACCOUNT_SITE_V2PUB.CREATE_CUST_ACCT_SITE

7. Create a customer site using – HZ_CUST_ACCOUNT_SITE_V2PUB.CREATE_CUST_SITE_USE

8. Create party site – HZ_PARTY_SITE.V2PUB.CREATE_PARTY_SITE

9. Create party_site_use  - HZ_PARTY_SITE.V2PUB.CREATE_PARTY_SITE_USE

10. Create a person-type party and customer account – HZ_CUST_ACCOUNT_V2PUB.CREATE_CUST_ACCOUNT

11. Create a phone number – HZ_CONTACT_POINT_V2PUB.CREATE_CONTACT_POINT

12. Create customer address – HZ_LOCATION_V2PUB.CREATE_LOCATION

13. Create group – HZ_PARTY_V2PUB.CREATE_GROUP

14. Update a customer profile – HZ_CUSTOMER_PROFILE_V2PUB.UPDATE_CUSTOMER_PROFILE

15. Update customer address – HZ_LOCATION_V2PUB.UPDATE_LOCATION

16. Create an organization-type party and customer account - HZ_CUST_ACCOUNT_V2PUB.CREATE_CUST_ACCOUNT

 17. Create person  - HZ_PARTY_V2PUB.CREATE_PERSON

 18. Create organization – HZ_PARTY_V2PUB.CREATE_ORGANIZATION