PL/SQL: Populating Cursor with List then do IF EXISTS against Table

Here is my issue. The DB I work with is an Event Database (Network Events). I have a task to take a list of 2600 nodes and see if these nodes exist with in a set of events which would be found with a certain WHERE clause. On my first pass, I did the WHERE clause AND NODE in (List of 2600 nodes). Well you can only put 1000 items in an in list so I split it up into 3 reports and then did a UNION all between them. This used 15% of db resources and ran for hours and the dba group would like me to find a better way. They would prefer that I use PL/SQL.
I'm new to pl/sql (but not to programming nor sql). So the first thing I did is just figure out how to get my list into an Varray, which I then switched to a table because I could not find any examples, after hours and hours of searching the web,
of referencing a Varray with SQL syntax.
******** BEGIN CODE Sample 1 ***********
DECLARE
DECLARE
TYPE NodeList IS TABLE OF VARCHAR2(255);
Nodes NodeList := NodeList('node1', node2', [......list of 2597 nodes..], 'node2600')
BEGIN
dbms_output.enable(1000000);
FOR i IN Nodes.FIRST .. Nodes.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('Relevant Items: ' || Nodes(i));
END LOOP;
END
******** END CODE Sample 1 ***********
The pseudocode I'm envisioning goes something like this:
IF any of these nodes (2600 node list) ,
exist in the nodes for this WHERE clause (which is pulling from the db event table),
then print out the records (of the events in the db event table) that contain those nodes (i.e the nodes from the list).
I think I found an example of doing this with 2 cursors but I cant find that example again. And, anyway, I cant figure out how to get my list into a cursor. I have many examples of how to fill a cursor via a select against a db.
I tried this but it did not work:
******** BEGIN CODE Sample 2 ***********
DECLARE
DECLARE
TYPE NodeList IS TABLE OF VARCHAR2(255);
Nodes NodeList := NodeList('node1', node2', [......list of 2597 nodes..], 'node2600')
CURSOR MOM_OVO_NODES IS
SELECT Node
FROM NodeList
ORDER BY Node;
BEGIN
dbms_output.enable(1000000);
FOR nodelist_rec IN MOM_OVO_NODES
LOOP
dbms_output.put_line('Relevant Nodes: '||nodelist_rec.node);
END LOOP;
END
******** END CODE Sample 2 ***********
Is there a way to get this list into a cursor? Or am I thinking about the whole thing incorrectly?
brad

Every time I come to OTN thinking I finally have a reason to use PL/SQL, I find that I really only need SQL! I'm never going to get to learn PL/SQL! :)
Thanks for the link. The information is well written.
Unfortunately, I dont have the rights to create a GTT table. I get ORA-01031.
I work for a very large organization where even the DBA's are so task individualized that many of them can only do very specific tasks. So I dont see myself getting rights to create even temporary tables. That would probably take and act of God.
So, accepting that pl/sql is NOT the best practices solution here. I'd still like to , as an exercise for the student, understand how I can populate a cursor with a list. Why wont my example work? Since I could ONLY find examples of using SELECT to populate a cursor, I turned my VARRAY into a table. Then I learned that I could initialize the table in the DECLARE section. Then I put my cursor declaration after the table initialization. Still no luck. Then I saw casting as TABLE mentioned but got nowhere with that. IS there a way to get that list into a cursor? Its really bothering me.
Also, since its a table, and, as far as I know, in referring to a table, one needs to reference a column, what is the syntax for naming the single column in my table?
I AM reading the pl/sql books on Safari, and I do have the 10lb tome from Steven Feuerstein sitting next to me. <-- I'd page through it more often if I could find a reason to use PL/SQL. Actually, the DBA group has mandated that I write everything in PL/SQL for "performance" reasons. I dont actually adhere to that but there it is.
Also, whats the protocol for giving credit for answers on threads like this where several people give relatively the same answer? Do I just give the earliest time stamp full credit or give them all a "helpful"?
brad

Similar Messages

  • PL/SQL cursor with FOR UPDATE STATEMENT

    Welcome,
    I have some troubles with cursors. When I try update values in table using cursor i receive ORA-01410 Error : "INVALID ROWID".
    I use code as below:
    ALTER SESSION SET CURRENT_SCHEMA=TEST_SCHEMA;
    DECLARE
    TYPE LogTable_typ IS TABLE OF ADMIN_FILE_LOG%ROWTYPE;
    v_ModuleId KTIMS.ADMIN_FILE_LOG.MODULE_ID%TYPE;
    v_CDR KTIMS.ADMIN_FILE_LOG.CDR_SUCCESS%TYPE;
    CURSOR c1 IS
    SELECT MODULE_ID, cdr_success FROM ADMIN_FILE_LOG
    FOR UPDATE OF CDR_SUCCESS NOWAIT;
    BEGIN
    OPEN c1;
    LOOP
    FETCH c1 INTO v_ModuleId,v_CDR;
    IF v_ModuleId = 'LOAD' THEN
    UPDATE ADMIN_FILE_LOG SET CDR_SUCCESS = 70 WHERE CURRENT OF c1;
    END IF;
    EXIT WHEN c1%NOTFOUND;
    END LOOP;
    EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(SQLERRM || SQLCODE);
    END;
    When I use ROWID in cursor declaration all works fine.Working code is:
    ALTER SESSION SET CURRENT_SCHEMA=KTIMS;
    DECLARE
    TYPE LogTable_typ IS TABLE OF ADMIN_FILE_LOG%ROWTYPE;
    v_ModuleId KTIMS.ADMIN_FILE_LOG.MODULE_ID%TYPE;
    v_CDR KTIMS.ADMIN_FILE_LOG.CDR_SUCCESS%TYPE;
    v_id ROWID;
    CURSOR c1 IS
    SELECT MODULE_ID, cdr_success, ROWID FROM ADMIN_FILE_LOG
    FOR UPDATE OF CDR_SUCCESS NOWAIT;
    BEGIN
    OPEN c1;
    LOOP
    FETCH c1 INTO v_ModuleId,v_CDR,v_id;
    IF v_ModuleId = 'LOAD' THEN
    UPDATE ADMIN_FILE_LOG SET CDR_SUCCESS = 70 WHERE ROWID = v_id;
    END IF;
    EXIT WHEN c1%NOTFOUND;
    END LOOP;
    EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(SQLERRM || SQLCODE);
    END;
    What is difference in this two cases ?
    I try to find this in Oracle documentation "Database PL/SQL User's Guide and Reference" ( http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#i45288 ).
    Please help.

    Hi,
    I think the USE of NOWAIT clause in cursor for update is, to remove the lock immediately after the transaction is over.
    In the second example where you are fetching the rowid explicitly and use the same id in loop to make the update, so there should not be any problem in this case.
    In the first example when you are using CURRENT OF to do the update, it is basically work on basis of latest fetched row from cursor and do the update (but i think implicitly it use the reference of row id also).
    I am not sure about it , but still try once by removing the NOWAIT clause from your cursor for update and try once , see whether you are still facing the error or not.

  • I deleted all my playlists provided. However, they are still on my Ipod Touch. Now, when I sync my Ipod with Itunes then the lists are copied back into Itunes?

    I deleted all my playlists provided. However, they are still on my Ipod Touch. Now, when I sync my Ipod with Itunes then the lists are copied back into Itunes?

    A playlist takes up almost no room. A few kB at most.
    It is simply a list of songs in the library
    Do you want the playlists or not?
    Now, when I sync my Ipod with Itunes then the lists are copied backinto iTunes?
    Do you want them copied back into iTunes?

  • How do I select a playlist to copy onto my devices?  On the older version, I could click on each list, then check "sync only checked playlists" and I'd have different playlists on different devices.  How do I do it with this version?  Thank you!

    How in 12.1.0.50 can I select a playlist to copy onto my devices from my iMac?  On the older version, I could click on each list, then check "sync only checked playlists" and I'd have different playlists on different devices (iPhone 6, iPod nano).  How do I do it with this version?  Thank you!

    Same way.
    Select the device, click Music tab and select the playlists to sync.

  • Populating a SELECT list from an Oracle Table

    Hello,
    I would like to populate a second SELECT list on
    a web page based on the selected value in the first
    SELECT list. I would like to take the value
    that the user selects in the first SELECT list and
    use it in a WHERE clause to bring back distinct
    values to display in the second SELECT list.
    I would like to do this all on a single page. As
    soon as the user selects a value in the first SELECT
    list, then the correct values retrieved from the
    database would appear in the second select list.
    Is this possible? Do you have sample code or ideas??
    I can write JavaScript to get the selected value, but
    I cannot get to Oracle data from the JavaScript environment.
    I can write PL/SQL or Java procedure to access
    Oracle data, but I cannot make the complete connection.
    Help, please.
    thanks
    paul

    Unless you can get every combination back in the first SQL statement, you'd have to do something along the lines of
    - Have the JavaScript submit the page as soon as the first pull-down is selected
    - Have the response be a page identical to the first page, but with the second pull-down populated.
    The other option would be to have a Java applet that can make the second query and return results. Since applets can't connect to the database by default, you'd have to have an app server that would do the actual query.
    Justin
    Distributed Database Consulting, Inc.
    www.ddbcinc.com/askDDBC

  • Need to increase performance-bulk collect in cursor with limit and in the for loop inserting into the trigger table

    Hi all,
    I have a performance issue in the below code,where i am trying to insert the data from table_stg into target_tab and in parent_tab tables and then to child tables via cursor with bulk collect .the target_tab and parent_tab are huge tables and have a row wise trigger enabled on it .the trigger is mandatory . This timetaken for this block to execute is 5000 seconds.Now my requirement is to reduce it to 5 to 10 mins.
    can someone please guide me here.Its bit urgent .Awaiting for your response.
    declare
    vmax_Value NUMBER(5);
      vcnt number(10);
      id_val number(20);
      pc_id number(15);
      vtable_nm VARCHAR2(100);
      vstep_no  VARCHAR2(10);
      vsql_code VARCHAR2(10);
      vsql_errm varchar2(200);
      vtarget_starttime timestamp;
      limit_in number :=10000;
      idx           number(10);
              cursor stg_cursor is
             select
                   DESCRIPTION,
                   SORT_CODE,
                   ACCOUNT_NUMBER,
                     to_number(to_char(CORRESPONDENCE_DATE,'DD')) crr_day,
                     to_char(CORRESPONDENCE_DATE,'MONTH') crr_month,
                     to_number(substr(to_char(CORRESPONDENCE_DATE,'DD-MON-YYYY'),8,4)) crr_year,
                   PARTY_ID,
                   GUID,
                   PAPERLESS_REF_IND,
                   PRODUCT_TYPE,
                   PRODUCT_BRAND,
                   PRODUCT_HELD_ID,
                   NOTIFICATION_PREF,
                   UNREAD_CORRES_PERIOD,
                   EMAIL_ID,
                   MOBILE_NUMBER,
                   TITLE,
                   SURNAME,
                   POSTCODE,
                   EVENT_TYPE,
                   PRIORITY_IND,
                   SUBJECT,
                   EXT_PRD_ID_TX,
                   EXT_PRD_HLD_ID_TX,
                   EXT_SYS_ID,
                   EXT_PTY_ID_TX,
                   ACCOUNT_TYPE_CD,
                   COM_PFR_TYP_TX,
                   COM_PFR_OPT_TX,
                   COM_PFR_RSN_CD
             from  table_stg;
    type rec_type is table of stg_rec_type index by pls_integer;
    v_rt_all_cols rec_type;
    BEGIN
      vstep_no   := '0';
      vmax_value := 0;
      vtarget_starttime := systimestamp;
      id_val    := 0;
      pc_id     := 0;
      success_flag := 0;
              vstep_no  := '1';
              vtable_nm := 'before cursor';
        OPEN stg_cursor;
              vstep_no  := '2';
              vtable_nm := 'After cursor';
       LOOP
              vstep_no  := '3';
              vtable_nm := 'before fetch';
    --loop
        FETCH stg_cursor BULK COLLECT INTO v_rt_all_cols LIMIT limit_in;
                  vstep_no  := '4';
                  vtable_nm := 'after fetch';
    --EXIT WHEN v_rt_all_cols.COUNT = 0;
        EXIT WHEN stg_cursor%NOTFOUND;
    FOR i IN 1 .. v_rt_all_cols.COUNT
      LOOP
       dbms_output.put_line(upper(v_rt_all_cols(i).event_type));
        if (upper(v_rt_all_cols(i).event_type) = upper('System_enforced')) then
                  vstep_no  := '4.1';
                  vtable_nm := 'before seq sel';
              select PC_SEQ.nextval into pc_id from dual;
                  vstep_no  := '4.2';
                  vtable_nm := 'before insert corres';
              INSERT INTO target1_tab
                           (ID,
                            PARTY_ID,
                            PRODUCT_BRAND,
                            SORT_CODE,
                            ACCOUNT_NUMBER,
                            EXT_PRD_ID_TX,         
                            EXT_PRD_HLD_ID_TX,
                            EXT_SYS_ID,
                            EXT_PTY_ID_TX,
                            ACCOUNT_TYPE_CD,
                            COM_PFR_TYP_TX,
                            COM_PFR_OPT_TX,
                            COM_PFR_RSN_CD,
                            status)
             VALUES
                            (pc_id,
                             v_rt_all_cols(i).party_id,
                             decode(v_rt_all_cols(i).product_brand,'LTB',2,'HLX',1,'HAL',1,'BOS',3,'VER',4,0),
                             v_rt_all_cols(i).sort_code,
                             'XXXX'||substr(trim(v_rt_all_cols(i).ACCOUNT_NUMBER),length(trim(v_rt_all_cols(i).ACCOUNT_NUMBER))-3,4),
                             v_rt_all_cols(i).EXT_PRD_ID_TX,
                             v_rt_all_cols(i).EXT_PRD_HLD_ID_TX,
                             v_rt_all_cols(i).EXT_SYS_ID,
                             v_rt_all_cols(i).EXT_PTY_ID_TX,
                             v_rt_all_cols(i).ACCOUNT_TYPE_CD,
                             v_rt_all_cols(i).COM_PFR_TYP_TX,
                             v_rt_all_cols(i).COM_PFR_OPT_TX,
                             v_rt_all_cols(i).COM_PFR_RSN_CD,
                             NULL);
                  vstep_no  := '4.3';
                  vtable_nm := 'after insert corres';
        else
              select COM_SEQ.nextval into id_val from dual;
                  vstep_no  := '6';
                  vtable_nm := 'before insertcomm';
          if (upper(v_rt_all_cols(i).event_type) = upper('REMINDER')) then
                vstep_no  := '6.01';
                  vtable_nm := 'after if insertcomm';
              insert into parent_tab
                 (ID ,
                 CTEM_CODE,
                 CHA_CODE,            
                 CT_CODE,                           
                 CONTACT_POINT_ID,             
                 SOURCE,
                 RECEIVED_DATE,                             
                 SEND_DATE,
                 RETRY_COUNT)
              values
                 (id_val,
                  lower(v_rt_all_cols(i).event_type), 
                  decode(v_rt_all_cols(i).product_brand,'LTB',2,'HLX',1,'HAL',1,'BOS',3,'VER',4,0),
                  'Email',
                  v_rt_all_cols(i).email_id,
                  'IADAREMINDER',
                  systimestamp,
                  systimestamp,
                  0);  
         else
                vstep_no  := '6.02';
                  vtable_nm := 'after else insertcomm';
              insert into parent_tab
                 (ID ,
                 CTEM_CODE,
                 CHA_CODE,            
                 CT_CODE,                           
                 CONTACT_POINT_ID,             
                 SOURCE,
                 RECEIVED_DATE,                             
                 SEND_DATE,
                 RETRY_COUNT)
              values
                 (id_val,
                  lower(v_rt_all_cols(i).event_type), 
                  decode(v_rt_all_cols(i).product_brand,'LTB',2,'HLX',1,'HAL',1,'BOS',3,'VER',4,0),
                  'Email',
                  v_rt_all_cols(i).email_id,
                  'CORRESPONDENCE',
                  systimestamp,
                  systimestamp,
                  0); 
            END if; 
                  vstep_no  := '6.11';
                  vtable_nm := 'before chop';
             if (v_rt_all_cols(i).ACCOUNT_NUMBER is not null) then 
                      v_rt_all_cols(i).ACCOUNT_NUMBER := 'XXXX'||substr(trim(v_rt_all_cols(i).ACCOUNT_NUMBER),length(trim(v_rt_all_cols(i).ACCOUNT_NUMBER))-3,4);
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 'IB.Correspondence.AccountNumberMasked',
                 v_rt_all_cols(i).ACCOUNT_NUMBER);
             end if;
                  vstep_no  := '6.1';
                  vtable_nm := 'before stateday';
             if (v_rt_all_cols(i).crr_day is not null) then 
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 --'IB.Correspondence.Date.Day',
                 'IB.Crsp.Date.Day',
                 v_rt_all_cols(i).crr_day);
             end if;
                  vstep_no  := '6.2';
                  vtable_nm := 'before statemth';
             if (v_rt_all_cols(i).crr_month is not null) then 
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 --'IB.Correspondence.Date.Month',
                 'IB.Crsp.Date.Month',
                 v_rt_all_cols(i).crr_month);
             end if;
                  vstep_no  := '6.3';
                  vtable_nm := 'before stateyear';
             if (v_rt_all_cols(i).crr_year is not null) then 
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)
              values
                (id_val,
                 --'IB.Correspondence.Date.Year',
                 'IB.Crsp.Date.Year',
                 v_rt_all_cols(i).crr_year);
             end if;
                  vstep_no  := '7';
                  vtable_nm := 'before type';
               if (v_rt_all_cols(i).product_type is not null) then
                  insert into child_tab
                     (COM_ID,                                            
                     KEY,                                                                                                                                        
                     VALUE)
                  values
                    (id_val,
                     'IB.Product.ProductName',
                   v_rt_all_cols(i).product_type);
                end if;
                  vstep_no  := '9';
                  vtable_nm := 'before title';         
              if (trim(v_rt_all_cols(i).title) is not null) then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE )
              values
                (id_val,
                 'IB.Customer.Title',
                 trim(v_rt_all_cols(i).title));
              end if;
                  vstep_no  := '10';
                  vtable_nm := 'before surname';
              if (v_rt_all_cols(i).surname is not null) then
                insert into child_tab
                   (COM_ID,                                            
                   KEY,                                                                                                                                          
                   VALUE)
                values
                  (id_val,
                  'IB.Customer.LastName',
                  v_rt_all_cols(i).surname);
              end if;
                            vstep_no  := '12';
                            vtable_nm := 'before postcd';
              if (trim(v_rt_all_cols(i).POSTCODE) is not null) then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)                              
               values
                (id_val,
                 'IB.Customer.Addr.PostCodeMasked',
                  substr(replace(v_rt_all_cols(i).POSTCODE,' ',''),length(replace(v_rt_all_cols(i).POSTCODE,' ',''))-2,3));
              end if;
                            vstep_no  := '13';
                            vtable_nm := 'before subject';
              if (trim(v_rt_all_cols(i).SUBJECT) is not null) then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)                              
               values
                (id_val,
                 'IB.Correspondence.Subject',
                  v_rt_all_cols(i).subject);
              end if;
                            vstep_no  := '14';
                            vtable_nm := 'before inactivity';
              if (trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) is null or
                  trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) = '3' or
                  trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) = '6' or
                  trim(v_rt_all_cols(i).UNREAD_CORRES_PERIOD) = '9') then
              insert into child_tab
                 (COM_ID,                                            
                 KEY,                                                                                                                                            
                 VALUE)                              
               values
                (id_val,
                 'IB.Correspondence.Inactivity',
                  v_rt_all_cols(i).UNREAD_CORRES_PERIOD);
              end if;
                          vstep_no  := '14.1';
                          vtable_nm := 'after notfound';
        end if;
                          vstep_no  := '15';
                          vtable_nm := 'after notfound';
        END LOOP;
        end loop;
                          vstep_no  := '16';
                          vtable_nm := 'before closecur';
        CLOSE stg_cursor;
                          vstep_no  := '17';
                          vtable_nm := 'before commit';
        DELETE FROM table_stg;
      COMMIT;
                          vstep_no  := '18';
                          vtable_nm := 'after commit';
    EXCEPTION
    WHEN OTHERS THEN
      ROLLBACK;
      success_flag := 1;
      vsql_code := SQLCODE;
      vsql_errm := SUBSTR(sqlerrm,1,200);
      error_logging_pkg.inserterrorlog('samp',vsql_code,vsql_errm, vtable_nm,vstep_no);
      RAISE_APPLICATION_ERROR (-20011, 'samp '||vstep_no||' SQLERRM:'||SQLERRM);
    end;
    Thanks

    Its bit urgent
    NO - it is NOT urgent. Not to us.
    If you have an urgent problem you need to hire a consultant.
    I have a performance issue in the below code,
    Maybe you do and maybe you don't. How are we to really know? You haven't posted ANYTHING indicating that a performance issue exists. Please read the FAQ for how to post a tuning request and the info you need to provide. First and foremost you have to post SOMETHING that actually shows that a performance issue exists. Troubleshooting requires FACTS not just a subjective opinion.
    where i am trying to insert the data from table_stg into target_tab and in parent_tab tables and then to child tables via cursor with bulk collect .the target_tab and parent_tab are huge tables and have a row wise trigger enabled on it .the trigger is mandatory . This timetaken for this block to execute is 5000 seconds.Now my requirement is to reduce it to 5 to 10 mins.
    Personally I think 5000 seconds (about 1 hr 20 minutes) is very fast for processing 800 trillion rows of data into parent and child tables. Why do you think that is slow?
    Your code has several major flaws that need to be corrected before you can even determine what, if anything, needs to be tuned.
    This code has the EXIT statement at the beginning of the loop instead of at the end
        FETCH stg_cursor BULK COLLECT INTO v_rt_all_cols LIMIT limit_in;
                  vstep_no  := '4';
                  vtable_nm := 'after fetch';
    --EXIT WHEN v_rt_all_cols.COUNT = 0;
        EXIT WHEN stg_cursor%NOTFOUND;
    The correct place for the %NOTFOUND test when using BULK COLLECT is at the END of the loop; that is, the last statement in the loop.
    You can use a COUNT test at the start of the loop but ironically you have commented it out and have now done it wrong. Either move the NOTFOUND test to the end of the loop or remove it and uncomment the COUNT test.
    WHEN OTHERS THEN
      ROLLBACK;
    That basically says you don't even care what problem occurs or whether the problem is for a single record of your 10,000 in the collection. You pretty much just throw away any stack trace and substitute your own message.
    Your code also has NO exception handling for any of the individual steps or blocks of code.
    The code you posted also begs the question of why you are using NAME=VALUE pairs for child data rows? Why aren't you using a standard relational table for this data?
    As others have noted you are using slow-by-slow (row by row processing). Let's assume that PL/SQL, the bulk collect and row-by-row is actually necessary.
    Then you should be constructing the parent and child records into collections and then inserting them in BULK using FORALL.
    1. Create a collection for the new parent rows
    2. Create a collection for the new child rows
    3. For each set of LIMIT source row data
      a. empty the parent and child collections
      b. populate those collections with new parent/child data
      c. bulk insert the parent collection into the parent table
      d. bulk insert the child collection into the child table
    And unless you really want to either load EVERYTHING or abandon everything you should use bulk exception handling so that the clean data gets processed and only the dirty data gets rejected.

  • PL/SQL 101 : Cursors and SQL Projection

    PL/SQL 101 : Cursors and SQL Projection
    This is not a question, it's a forum article, in reponse to the number of questions we get regarding a "dynamic number of columns" or "rows to columns"
    There are two integral parts to an SQL Select statement that relate to what data is selected. One is Projection and the other is Selection:-
    Selection is the one that we always recognise and use as it forms the WHERE clause of the select statement, and hence selects which rows of data are queried.
    The other, SQL Projection is the one that is less understood, and the one that this article will help to explain.
    In short, SQL Projection is the collective name for the columns that are Selected and returned from a query.
    So what? Big deal eh? Why do we need to know this?
    The reason for knowing this is that many people are not aware of when SQL projection comes into play when you issue a select statement. So let's take a basic query...
    First create some test data...
    create table proj_test as
      select 1 as id, 1 as rn, 'Fred' as nm from dual union all
      select 1,2,'Bloggs' from dual union all
      select 2,1,'Scott' from dual union all
      select 2,2,'Smith' from dual union all
      select 3,1,'Jim' from dual union all
      select 3,2,'Jones' from dual
    ... and now query that data...
    SQL> select * from proj_test;
             ID         RN NM
             1          1 Fred
             1          2 Bloggs
             2          1 Scott
             2          2 Smith
             3          1 Jim
             3          2 Jones
    6 rows selected.
    OK, so what is that query actually doing?
    To know that we need to consider that all queries are cursors and all cursors are processed in a set manner, roughly speaking...
    1. The cursor is opened
    2. The query is parsed
    3. The query is described to know the projection (what columns are going to be returned, names, datatypes etc.)
    4. Bind variables are bound in
    5. The query is executed to apply the selection and identify the data to be retrieved
    6. A row of data is fetched
    7. The data values from the columns within that row are extracted into the known projection
    8. Step 6 and 7 are repeated until there is no more data or another condition ceases the fetching
    9. The cursor is closed
    The purpose of the projection being determined is so that the internal processing of the cursor can allocate memory etc. ready to fetch the data into. We won't get to see that memory allocation happening easily, but we can see the same query being executed in these steps if we do it programatically using the dbms_sql package...
    CREATE OR REPLACE PROCEDURE process_cursor (p_query in varchar2) IS
      v_sql       varchar2(32767) := p_query;
      v_cursor    number;            -- A cursor is a handle (numeric identifier) to the query
      col_cnt     integer;
      v_n_val     number;            -- numeric type to fetch data into
      v_v_val     varchar2(20);      -- varchar type to fetch data into
      v_d_val     date;              -- date type to fetch data into
      rec_tab     dbms_sql.desc_tab; -- table structure to hold sql projection info
      dummy       number;
      v_ret       number;            -- number of rows returned
      v_finaltxt  varchar2(100);
      col_num     number;
    BEGIN
      -- 1. Open the cursor
      dbms_output.put_line('1 - Opening Cursor');
      v_cursor := dbms_sql.open_cursor;
      -- 2. Parse the cursor
      dbms_output.put_line('2 - Parsing the query');
      dbms_sql.parse(v_cursor, v_sql, dbms_sql.NATIVE);
      -- 3. Describe the query
      -- Note: The query has been described internally when it was parsed, but we can look at
      --       that description...
      -- Fetch the description into a structure we can read, returning the count of columns that has been projected
      dbms_output.put_line('3 - Describing the query');
      dbms_sql.describe_columns(v_cursor, col_cnt, rec_tab);
      -- Use that description to define local datatypes into which we want to fetch our values
      -- Note: This only defines the types, it doesn't fetch any data and whilst we can also
      --       determine the size of the columns we'll just use some fixed sizes for this example
      dbms_output.put_line(chr(10)||'3a - SQL Projection:-');
      for j in 1..col_cnt
      loop
        v_finaltxt := 'Column Name: '||rpad(upper(rec_tab(j).col_name),30,' ');
        case rec_tab(j).col_type
          -- if the type of column is varchar2, bind that to our varchar2 variable
          when 1 then
            dbms_sql.define_column(v_cursor,j,v_v_val,20);
            v_finaltxt := v_finaltxt||' Datatype: Varchar2';
          -- if the type of the column is number, bind that to our number variable
          when 2 then
            dbms_sql.define_column(v_cursor,j,v_n_val);
            v_finaltxt := v_finaltxt||' Datatype: Number';
          -- if the type of the column is date, bind that to our date variable
          when 12 then
            dbms_sql.define_column(v_cursor,j,v_d_val);
            v_finaltxt := v_finaltxt||' Datatype: Date';
          -- ...Other types can be added as necessary...
        else
          -- All other types we'll assume are varchar2 compatible (implicitly converted)
          dbms_sql.DEFINE_COLUMN(v_cursor,j,v_v_val,2000);
          v_finaltxt := v_finaltxt||' Datatype: Varchar2 (implicit)';
        end case;
        dbms_output.put_line(v_finaltxt);
      end loop;
      -- 4. Bind variables
      dbms_output.put_line(chr(10)||'4 - Binding in values');
      null; -- we have no values to bind in for our test
      -- 5. Execute the query to make it identify the data on the database (Selection)
      -- Note: This doesn't fetch any data, it just identifies what data is required.
      dbms_output.put_line('5 - Executing the query');
      dummy := dbms_sql.execute(v_cursor);
      -- 6.,7.,8. Fetch the rows of data...
      dbms_output.put_line(chr(10)||'6,7 and 8 Fetching Data:-');
      loop
        -- 6. Fetch next row of data
        v_ret := dbms_sql.fetch_rows(v_cursor);
        -- If the fetch returned no row then exit the loop
        exit when v_ret = 0;
        -- 7. Extract the values from the row
        v_finaltxt := null;
        -- loop through each of the Projected columns
        for j in 1..col_cnt
        loop
          case rec_tab(j).col_type
            -- if it's a varchar2 column
            when 1 then
              -- read the value into our varchar2 variable
              dbms_sql.column_value(v_cursor,j,v_v_val);
              v_finaltxt := ltrim(v_finaltxt||','||rpad(v_v_val,20,' '),',');
            -- if it's a number column
            when 2 then
              -- read the value into our number variable
              dbms_sql.column_value(v_cursor,j,v_n_val);
              v_finaltxt := ltrim(v_finaltxt||','||to_char(v_n_val,'fm999999'),',');
            -- if it's a date column
            when 12 then
              -- read the value into our date variable
              dbms_sql.column_value(v_cursor,j,v_d_val);
              v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
          else
            -- read the value into our varchar2 variable (assumes it can be implicitly converted)
            dbms_sql.column_value(v_cursor,j,v_v_val);
            v_finaltxt := ltrim(v_finaltxt||',"'||rpad(v_v_val,20,' ')||'"',',');
          end case;
        end loop;
        dbms_output.put_line(v_finaltxt);
        -- 8. Loop to fetch next row
      end loop;
      -- 9. Close the cursor
      dbms_output.put_line(chr(10)||'9 - Closing the cursor');
      dbms_sql.close_cursor(v_cursor);
    END;
    SQL> exec process_cursor('select * from proj_test');
    1 - Opening Cursor
    2 - Parsing the query
    3 - Describing the query
    3a - SQL Projection:-
    Column Name: ID                             Datatype: Number
    Column Name: RN                             Datatype: Number
    Column Name: NM                             Datatype: Varchar2
    4 - Binding in values
    5 - Executing the query
    6,7 and 8 Fetching Data:-
    1     ,1     ,Fred
    1     ,2     ,Bloggs
    2     ,1     ,Scott
    2     ,2     ,Smith
    3     ,1     ,Jim
    3     ,2     ,Jones
    1     ,3     ,Freddy
    1     ,4     ,Fud
    9 - Closing the cursor
    PL/SQL procedure successfully completed.
    So, what's really the point in knowing when SQL Projection occurs in a query?
    Well, we get many questions asking "How do I convert rows to columns?" (otherwise known as a pivot) or questions like "How can I get the data back from a dynamic query with different columns?"
    Let's look at a regular pivot. We would normally do something like...
    SQL> select id
      2        ,max(decode(rn,1,nm)) as nm_1
      3        ,max(decode(rn,2,nm)) as nm_2
      4  from proj_test
      5  group by id
      6  /
            ID NM_1   NM_2
             1 Fred   Bloggs
             2 Scott  Smith
             3 Jim    Jones
    (or, in 11g, use the new PIVOT statement)
    but many of these questioners don't understand it when they say their issue is that, they have an unknown number of rows and don't know how many columns it will have, and they are told that you can't do that in a single SQL statement. e.g.
    SQL> insert into proj_test (id, rn, nm) values (1,3,'Freddy');
    1 row created.
    SQL> select id
      2        ,max(decode(rn,1,nm)) as nm_1
      3        ,max(decode(rn,2,nm)) as nm_2
      4  from proj_test
      5  group by id
      6  /
            ID NM_1   NM_2
             1 Fred   Bloggs
             2 Scott  Smith
             3 Jim    Jones
    ... it's not giving us this 3rd entry as a new column and we can only get that by writing the expected columns into the query, but then what if more columns are added after that etc.
    If we look back at the steps of a cursor we see again that the description and projection of what columns are returned by a query happens before any data is fetched back.
    Because of this, it's not possible to have the query return back a number of columns that are based on the data itself, as no data has been fetched at the point the projection is required.
    So, what is the answer to getting an unknown number of columns in the output?
    1) The most obvious answer is, don't use SQL to try and pivot your data. Pivoting of data is more of a reporting requirement and most reporting tools include the ability to pivot data either as part of the initial report generation or on-the-fly at the users request. The main point about using the reporting tools is that they query the data first and then the pivoting is simply a case of manipulating the display of those results, which can be dynamically determined by the reporting tool based on what data there is.
    2) The other answer is to write dynamic SQL. Because you're not going to know the number of columns, this isn't just a simple case of building up a SQL query as a string and passing it to the EXECUTE IMMEDIATE command within PL/SQL, because you won't have a suitable structure to read the results back into as those structures must have a known number of variables for each of the columns at design time, before the data is know. As such, inside PL/SQL code, you would have to use the DBMS_SQL package, just like in the code above that showed the workings of a cursor, as the columns there are referenced by position rather than name, and you have to deal with each column seperately. What you do with each column is up to you... store them in an array/collection, process them as you get them, or whatever. They key thing though with doing this is that, just like the reporting tools, you would need to process the data first to determine what your SQL projection is, before you execute the query to fetch the data in the format you want e.g.
    create or replace procedure dyn_pivot is
      v_sql varchar2(32767);
      -- cursor to find out the maximum number of projected columns required
      -- by looking at the data
      cursor cur_proj_test is
        select distinct rn
        from   proj_test
        order by rn;
    begin
      v_sql := 'select id';
      for i in cur_proj_test
      loop
        -- dynamically add to the projection for the query
        v_sql := v_sql||',max(decode(rn,'||i.rn||',nm)) as nm_'||i.rn;
      end loop;
      v_sql := v_sql||' from proj_test group by id order by id';
      dbms_output.put_line('Dynamic SQL Statement:-'||chr(10)||v_sql||chr(10)||chr(10));
      -- call our DBMS_SQL procedure to process the query with it's dynamic projection
      process_cursor(v_sql);
    end;
    SQL> exec dyn_pivot;
    Dynamic SQL Statement:-
    select id,max(decode(rn,1,nm)) as nm_1,max(decode(rn,2,nm)) as nm_2,max(decode(rn,3,nm)) as nm_3 from proj_test group by id order by id
    1 - Opening Cursor
    2 - Parsing the query
    3 - Describing the query
    3a - SQL Projection:-
    Column Name: ID                             Datatype: Number
    Column Name: NM_1                           Datatype: Varchar2
    Column Name: NM_2                           Datatype: Varchar2
    Column Name: NM_3                           Datatype: Varchar2
    4 - Binding in values
    5 - Executing the query
    6,7 and 8 Fetching Data:-
    1     ,Fred                ,Bloggs              ,Freddy
    2     ,Scott               ,Smith               ,
    3     ,Jim                 ,Jones               ,
    9 - Closing the cursor
    PL/SQL procedure successfully completed.
    ... and if more data is added ...
    SQL> insert into proj_test (id, rn, nm) values (1,4,'Fud');
    1 row created.
    SQL> exec dyn_pivot;
    Dynamic SQL Statement:-
    select id,max(decode(rn,1,nm)) as nm_1,max(decode(rn,2,nm)) as nm_2,max(decode(rn,3,nm)) as nm_3,max(decode(rn,4,nm)) as nm_4 from proj_test group by id order by id
    1 - Opening Cursor
    2 - Parsing the query
    3 - Describing the query
    3a - SQL Projection:-
    Column Name: ID                             Datatype: Number
    Column Name: NM_1                           Datatype: Varchar2
    Column Name: NM_2                           Datatype: Varchar2
    Column Name: NM_3                           Datatype: Varchar2
    Column Name: NM_4                           Datatype: Varchar2
    4 - Binding in values
    5 - Executing the query
    6,7 and 8 Fetching Data:-
    1     ,Fred                ,Bloggs              ,Freddy              ,Fud
    2     ,Scott               ,Smith               ,                    ,
    3     ,Jim                 ,Jones               ,                    ,
    9 - Closing the cursor
    PL/SQL procedure successfully completed.
    Of course there are other methods, using dynamically generated scripts etc. (see Re: 4. How do I convert rows to columns?), but the above simply demonstrates that:-
    a) having a dynamic projection requires two passes of the data; one to dynamically generate the query and another to actually query the data,
    b) it is not a good idea in most cases as it requires code to handle the results dynamically rather than being able to simply query directly into a known structure or variables, and
    c) a simple SQL statement cannot have a dynamic projection.
    Most importantly, dynamic queries prevent validation of your queries at the time your code is compiled, so the compiler can't check that the column names are correct or the tables names, or that the actual syntax of the generated query is correct. This only happens at run-time, and depending upon the complexity of your dynamic query, some problems may only be experienced under certain conditions. In effect you are writing queries that are harder to validate and could potentially have bugs in them that would are not apparent until they get to a run time environment. Dynamic queries can also introduce the possibility of SQL injection (a potential security risk), especially if a user is supplying a string value into the query from an interface.
    To summarise:-
    The projection of an SQL statement must be known by the SQL engine before any data is fetched, so don't expect SQL to magically create columns on-the-fly based on the data it's retrieving back; and, if you find yourself thinking of using dynamic SQL to get around it, just take a step back and see if what you are trying to achieve may be better done elsewhere, such as in a reporting tool or the user interface.
    Other articles in the PL/SQL 101 series:-
    PL/SQL 101 : Understanding Ref Cursors
    PL/SQL 101 : Exception Handling

    excellent article. However there is one thing which is slightly erroneous. You don't need a type to be declared in the database to fetch the data, but you do need to declare a type;
    here is one of my unit test scripts that does just that.
    DECLARE
    PN_CARDAPPL_ID NUMBER;
    v_Return Cci_Standard.ref_cursor;
    type getcardapplattrval_recordtype
    Is record
    (cardappl_id ci_cardapplattrvalue.cardappl_ID%TYPE,
    tag ci_cardapplattrvalue.tag%TYPE,
    value ci_cardapplattrvalue.value%TYPE
    getcardapplattrvalue_record getcardapplattrval_recordtype;
    BEGIN
    PN_CARDAPPL_ID := 1; --value must be supplied
    v_Return := CCI_GETCUSTCARD.GETCARDAPPLATTRVALUE(
    PN_CARDAPPL_ID => PN_CARDAPPL_ID
    loop
    fetch v_return
    into getcardapplattrvalue_record;
    dbms_output.put_line('Cardappl_id=>'||getcardapplattrvalue_record.cardappl_id);
    dbms_output.put_line('Tag =>'||getcardapplattrvalue_record.tag);
    dbms_output.put_line('Value =>'||getcardapplattrvalue_record.value);
    exit when v_Return%NOTFOUND;
    end loop;
    END;

  • Issue installing SQL Server 2012(with SP1) on windows 7 Error result: -2067529718

    When running the installation, it suddenly stops without any error popup.
    I ran the installation from the command prompt and this is what I get:
    An error occurred during the SQL Server 2012 Setup operation.
    Error result: -2067529718
    Result facility code: 1220
    Result error code: 10
    I recently uninstalled SQL Server 2012 SP1, because the SQL server service was not starting up, it would just hang in there with no response. I tried many things to get that issue fixed, finally I decided to uninstall and start fresh but I couldn't since
    I was getting an error stating that I needed Windows 7 SP1(which was already installed). I downloaded the
    microsoft fix it tool which fixed the issue.
    On the control panel/Programs and features, I still see that SQL Server 2012 is listed if I click to uninstall again it tells me:
    An error occurred while trying to uninstall Microsoft SQL Server 2012. it may have already been uninstalled. do you want to remove Microsoft SQL Server 2012 from the Programs and features list?
    if I click yes, eventually it will be listed again.
    currently I'm not able to install SQL server again and besides the info provided there are not error messages (I also copied the installation files to disk).
    UPDATE: it appears that the sql server was not completely removed from the system, I still see listed the SQL server(SQLExpress) and server browser services on the SQL server configuration manager.
    Thanks.

    1) First Reboot your server and try again for the installation.
    2) If first fail again than Take a backup of Registry first for safe side and then delete the entries of SQL server 2012 if found take a reboot again and try to install again.
    One more thing if installation created any file then move/Delete that also before installation.

  • Help with listing records from two tables

    Hi: I have two tables joined by the first field. The field is primary key in first table. Need help listing records from both tables with each record one line/record.
    create table EVENTS (
    event_key varchar2(64) primary key,
    event_description varchar2(64),
    create_time int
    create table EVENT_UPDATES (
    event_key varchar2(64) NOT NULL ,
    update_description varchar2(64),
    update_time int
    insert into EVENTS values('Event1', 'This is event1', 1);
    insert into EVENT_UPDATES values('Event1', 'Ticket created', 3);
    insert into EVENT_UPDATES values('Event1', 'Event cleared', 10);
    insert into EVENTS values('Event2', 'This is event2', 4);
    insert into EVENT_UPDATES values('Event2', 'Ticket created', 6);
    insert into EVENT_UPDATES values('Event2', 'Event cleared', 8);I want to print each record in EVENTS table as one line and corresponding records in EVENT_UPDATES as one line/record like this
    Event1   1     This is event1
                3     Ticket created
                10   Event cleared
    Event2   4     This is event2
                6     Ticket created
                8     Event clearedTIA
    Ravi

    select  case weight
              when 1 then event_key
            end key,
            time_val,
            description
      from  (
              select  event_key,
                      create_time time_val,
                      event_description description,
                      1 weight
                from  events
             union all
              select  event_key,
                      update_time,
                      update_description,
                      2 weight
                from  event_updates
      order by event_key,
               weight
    KEY          TIME_VAL DESCRIPTION
    Event1              1 This is event1
                        3 Ticket created
                       10 Event cleared
    Event2              4 This is event2
                        6 Ticket created
                        8 Event cleared
    6 rows selected.
    SQL> SY.

  • SQL IN clause with Bind parameter?

    Hi
    I have a simple task that hasn't been so simple to figure out. I want to allow a user to search for one or more comma-separated values in a simple JClient ADF app. Is there a way to use a SQL IN clause with a single bind variable? e.g. SELECT TITLE FROM CITATION WHERE ID IN (:0)
    When I pass a single value it works fine but a comma separated list doesn't.
    Thanks
    John

    Update: I wanted to combine the techniques found in two of Steve Muench's articles -
    1) Providing Default Values for View Object Bind Variables (so I could display an ADF-bound JPanel with defaults)
    http://radio.weblogs.com/0118231/stories/2004/10/07/providingDefaultValuesForViewObjectBindVariables.html
    2) Array of String Domain Example (so a user could enter one or more comma-separated values into a text box for DB searches)
    http://radio.weblogs.com/0118231/stories/2004/09/23/notYetDocumentedAdfSampleApplications.html
    I learned some helpful stuff about the framework but spent lots of time banging my head against the wall because the two examples wouldn't work when directly combined. To best understand this, be sure to study Steve's examples above.
    In example 1 Steve passes an array of objects (Object[] DEFAULT_VALUES) to the ViewObject's where clause using the setWhereClauseParams(Object[] values). However, in example 2 he creates an Oracle Array which contains an Oracle ArrayDescriptor, Connection, and array of values to pass to the "IN" bind variable. Also, example 1 allows for multiple bind vars to be included whereas example 2 allows for an array of data to be passed to a single bind var. Even though my code provides an array to a single bind var (per ex. 2) it should still allow for the passage of multiple bind vars with minimal code modification.
    Code from Steve's example 1 was copied into my EmpView ViewObject but certain modifications were necessary:
    1) Change the ViewObject's DEFAULT_VALUES from Object[] to String[].
    2) Modify the executeQueryForCollection() method in the ViewObject to call a function which will set the bind variables as Oracle Arrays (effectively converting the "params" data type from that of String[] to Oracle Array[])
    3) Create a setManagerID(String[]) method in the EmpView object and expose it to the client.
    (there are a number of others so it's best for you to go through the code and compare)
    I finally got it working so I'm attaching the code, however beware - I'm new to this so there may be other, better ways to go about it. Also, there are no framework bind vars so that section of code is never executed...it compiles but may fail at run time.
    In order for this to work I suggest you use JDev to re-create the EmpView and Panel1 objects. This will ensure that the necessary ADF framework components are generated. Once complete, then copy in the code provided.
    *File: EmpViewImpl.java
    *Created as Read-Only access view object with the
    *query:
    *select manager_id, last_name from hr.employees
    *where manager_id IN
    *(select * from TABLE(CAST(:0 as TABLE_OF_VARCHAR)))
    *order by manager_id
    package model;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import oracle.jbo.domain.Array;
    import oracle.jbo.server.ViewObjectImpl;
    import oracle.sql.ArrayDescriptor;
    // --- File generated by Oracle ADF Business Components Design Time.
    // --- Custom code may be added to this class.
    // --- Warning: Do not modify method signatures of generated methods.
    public class EmpViewImpl extends ViewObjectImpl implements model.common.EmpView
    private ArrayDescriptor descriptor;
    private final static String[] DEFAULT_VALUES_STRING = new String[]{"100"};
    private final static int NUM_DEFAULT_VALUES = DEFAULT_VALUES_STRING.length;
    * This is the default constructor (do not remove)
    public EmpViewImpl()
    protected void executeQueryForCollection (Object qc, Object[] params, int numUserParams)
    Object pars[] = null;
    // Bind default variables only if none have been provided by the user
    if (numUserParams == 0)
    numUserParams = NUM_DEFAULT_VALUES;
    int numFwkSuppliedBindVals = (params != null) ? params.length : 0;
    if (numFwkSuppliedBindVals > 0)
    // Allocate a new Object[] array with enough room for both user- and framework-supplied vars
    Object[] newBinds = new Object[numFwkSuppliedBindVals + numUserParams];
    // Copy the framework-supplied bind variables into a new Object[] array
    // leaving enough slots at the beginning for the user-supplied vars
    System.arraycopy(params, 0, newBinds, numUserParams, numFwkSuppliedBindVals);
    // Now copy in the user-supplied vars to the beginning of the array
    System.arraycopy(DEFAULT_VALUES_STRING, 0, newBinds, 0, numUserParams);
    params = newBinds;
    } else
    params = DEFAULT_VALUES_STRING;
    // We have to call this method to convert the default values into the proper Oracle Array expected by the query.
    // If you set a debugger breakpoint at this line you can see that the "params" data type changes from String[] to Object[]
    setWhereClauseParamsToDefaultValues();
    // Now retrieve the params of the new data type
    params = this.getWhereClauseParams();
    super.executeQueryForCollection(qc, params, numUserParams);
    private void setWhereClauseParamsToDefaultValues()
    this.setManagerID(DEFAULT_VALUES_STRING);
    private Connection getCurrentConnection() throws SQLException
    // Create a bogus statement so that we can access our current connection
    // JBD note - Does this get run each time??
    PreparedStatement st = getDBTransaction().createPreparedStatement("commit", 1);
    Connection conn = st.getConnection();
    st.close();
    return conn;
    private synchronized void setupDescriptor(Connection conn) throws SQLException
    descriptor = new ArrayDescriptor("TABLE_OF_VARCHAR", conn);
    * setManagerID
    * Exposed to client to accept an array of values (presumably passed in by user-entry into text box
    * @param aryMan
    public void setManagerID(String[] aryMan)
    Array arr = null;
    try
    // Find the connection
    Connection conn = getCurrentConnection();
    //Create the ArrayDescriptor by looking for our custom data type in our connected DB
    if(descriptor == null)
    setupDescriptor(conn);
    // Create the Oracle Array by passing in the descriptor, connection, and object array of data
    arr = new Array(descriptor, conn, aryMan);
    } catch (SQLException se)
    System.out.println("SQL Exception: " + se.getMessage());
    // Now we can set the WHERE clause parameter bind variable (index = 0) to the Oracle Array
    if (arr != null) setWhereClauseParam(0, arr);
    * FILE: Panel1.java
    * Created as an empty panel. Then a JTextField, a
    * JButton, and an EmpView1 table were dragged on.
    * A custom actionPerformed method was created for the
    * JButton which grabs the data from the text box.
    * The user can enter either a single manager id or
    * multiple, comma-separated ids.
    * All code in this class was created by JDev except
    * for the Jbutton action
    package view;
    import java.awt.*;
    import javax.swing.*;
    import model.common.*;
    import oracle.jbo.ApplicationModule;
    import oracle.jbo.SQLStmtException;
    import oracle.jbo.uicli.jui.*;
    import oracle.jbo.uicli.controls.*;
    import oracle.jbo.uicli.binding.*;
    import oracle.jdeveloper.layout.*;
    import oracle.adf.model.*;
    import oracle.adf.model.binding.*;
    import java.util.ArrayList;
    import oracle.jdeveloper.layout.VerticalFlowLayout;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTable;
    import javax.swing.table.TableModel;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class Panel1 extends JPanel implements JUPanel
    * NOTE: You need to have previous created the Oracle8 type named
    * ==== TABLE_OF_VARCHAR by doing the following at the SQL*Plus
    * command prompt:
    * create type table_of_varchar as table of varchar2(20)
    // Panel binding definition used by design time
    private JUPanelBinding panelBinding = new JUPanelBinding("Panel1UIModel");
    private VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
    private JTextField jTextField1 = new JTextField();
    private JButton jButton1 = new JButton();
    private JTable jTable1 = new JTable();
    * The default constructor for panel
    public Panel1()
    * the JbInit method
    public void jbInit() throws Exception
    this.setLayout(verticalFlowLayout1);
    jTextField1.setText("jTextField1");
    jButton1.setText("jButton1");
    jButton1.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e)
    jButton1_actionPerformed(e);
    this.add(jTextField1, null);
    this.add(jButton1, null);
    this.add(jTable1, null);
    jTable1.setModel((TableModel)panelBinding.bindUIControl("EmpView1", jTable1));
    public static void main(String [] args)
    try
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    catch(Exception exemp)
    exemp.printStackTrace();
    Panel1 panel = new Panel1();
    panel.setBindingContext(JUTestFrame.startTestFrame("DataBindings.cpx", "null", panel, panel.getPanelBinding(), new Dimension(400, 300)));
    panel.revalidate();
    * JUPanel implementation
    public JUPanelBinding getPanelBinding()
    return panelBinding;
    private void unRegisterProjectGlobalVariables(BindingContext bindCtx)
    JUUtil.unRegisterNavigationBarInterface(panelBinding, bindCtx);
    private void registerProjectGlobalVariables(BindingContext bindCtx)
    JUUtil.registerNavigationBarInterface(panelBinding, bindCtx);
    public void setBindingContext(BindingContext bindCtx)
    if (panelBinding.getPanel() == null)
    panelBinding = panelBinding.setup(bindCtx, this);
    registerProjectGlobalVariables(bindCtx);
    panelBinding.refreshControl();
    try
    jbInit();
    panelBinding.refreshControl();
    catch(Exception ex)
    panelBinding.reportException(ex);
    private void jButton1_actionPerformed(ActionEvent e)
    // Get the user-supplied values
    String txt = jTextField1.getText();
    String[] mIds = txt.split(",");
    // Now trim
    for (int i=0; i<mIds.length; i++)
    mIds[i] = mIds.trim();
    ApplicationModule am = (ApplicationModule)panelBinding.getDataControl().getDataProvider();
    EmpView vo = (EmpView)am.findViewObject("EmpView1");
    vo.setManagerID(mIds);
    try
    vo.executeQuery();
    } catch (SQLStmtException s)
    System.out.println("Query failed: " + s.getMessage());

  • SQL*Loader issue with WHEN command

    Environment: R12.1.2
    We have a file coming in from a bank that needs to be loaded into a custom table using SQL*Loader.
    The file has multiple record formats. Each record in the file starts with a "record type", which defines the format.
    For simplicity, let me say that there is a record type of "H" with the header format, and another record type "D" has a detail record format. An "H" record may be followed by multiple "D" records until the next "H" record is encountered. Unfortunately, there is no common key, like say "Vendor Number" in both the "H" and "D" records to establish a relationship. So the plan was to use a Oracle sequence or SQL*Loader sequence to get a sequence loaded into the table as the file is being loaded. Then if consecutive "H" records had a sequence value of 100 and 112, we would know that the "D" records for the "H" 100 record are all the records with sequence value of 101 through 111.
    The issue occurs as we have to use the WHEN command in the control file to direct a certain record type to specific columns of the table. Based on the populated sequence values, with the WHEN command, it seems that all the "H" records get loaded first followed by the "D" records. The sequence becomes of no use and we cannot establish a link between the "H" and "D" records. The alternative is to not use WHEN with the sequence, but load the file into generic column names which provides for less understanding in the application.
    Is there a way (command feature) to ensure that SQL*Loader loads the records sequentially while using WHEN?
    Thanks
    Satish

    I used RECNUM parameter instead of sequence and it worked fine

  • SQL Statement ignored performing List of Values query

    Hi, New user just learning the basics. I have created a simple table PERSON with columns, ID, firstname, lastname, phone, city, State_ID
    Then clicked create Lookup table - State_Lookup with columns State_ID and State_Name.
    I create a page, include all columns from PERSON. For State the field is a select list that should do a lookup form the STATE_LOOKUP table. (I have entered 4 states in the table)
    I am getting the following error however:
    Error: ORA-06550: line 1, column 14: PL/SQL: ORA-00904: "STATE_ID": invalid identifier ORA-06550: line 1, column 7: PL/SQL: SQL Statement ignored performing List of Values query: "select STATE_ID d, STATE_ID v from STATE_ID_LOOKUP order by 1".
    I have not entered any sql, just selected all of my options using defaults and dropdowns. What is causing the error and what do I need to change?
    Thanks

    Okay, learned something: The database link name used, must not contain a dash. The DB_DOMAIN is appended automatically when you create a DB link, so if IT contains a dash, the db link name does as well. Check DBA_DB_LINKS to make sure you don't hit this well-hidden feature.
    Regards
    Martin Klier
    [http://www.usn-it.de|http://www.usn-it.de]

  • OPENING CURSOR WITH TABLE NAME AS PERAMETER

    hi all,
    here i have a problem. please help me.
    create or replace procedure MAX_ID
    (COLUMN_NAME IN VARCHAR2,
    TABLE_NAME IN VARCHAR2) IS
    CURSOR MAX_CURSOR IS SELECT COLUMN_NAME FROM TABLE_NAME;
    begin
    END;
    in the above procedure table name and column name are in parameters. I have to declare cursor with the in parameter values. How to solve this problem. Give me solution as soon as possible. Waiting for your valuable suggestions.
    with regards,
    vali

    Hi,
    You will need to use dynamic SQL to be able to do that. This one returns a single row but you may need to work more on this to achieve the results which you are looking for based on your requirement.
    PROCEDURE get_data_dynamically_pr(pv_sqlstr VARCHAR2,pn_count IN OUT NUMBER) IS
    ln_cursor NUMBER;
    ln_rows NUMBER(5);
    BEGIN
    /* Retrieve the count from a table we passed */
    ln_cursor := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE(ln_cursor, pv_sqlstr,DBMS_SQL.NATIVE);
    DBMS_SQL.DEFINE_COLUMN(ln_cursor, 1, pn_count);
    ln_rows := DBMS_SQL.EXECUTE(ln_cursor);
    IF DBMS_SQL.FETCH_ROWS(ln_cursor) = 0 then
    RETURN;
    END IF;
    DBMS_SQL.COLUMN_VALUE(ln_cursor, 1, pn_count);
    DBMS_SQL.CLOSE_CURSOR(ln_cursor);
    EXCEPTION
    WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR (-20400,SQLERRM||'Exception raised in dynamic sql proc..');
    END;
    I hope this might be useful to you.

  • For update cursor with nowait

    I have a 'for update' cursor defined with 'NOWAIT'. When soem of teh records that are supposed to be fetched by teh cursor are locked by another user for update..teh pl?SQL script returns
    "ORA-00054: resource busy and acquire with NOWAIT specified" error.
    If I declare teh cursor with out 'FOR UPDATE' or did NOT put 'NOWAIT' clause, teh script hangs waiting for teh records to be unlocked.
    If the user opens a record in the front end (web app) and does not close it.. i can not run the script. Is theer any way to ignore those records that are locked by other users and query only ones that are available as part of the select statement in the cursor.

    Optimistic locking implies, essentailly, that you never lock the row. Instead, if you want to update the row, you check all the other columns of the row to see whether they have changed. In other words, you'd do a straight SELECT here and then when you went to UPDATE the data, you'd do
    UPDATE <<change some column>>
    WHERE col1=<<old col1 value>>
       AND col2=<<old col2 value>>
       AND ...If the update changed 1 row, you're set. If it changed 0 rows, someone had changed the underlying row since you SELECTED it, so you'd have to handle that condition. If it returned an error indicating that someone else had locked the row, you could handle that situation as well. If you just continue on, however, be sure that you know how to identify that this row wasn't updated so you can try to do the update the next time (assuming that makes sense).
    If all your applications take the optimistic locking approach, you're pretty much guaranteed that no one else will have teh row locked, so you don't have to handle that state nearly as robustly.
    Justin
    Distributed Database Consulting, Inc.
    www.ddbcinc.com

  • UI not getting change update when working with LIST and INotifyPropertyChanged

    i was trying to know two way data binding. i have simple car class which extend INotifyPropertyChanged for notify the change to update UI. bind List object to few textboxes and notice when one textbox value change then other textbox value not updated. all
    textboxes bind to same property. so one's value change should propagate to other textboxes.
    this is my code
    public class Car : INotifyPropertyChanged
    private string _make;
    private string _model;
    private int _year;
    public event PropertyChangedEventHandler PropertyChanged;
    public Car(string make, string model, int year)
    _make = make;
    _model = model;
    _year = year;
    public string Make
    get { return _make; }
    set
    _make = value;
    this.NotifyPropertyChanged("Make");
    public string Model
    get { return _model; }
    set
    _model = value;
    this.NotifyPropertyChanged("Model");
    public int Year
    get { return _year; }
    set
    _year = value;
    this.NotifyPropertyChanged("Year");
    private void NotifyPropertyChanged(string name)
    if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs(name));
    This way i bind
    Car carTest;
    private void Form1_Load(object sender, EventArgs e)
    carTest = new Car("Ford", "Mustang", 1967);
    List<Car> ol = new List<Car>();
    ol.Add(carTest);
    this.textBox1.DataBindings.Add("Text", ol, "Make", true, DataSourceUpdateMode.OnPropertyChanged);
    this.textBox2.DataBindings.Add("Text", ol, "Make", true, DataSourceUpdateMode.OnPropertyChanged);
    this.textBox3.DataBindings.Add("Text", ol, "Make");
    when run the code then Ford was showing as make name but when change value in any textbox then that change is not shown in other textboxes.
    the moment i change this line List<Car> ol = new List<Car>(); to
    BindingList<Car> ol = new BindingList<Car>(); then code started to work fine.
    My Question
    1) what is the difference between List and BindingList class ?
    2) can't we use List<> for my situation instead of BindingList
    3)
    this.textBox2.DataBindings.Add("Text", ol, "Make", true, DataSourceUpdateMode.OnPropertyChanged);
    this.textBox3.DataBindings.Add("Text", ol, "Make");
    see the above code and tell me what is the advantage of using DataSourceUpdateMode.OnPropertyChanged because i have seen if we do not use this code
    DataSourceUpdateMode.OnPropertyChanged then also data change is propagated to other textbox when cursor focus change.

    I would have thought that'd work with List<t>, in fact I think there must be something wrong in your code there.  I can't spot it though.
    I recommend use of ObservableCollection rather than BindingList.
    The default on bindings is that changes are propagated from the target ( view ) to source ( vm ) when the control loses focus.
    If you want to do the equivalent to a keydown event handler in a viewmodel then onpropertychanged is the way to go.
    You want to avoid creating bindings in code unless you really really have to, it's way easier to put them in xaml.
    Even if your ui is dynamic, you can build xaml and use that to create the ui objects:
    http://social.technet.microsoft.com/wiki/contents/articles/28797.aspx
    The difference between BindingList and List is, literally, iBindingList.
    See
    https://msdn.microsoft.com/en-us/library/system.componentmodel.ibindinglist%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
    What probably isn't very obvious is that BindingList fires an event - iirc  itemchanged when properties on objects in it change.
    Maybe you did something wrong in your implementation of inotifypropertychanged.  I must admit, I can't see anything there though.
    You don't really need those magic strings since .net4.5 and you also don't need to explicitly implement inotifypropertychanged you could use:
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged([CallerMemberName] String propertyName = "")
    if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    As used in this:
    https://gallery.technet.microsoft.com/WPF-Dynamic-Fonts-ad3741ca
    If you try that sample you can have:
    public class FontDetails : INotifyPropertyChanged
    or
    public class FontDetails
    And you can see it still notifies change successfully to both windows.
    Most wpf devs will use observablecollection rather than List or bindinglist.
    Observablecollection notifies addition or removal of entries.  It can be used to notify an entry has changed, but does not detect change of property.  You would have to raise the event in code if you want to tell it an item changed.
    Hope that helps.
    Technet articles: Uneventful MVVM;
    All my Technet Articles

Maybe you are looking for