Monday, June 10, 2013

PL/SQL Collections

This is one of those topics i used to dread when i started out as a database developer, not anymore. But still from time to time i need to refresh my memory when it comes to collections:

I recently started reading a book "Oracle PL/SQL Tuning: Expert Secrets for High Performance Programming" and it describes collections in such a way that i have to make a post of it. It is precise, with an example and perfect read for me:

Below are a few pointers that i usually like to remember:

Associative Arrays (TABLE OF)

  • AKA Index by tables
  • Of course use an index by BINARY_INTEGER or PLS_INTEGER or VARCHAR2
  • Sparse collection
  • Need not be consecutive
  • Unbounded - no upper boundary
  • Collection is extended by assigning values to non existing index values
  • Data can be deleted
  • Start with FIRST method to access data

Nested Tables (TABLE OF)

  • No index
  • Unbounded colleciton
  • Dense collection when creating collection
  • Data can be deleted
  • NEXT and PRIOR (?) helps access collection elements

VARRAYS (VARRAY OF)

  • No index
  • Bounded collection so cannot be extended above the specified limit
  • Data cannot be deleted
  • Dense data collection
  • Consecutive data
  • Start with FIRST method to access data
Methods that can be used with collections. Not all methods can be used with all collections:
  • EXISTS - exists(v) - returns boolean
  • DELETE - delete, delete(v1) and delete(v1,v8) - removes data
  • COUNT - returns number
  • TRIM - trim and trim (v) - removes data
  • LIMIT - returns number
  • EXTEND - extend, extend(v1) and extend(v1,v8) - adds NULL elements
  • FIRST - returns index of first element
  • LAST - returns index of last element
  • PRIOR - prior(v) - returns index of prior element
  • NEXT - next(v) - returns index of next element
Hope this helps....

Friday, June 7, 2013

Quick Tip #4

To see what SQL a session is running in the database, find the SPID for that user using sp_who2 and then pass it as an input to the dbcc command.


sp_who2

dbcc inputbuffer (:SPID)

Thursday, June 6, 2013

NULL SELF Argument Is Disallowed - Oracle

ORA-30625: method dispatch on NULL SELF argument is disallowed 

Cause: A member method of a type is being invoked with a NULL SELF argument. 
Action: Change the method invocation to pass in a valid self argument.


Well i was trying to parse an XML with multiple namespaces and was using EXISTSNODE function. When traversing between nodes and subnodes i forgot to remove a subnode name and the XPATH formed was incorrect:

    l_xmltype := xmltype (l_doc) ;
    l_index := 1;
    v_count := l_xmltype.existsnode (
    'ns1:NewSubscriptionNotificationRequest/subscriptionInfo/subscriptionKey/subscriptionInfo [' || to_char (l_index
    ) || ']', v_namespaces);
    dbms_output.put_line ('------' || v_count);
    --
    WHILE l_xmltype.existsnode (
    'ns1:NewSubscriptionNotificationRequest/subscriptionInfo [' || TO_CHAR (l_index
    ) || ']', v_namespaces) > 0
    LOOP
        l_value := l_xmltype.extract (
        'ns1:NewSubscriptionNotificationRequest/subscriptionInfo [' || to_char (
        l_index) || ']/duration/text()', v_namespaces) .getstringval () ;
        dbms_output.put_line ('-------------> ' || l_value) ;
        --
        l_index := l_index + 1;

    END LOOP;

This error is same as the evil NULL POINTER EXCEPTION in JAVA....

Just correcting the subnode path (remove subscriptionInfo from EXISTSNODE) to represent right structure helped resolve this problem.

Many thanks to A_Non On XML for his detailed explanation and examples on this blog about XML parsing. I hope he does not mind a well deserved praise.