Monday, December 19, 2011

Can we define cursor inside package if so how to call cursors inside the package?

Yes, we can. call using packagename.cursorname.
Ex:  OPEN XX_INVOICE_VAL_PKG.c_eligible_invoice
       LOOP XX_INVOICE_VAL_PKG.c_eligible_invoice..

       END LOOP;

When you query Multi-Org tables in SQL*Plus or any tool (like TOAD), we get zero rows, though actually those tables got data? What is the reason? What do you do then?

Nee to set org context to the current user session.
Single-org:
Execute mo_global.set_policy_context('S',&org_id);
Multi-org:
Begin
FND_GLOBAL.apps_initialize(
   l_user_id,    -- User id
   l_resp_id,    -- Responsibility Id
   200);           -- Application Id
MO_GLOBAL.init('SQLAP');
End;

How do you stop inserting records after 50 records are inserted from flat file through sql*loader


This can be done using option LOAD

OPTIONS (SKIP=1, LOAD=10, ERRORS=5)
LOAD DATA infile c:/sv_test.dat
REPLACE INTO TABLE sv_test_sql_tbl
FIELDS TERMINATED BY "," optionally enclosed by '"'
trailing nullcols
(
    item_number     "trim(:item_number)"
  , vendor_name "trim(:vendor_name)"
  , vendor_site_name "trim(:vendor_site_name)"
  , supplier_item   "trim(:supplier_item)"
  , process_flag  Constant 'UNPROCESSED'
)

In the example above the total records to be loaded is limited to 10, error records is 5 and 1 record is skipped.

These options can also be given with sqlldr command as follows..

sqlldr control='sv_test.ctl' data='sv_test.dat' load=10 errors=5  skip=1

Can we set Index for primary key? If so what is the name of the Index?


Oracle automatically creates an index for each UNIQUE or PRIMARY KEY declaration. For example, if you create a table foo as follows:

create table foo (a int primary key,
                          b varchar(20) unique);

Oracle will automatically create one index on foo.a and another on foo.b. Note that you cannot drop indexes for UNIQUE and PRIMARY KEY attributes. These indexes are dropped automatically when you drop the table or the key constraints (see the section on Constraints).

To find out what indexes you have, use
    select index_name from user_indexes;

USER_INDEXES is another system table just like USER_TABLES. This can become especially helpful if you forget the names of your indexes and therefore cannot drop them. You might also see weird names of the indexes created by Oracle for UNIQUE and PRIMARY KEY attributes, but you will not be able to drop these indexes.

How do you track a Request Set? If an error occurs in the first program in a Request Set, I need to control the execution of other programs in the Request Set, How do you achieve this?

While creating Request Set, you can select execution sequance as Parallel or Sequancially. In this case u select as Sequance, so one fails other wont be exceuted.

How will you debug your reports?

SRW.MESSAGE (msg_number NUMBER, msg_text CHAR);

Decode vs Case

Example for Decode and Case:

SQL> SELECT ename,
  2         DECODE(deptno, 10, 'ACCOUNTING',
  3                        20, 'RESEARCH',
  4                        30, 'SALES',
  5                        40, 'OPERATIONS',
  6                            'UNKNOWN') AS department
  7    FROM emp
  8   WHERE rownum < 4
  9  /

SQL> SELECT ename,
  2         CASE deptno
  3           WHEN 10 THEN 'ACCOUNTING'
  4           WHEN 20 THEN 'RESEARCH'
  5           WHEN 30 THEN 'SALES'
  6           WHEN 40 THEN 'OPERATIONS'
  7           ELSE
  8                        'UNKNOWN'
  9         END AS department
 10    FROM emp
 11   WHERE rownum < 4
 12  /

Difference:
1.
DECODE works with expressions which are scalar values.
CASE can work with predicates and subqueries in searchable form

SQL> select ename,
  2   case
  3     when ename in ('KING','SMITH','ALLEN') then
  4        'Managers'
  5     when exists (select 1 from dept where deptno = emp.deptno and deptno = 10) then
  6        'Guy from 10th'
  7     else
  8        'Another person'
  9   end blah_blah
 10  from emp  
 11  /

2.
CASE executes faster in the optimizer than does DECODE.

3.
DECODE will return "true" if you compare NULL to NULL. CASE will not.

DECODE(NULL, NULL, 1, 0)
will return '1'.

CASE NULL
    WHEN NULL THEN 1
    ELSE 0
END
will return '0'.

You would have to write it as:
CASE
    WHEN NULL IS NULL THEN 1
    ELSE 0
END
will return '1'

4.
DECODE can be used Only inside SQL statement....
But CASE can be used any where even as a paramtre of a function/procedure

SQL> Declare
  1  a varchar2 := 'ONE';
  2  Begin
  3    procedure_01(case :a when 'ONE' then 1 else 0 end);
  4  End;
  5  /
The number  = 1