Saturday, August 3, 2019

Interview Questions



Written
  1. select trunc(round(156.00,-1),-1) from dual
160
  1. IN, ANY, ALL
divide into 10 (or n) batches dynamically
SELECT
    row_number() over (partition by mod(rownum,100) order by rownum) batch_number
    ,h.*
FROM
    oe_order_headers_all h
WHERE
    ROWNUM < 1001
    order by batch_number;
calculate no. of working days for a given month
For current month:
select trunc(last_day(sysdate))-trunc(sysdate,'MM')+1 - (
select count(*) voffs from
(select vdate,to_char(vdate,'DY') vday from
(select trunc(sysdate,'MM')+level-1 vdate from dual
connect by level <=  ABS(trunc(sysdate,'MM') - last_day(sysdate)-1)))
where
vday in ('SAT','SUN')) Working_days
from dual;
For any month:
select trunc(last_day(DATE '2018-11-01'))-trunc(DATE '2018-11-01','MM')+1 - (
select count(*) voffs from
(select vdate,to_char(vdate,'DY') vday from
(select trunc(date '2018-11-01','MM')+level-1 vdate from dual
connect by level <=  ABS(trunc(date '2018-11-01','MM') - last_day(date '2018-11-01')-1)))
where
vday in ('SAT','SUN')) Working_days
from dual;
what are the inbound interface you worked on
Item conversion
how to handle exception when you use bulk collect
SET SERVEROUTPUT ON
DECLARE
  TYPE t_tab IS TABLE OF exception_test%ROWTYPE;

  l_tab          t_tab := t_tab();
  l_error_count  NUMBER;

  ex_dml_errors EXCEPTION;
  PRAGMA EXCEPTION_INIT(ex_dml_errors, -24381);
BEGIN
  -- Fill the collection.
  FOR i IN 1 .. 100 LOOP
    l_tab.extend;
    l_tab(l_tab.last).id := i;
  END LOOP;

  -- Cause a failure.
  l_tab(50).id := NULL;
  l_tab(51).id := NULL;

  EXECUTE IMMEDIATE 'TRUNCATE TABLE exception_test';

  -- Perform a bulk operation.
  BEGIN
    FORALL i IN l_tab.first .. l_tab.last SAVE EXCEPTIONS
      INSERT INTO exception_test
      VALUES l_tab(i);
  EXCEPTION
    WHEN ex_dml_errors THEN
      l_error_count := SQL%BULK_EXCEPTIONS.count;
      DBMS_OUTPUT.put_line('Number of failures: ' || l_error_count);
      FOR i IN 1 .. l_error_count LOOP
        DBMS_OUTPUT.put_line('Error: ' || i ||
          ' Array Index: ' || SQL%BULK_EXCEPTIONS(i).error_index ||
          ' Message: ' || SQLERRM(-SQL%BULK_EXCEPTIONS(i).ERROR_CODE));
      END LOOP;
  END;
END;
/
steps for conversion
how to load data from flat file into oracle staging table using .CTL file (full code)
Solution
Step 1
Start MS-Access and convert the table into comma delimited flat (popularly known as csv), by clicking on File/Save As menu. Let the delimited file name be emp.csv

Now transfer this file to Linux Server using FTP command
a.     Go to Command Prompt in windows
b.     At the command prompt type FTP followed by IP address of the server running Oracle.

FTP will then prompt you for username and password to connect to the Linux Server. Supply a valid username and password of Oracle User in Linux

For example:-
c.  C:\> ftp 200.200.100.111
Name: oracle


            Password:oracle
FTP>
d.     Now give PUT command to transfer file from current Windows machine to Linux machine
e.  FTP>put
Local file:C:\>emp.csv
remote-file:/u01/oracle/emp.csv
f.   
g.  File transferred in 0.29 Seconds
FTP>
h.     Now after the file is transferred quit the FTP utility by typing bye command.
FTP>bye
Good-Bye
Step 2
Now come to the Linux Machine and create a table in Oracle with the same structure as in MS-ACCESS by taking appropriate datatypes. For example,  create a table like this
$ sqlplus scott/tiger
SQL> CREATE TABLE emp (empno number(5),
   name varchar2(50),
   sal  number(10,2),
   jdate date);
Step 3
After creating the table, you have to write a control file describing the actions which SQL Loader should do. You can use any text editor to write the control file. Now let us write a controlfile for our case study
$ vi emp.ctl

1 LOAD DATA
2 INFILE ‘/u01/oracle/emp.csv’
3 BADFILE      ‘/u01/oracle/emp.bad’
4 DISCARDFILE  ‘/u01/oracle/emp.dsc’
5 INSERT INTO TABLE emp
6 FIELDS TERMINATED BY “,” OPTIONALLY ENCLOSED BY ‘”’ TRAILING NULLCOLS
7 (empno,name,sal,jdate date ‘mm/dd/yyyy’)
Notes: (Do not write the line numbers, they are meant for explanation purpose)
1.       The LOAD DATA statement is required at the beginning of the control file.
2.       The INFILE option specifies where the input file is located
3.       Specifying BADFILE is optional. If you specify,  then bad records found during loading will be stored in this file.
4.       Specifying DISCARDFILE is optional. If you specify, then records which do not meet a WHEN condition will be written to this file.
5.       You can use any of the following loading option
    i.       INSERT : Loads rows only if the target table is empty
    ii.       APPEND: Load rows if the target table is empty or not.
    iii.      REPLACE: First deletes all the rows in the existing table and then, load rows.
    iv.      TRUNCATE: First truncates the table and then load rows.
6.       This line indicates how the fields are separated in input file. Since in our case the fields are separated by “,” so we have specified “,” as the terminating char for fields. You can replace this by any char which is used to terminate fields. Some of the popularly use terminating characters are semicolon “;”, colon “:”, pipe “|” etc. TRAILING NULLCOLS means if the last column is null then treat this as null value, otherwise,  SQL LOADER will treat the record as bad if the last column is null.
7.        In this line specify the columns of the target table. Note how do you specify format for Date columns
Step 4
After you have wrote the control file save it and then, call SQL Loader utility by typing the following command
$sqlldr userid=scott/tiger control=emp.ctl log=emp.log
After you have executed the above command SQL Loader will shows you the output describing how many rows it has loaded.

The LOG option of sqlldr specifies where the log file of this sql loader session should be created. The log file contains all actions which SQL loader has performed i.e. how many rows were loaded, how many were rejected and how much time is taken to load the rows and etc. You have to view this file for any errors encountered while running SQL Loader.


No comments:

Post a Comment

Link between Receivables, Payables and Projects

 --Link between AP, AR and PA select aia.* from ap_invoices_all aia ,pjc_exp_items_all peia ,pjb_bill_trxs pbt ,pjb_inv_line_dists pild ,pjb...