Discretionary updates and opening a cursor from passed parameters

I am trying to pass a list of item IDs into a procedure, and have the procedure loop through a cursor of those items, working out the length of columns and taking appropriate action when the length of the data threatens to exceed the length of the column.
For example:
drop table t;
create table t (itemid number, itemdesc varchar2(10));
insert into t values (1,'cat,rat');
insert into t values (2,'dog');
commit;If I attempt to append ',cow' to the end of item 1, that would error. But it would be OK for item 2. However, if I try to add ',vestibule' to either of the items, both should error.
So I have this procedure:
create or replace PROCEDURE item_change (p_item_list IN VARCHAR2,
                                  p_newword IN VARCHAR2)
IS
  v_addlength      number;
  v_maxlength   number;
BEGIN
  v_addlength := length(p_newword);
  execute immediate 'select max(length(itemdesc)) from t
                     where itemid in
                       (select * from table(sys.odcinumberlist('||p_item_list||')))' into v_maxlength ;
  if v_maxlength+v_addlength>10 then
    raise_application_error(-20001,'Too Long!');
  else
    execute immediate 'update t set itemdesc=itemdesc||'''||p_newword||''' where itemid in  (select * from table(sys.odcinumberlist('||p_item_list||')))';
    commit;
  end if;
END;
/As you can see, I workout what the maximum length of all items descriptions are for all possible items, and then providing adding the length of the new addition would NOT exceed the column length, I go ahead and append the new word to the description. It works fine, provided you stick to one case at a time:
SQL> exec item_change('1',',cow');
BEGIN item_change('1',',cow'); END;
ERROR at line 1:
ORA-20001: Too Long!
ORA-06512: at "SYS.ITEM_CHANGE", line 15
ORA-06512: at line 1
SQL> exec item_change('2',',cow');
PL/SQL procedure successfully completed.
SQL> select * from t;
    ITEMID ITEMDESC
      1 cat,rat
      2 dog,cowBut now the twist: if someone asks to modify both items, and the request is only valid for one of them, I nevertheless want to update the one that it's OK for, and skip the one that it isn't going to work. The way I thought to do this was, instead of the bit where I currently raise the application error, start looping through a cursor of itemIDs, evaluating each one's length in turn, and doing the update if the length computation is OK, and skipping it if not. In pseudo code (because this is what I can't get right!) I was thinking this:
create or replace PROCEDURE item_change (p_item_list IN VARCHAR2,
                                  p_newword IN VARCHAR2)
IS
  v_addlength      number;
  v_maxlength   number;
  v_wordlength     number;
  cursor c1 is select itemid, length(itemdesc) from t where itemid in (select * from table(sys.odcinumberlist(p_item_list)));
BEGIN
  v_addlength := length(p_newword);
  execute immediate 'select max(length(itemdesc)) from t
                     where itemid in
                       (select * from table(sys.odcinumberlist('||p_item_list||')))' into v_maxlength ;
  if v_maxlength+v_addlength>10 then
    for x in c1 loop
      select length(itemdesc) into v_wordlength from t where itemid=x.itemid;
      if v_wordlength+v_addlength <= 10 then
        --do the update!
      end if;
    end loop
  else
    execute immediate 'update t set itemdesc=itemdesc||'''||p_newword||''' where itemid in  (select * from table(sys.odcinumberlist('||p_item_list||')))';
    commit;
  end if;
END;
/Of course, this doesn't actually work, because the syntax for declaring the cursor is all wrong, which is what I'm really asking about.
In reality, I'm dealing with a table of millions of records, and a user might have selected hundreds of thousands of them to add words to. I can't afford to not update any of them just because one of them would breach its maximum possible description length. So I have to have a way to update those that can be updated and not attempt to update those where the update would cause things to go wrong. And I'm not allowed to just truncate the descriptions or the additions to fit: the things either have to be updated the way the user has specified, or not. (*Added later*: and I have to have a way of listing/reporting on which specific items are not updated if they are skipped because of length reasons).
Can anyone suggest either new syntax for the above code so that it does what I conceptually want it to do? Or, alternatively, a different sort of approach entirely?
Success would be to have table T in this example have its original data (1,'cat,rat' and 2,'dog'), and for me to issue the command:
exec item_change('1,2',',cow');...and have it say, 'Procedure Successfully completed', with the result that a select from t would show item 1 as 'cat,rat' and item 2 as 'dog,cow'.
Thank you in advance.

Just to be sure I understand this. You will get a list of items, which identify individual records, possibly in the 100's of thousands. For each of these items, you need to add a single word to the description column, recording which ones could not be updated.
Would an individual item in p_item_list ever result in multiple rows being updated, or is each item unique in the table?
Will p_newword always be a single word, or could there be multiple words e.g. 'cat, hat'? If there could be multiple words, is it an all or nothing update, or could you add cat but not hat for one item, both for another and neither for a third? If the words were of different lengths and the second word would fit but the first not would you add the second but not the first?
First, I would completely support Karthik's suggestion to use an array instead of a string list if you can get the caller to do that, if not, I would transform the list into an array in the procedure. The code that Karthic showed that generates the cursor loop could be used to generate the array. In fact, this potential solution more or less depends on that.
In the simplest case, that is, that the update is to be treated as single word, that is all or nothing, then I would be tempted to try something along the lines of this.
CREATE PROCEDURE item_change (p_item_list IN VARCHAR2,
                                      p_newword IN VARCHAR2)  IS
   item_list_tbl is TABLE OF VARCHAR2(50);
   l_item_list  item_list_tbl;
   l_num_errors NUMBER
   dml_errors EXCEPTION;
   PRAGMA EXCEPTION_INIT(dml_errors, -24381);
BEGIN
   SELECT TRIM(SUBSTR(str,INSTR(str, ',', 1,level)+1, INSTR(str, ',', 1,level+1)-INSTR(str, ',', 1,level)-1)) item
   BULK COLLECT INTO l_item_list
   FROM (SELECT ','||p_item_list||',' str FROM DUAL)
         CONNECT BY level <= LENGTH(str)-LENGTH(REPLACE(str,','))-1;
   FORALL i IN l_item_list.First .. l_item_list.Last SAVE EXCEPTIONS
      UPDATE t
      SET itemdesc = itemdesc||p_newword;
EXCEPTION
   WHEN dml_errors THEN
      l_num_errors := SQL%BULK_EXCEPTIONS.COUNT;
      FOR i IN 1 .. l_num_errors LOOP
         DEBMS_OUTPUT.Put_Line('Errors occurred for '||l_item_list(SQL%BULK_EXCEPTIONS(i).ERROR_INDEX));
      END LOOP;
END;Note that if your item list could really be 100's of thousands of items, or at least affect that many rows in the update, then you probably want to think about doing the FORALL UPDATE/BULK_EXCEPTIONS processing in a loop updating a "reasonable" number of items at a time.
Depending on what you need in terms of error reporting. I would use possible use a log table (perhaps a GTT) then query off that to report back to the user, or perhaps a collection to return to the user. Note that the collection approach could require significant memory, so if you are really expecting to update 100's of thousands of rows I would definitely go for a table of some kind.
You can see more about bulk collect [in the documentation|http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/tuning.htm#sthref2176]
Edited by: John Spencer on Aug 21, 2009 10:44 AM
Added link

Similar Messages

  • I have recently updated my CC programs to the latest version and now all of my files wont open by default into their respective programs, only if I open the program and go to file open and open the file from there. How can I fix this?

    I have recently updated my CC programs to the latest version (CC2014) and now all of my files wont open by default into their respective programs, only if I open the program and go to file>open and open the file from there. How can I fix this?
    I have tried 'Open with' and the version of the program now installed on my computer isn't even suggested as an option and when I browse for it, the file wont open with it anyway

    On Windows (don't know about Mac), the latest version will always take over the file association, and become the default for indd files. It's impossible to change it.
    But there is a plugin for ID that makes this possible. Never tried it myself.
    https://www.rorohiko.com/wordpress/downloads/lightning-brain-soxy/

  • Firefox will not start and open a website from a Thunderbird email link. It will go to a link from Thunderbird once Firefox is open, but will not initially start Firefox and proceed to open the Thunderbird email link.

    Firefox will not ''start'' and open a website from a Thunderbird email link. A link from Thunderbird will open once Firefox is ''already'' started, but will not initially start Firefox and proceed to open the Thunderbird email link.

    I also have this problem. I cannot open links from a discussion board I frequent nor can I open links from my email. Also after opening my email, I cannot go to any other sites except my home page. I can then go no where else.
    When I close Firefox and try to reopen it, it does nothing! I have to bring up the Task Manager and end the Firefox process before I can get back in.
    To the best of my knowledge, this problem started with the new update to Ver. 3.6.9. I am running Windows XP Media Center 2005

  • TS1702 Keynotes got hung up in updating and kept everything else from updating. I deleted it and now it doesn't show as an app I had purchased, so I can't try downloading it again. What's going on?

    Keynotes got hungup in updating and kept everything else from downloading. I deleted Keynotes and everything else downloaded. Then I tried to relad Keynotes and it didn't show up as something I've purchased. What's going on?

    Hi Liz,
    Sorry to hear you are having a similar problem.  Last night I went to the tool bar at the top of iphoto, clicked on "File",  then clicked "Browse Backups" in the drop down menu.    I have an external hard drive that is set up to Time Machine.   The Browse Backups  opened the iphoto pages in the Time Machine.  I selected a date one day ahead of the day I performed the now infamous update, and it showed my iphoto library as it had existed that day.   I then clicked  "Restore Library" at the bottom right corner of the Time Machine screen.   Roughly 2 hours later my iphoto was back to normal.   When I opened iphoto there was a message saying I need to upgrade my program to be compatible with the new version of iphoto(version 9.2.1).  I clicked "Upgrade" and within seconds it had done whatever upgrading it needed to do. 
    The only glitch in the restoration was that it restored the library as it appeared last week, so I no longer had photos I had imported this past weekend.   I simply went back to the Browse Backups in the drop down menu,  when Time Machine opened I selected the page showing my pictures from this weekend and again said to Restore Library.   Roughly 45 minutes later the library was restored including the most recent photos.  
    I am now a happy camper. 
    I don't know if any of this will be of help to you because your email says you are having trouble with photos imported after the upgrade was performed.   Have you had any pop up notices when you first open iphoto,  that tell you you need an upgrade to be compatible with the new iphoto?     If so have you clicked "upgrade"? 
    Good luck Liz,  if you have Time Machine running as a back up to your library, maybe you wil be able to get help there, by following my instructions above.   Otherwise,   good luck with your investigations.   I'd be interested in hearing how you make out.
    Karen

  • TS3991 I am working on an article of over 7000 words... suddenly, Page, says that document cannot be updated and opened... I lost valuable time and text in the process! What is going on here?

    I am working on an article of over 7000 words... suddenly, Page, says that document cannot be updated and opened... I lost valuable time and text in the process! What is going on here?

    Pages used to make backups... It does no longer... I cannot even revert to a previous version not being able to open the document that I was still working on half an hour ago. Thank heavens, I made a copy by selecting all text and copying it into an email when the problem first occurred yesterday evening. I am now working on it as an email. I can't risk loosing text this way anymore thru Pages. This is very bad. I am truly disappointed by this... particularly having moved from MS Office to iWorks!

  • Browse and Open  an Image from a phone,  save too

    can anyone tell me how to browse and open an image from the phone and then save that image with different name?

    you only have access to file fia the File Connection API, otherwise you can just read it from your jar, not writ or change the name.

  • I'm no longer able to click and open AI file from my folder.

    I’m no longer able to click and open AI file from my folder. 
    Error messege:  Unable to open... insufficient file....
    Evertime I open AI file, I need to reopen the illustrator software and then click the AI file from saved files.
    Please help.

    Not enough information. What OS? What version of AI? Where are the files located? Do they open from the File menu in AI?

  • HT3702 My apple id unable to update and install the software from my iphone

    My apple id unable to update and install the software from my iphone.I had checked the purchased history and found that my last payment had failed.I went to the bank to check my debit card status.But unable to found any problem from the bank.Had confirmed that my debit card have sufficient money to deduct.Pls help me as soon as possible ...
    Thanks,
    Jay Tan

    When you select update, verify that it is showing your correct Apple ID, perhaps you have an app that was downloaded with a different ID, all apps are always associated with the ID that first downloaded them.

  • Why can't I click and open my Skydrive from the favorites bar in finder?

    I am able to do this for Google Drive and also Copy, so I should be able to do the same for Skydrive.
    So how come I cannot click on Skydrive in the favorites bar in finder? I can click on everything else but when I try to click skydrive it is as if I clicked on the window outside of finder where it would be inactive. I want to be able to open my skydrive from finder (in favorites) so how can I fix this?
    Please and thanks for any answers at all in advance

    If I understand correctly you want the title to appear on top of a later video clip so both title and clip are visible.   The problem is the "whites" background which is both making the background opaque and stopping you from pasting it above another clip.  If you just select and drag the title by itself, the background will be transparent so the video clip below will show through.  You should also be able to copy and paste it directly above the clip you want.
    If you want different parts of the title to have a special effect you will have to edit each instance after you have made and positioned the copies.
    I hope this helps - or have I misunderstood?
    Geoff.

  • Calling PL/SQL Functions And Open Oracle Reports From ADF Application

    Hi all,
    My company will convert some projects from Oracle forms to Oracle ADF so, we need to call the PL/SQL functions and open the Oracle Reports (which are already exist) from ADF Application.
    Thank You..
    Jack.N

    Hi Jack.N,
    calling PL/SQL Functions -----> http://sameh-nassar.blogspot.com/2010/01/create-plsql-function-and-call-it-from.html
    Open Oracle Reports ---------> http://radio-weblogs.com/0137094/2008/06/15.html
    You will find The Integration between ADF and other systems in ---> http://wiki.oracle.com/page/ADF+Integration
    Sameh Nassar

  • I received (and opened) a mail from a friend, it contained a link that I could not open ( I tried, though) . It was a hacked message, she later apologized.. Can my iPad2 be affected? please advise

    I received ( and opened, because I knew her address as safe) a mail from  friend, it contained a link that I tried to open, but it could not.
    It was clear it was a hacked email, she then apologized..could my iPad2 be affected by this? Please advise, thank you.

    You SHOULD be safe. The iPad's operating system is different from PC or Mac ones, and most viruses and malware are written to infect PC's or Mac's.
    If, for example, that link led to an .exe file the iOS can't run that so it does nothing.

  • How to get updated and recently created events from google calendar in pl/s

    Hello
    I am able to get Google calendar events using pl/sql..I am receiving huge xml code(if there are more events),want only update and recently created events instead of getting repeated events.please help me in this regard.
    Thanks

    From the Google Calendar API online documentation:
    "Retrieving an event again
    If you want to retrieve an event that you've retrieved before, you can improve efficiency by telling Calendar to send the event only if it has changed since the last time you retrieved it."
    http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingCached

  • Open Doc Function to pass parameters from Xcelsius to WEBI

    Hi All,
    I need to have a drill through functionality from xcelsius report to WebI report I am using the following open doc URL
    http://BO ServerName/OpenDocument/opendoc/openDocument.jsp?
    sType=wid&sRefresh=Y&sIDType=CUID&iDocID="Doc ID"&mode=full&lsSRegion=""&lsSDay=""&lsSPlant=""&lsSProduct%20Name=""
    When I run the flash in Internet Explorer then the drill through webi report opens without any error for the 1st time
    but when I try to reopen the report again with new set of parameters , the browser gets hanged
    Also when I run the flash in Firefox , I get error while opening the Webi Report for the 1st time but when I try to open the report again it works fine.
    Please let me know if anyone has faced similar issue before.
    I am Using Xcelsius 2008 SP3 and BO XI 3.1
    Thanks,
    Shweta

    Hi Shweta
    If you place the URL un the browser directly does the report open and refresh ok?  Can you try altering the parameters of the URL, also directly in the browser?
    Something else to check would be if the prompts in the WebI report can be optional, store the last value etc, and I wouldn't recommend having a prompt which has a space in the name such as you have (Product%20Name). Keep the prompts as simple as possible, and make sure that the 'equal to' and 'in list' operators are properly reflected in the URL. 
    Regards
    Charles

  • Authentication error trying to download and open a book from my library using Windows 8 on my Surface2 tablet.

    When I chose ePUB as the download option for books from my library, the Overdrive Media Console application times out and locks up.  I get an Authentication error saying unable to open.  Then, I am unable to return to books to try another download option as the item did not successfully open using the Adobe Reader Digital Rights Management.

    See http://windows.microsoft.com/en-us/windows/change-file-open-program#1TC=windows-7
    Also http://windows.microsoft.com/en-us/windows/change-default-programs#1TC=windows-7

  • Did an update and lost music purchased from the iPhone?

    OK, updated a friends iPhone, it appeared to backup and all but when it it was done the music purchased from the phone was missing?? Is it hidden somewhere I can get it back onto her phone? I figured it was like apps that have been bought, once you buy them you can download them again..Guess that's not the case!! also some apps where missing. but that was easy enough to put back on, just a pain!

    Not the iphone back up.
    Everyone should always keep a back up copy of their important info on an external source - external drive, flash drive, dvd, cd, etc.
    If you do not back up, you will likely lose it at some point.
    Itunes prompts you to back up on occasion. You can click "Support" at the top of this page and type back up. You can use the help feature in itunes.
    You can copy the entire itunes folder.
    Please back up. I am amazed that some do not back up their info.
    It will save a lot of frustration in the future.

Maybe you are looking for

  • All my Notes are gone now. How do I get my Notes back?

    I noticed emails in my sent folder titled No Reciepent. I deleted them I found that all my Notes are gone now. How do I get my Notes back?

  • Cant join PC to domain and not able to send emails

    Hi I'm having a few issues with a SBS 2011 Standard server. To give you a setup overview Server x1 (SBS 2011) Workstations x6 (4 running Windows 7 Pro and 2 running Windows 8.1 Pro) The server is used as domain controller and exchange. It doesn't hos

  • Iphone 3gs wont sync

    Whenever i try to sync my iphone to my computer, the computer will recognize the camera of my phone, but itunes does not register that the phone is connected. I tried restarting services, and i reinstalled itunes and all like it said to. if anybody h

  • ITUNESItunes is not letting me purchase a song it says that payment info. is required when i have $1.33 credit and the song only costs $1.29

    Itunes is not letting me buy a song! It keeps telling me that payment onfo. is required but a couple days ago i just redeemed a $25 itunes card and still have $1.33 left in credit and the song only costs $1.29! Whats the deal!!!???>:/

  • Adobe reader ver,8.21 インストールエラー

    os : windows vista home premiam メモリ:1G の状況で.ダウンロードは出来るのですが.インストールしようとすると.エラー 「アセンブル processor architecture = x86 のインストールに失敗しました.」 という文が出てきてインストールできない状況です. どなたか解決方法が分かる方.ご教授お願いします.