How to select a row for update values, Using CTEs or suitable method

HI All,
I have a table as claim_data as below.
claim_id   
amount         change_monthkey
         created_monthkey
         ord
54511      
300            201304         
         201304          
          1
54511      
0              201305         
         201304          
          2
120301     
250            201502         
         201502          
          1
120624     
150            201502         
         201502          
          1
120624     
0              201503    
              201502          
          2
I want to isolate rows which appear in the table and do a update using below query ( This query is already in a procedure, so I just looking for a suitable way to modify it to support my task )
In the above e.g.  ONLY row containing claim_id = 120301 needs to update (amount  as updated_amount)
I use following query, but it update other rows as well, I want a suitable query only to capture rows of nature claim_id = 120301 and do this update? Is there any functions that I could use to facilitate my task??
select
     a.claim_id
    , a.Created_MonthKey
    , a.Change_MonthKey
    , a.amount - ISNULL(b.amount,0.00) as amount_movement
    ,a.amount  as updated_amount   --- >> I need help here??
    FROM claim_data a
LEFT JOIN claim_data b
    ON a.claim_id = b.claim_id
    AND b.Ord = a.Ord - 1
Thanks
Mira

Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your datC1. You should follow ISO-11179 rules for naming data elements. You do not know this IT standard.
You should follow ISO-8601 rules for displaying temporal datC1. We need to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. 
And you need to read and download the PDF for: 
https://www.simple-talk.com/books/sql-books/119-sql-code-smells/
>> I have a table AS claim_data AS below. << 
Thanks to your lack of netiquette, we have no name, no DDL, no keys and have to re-type the raw data into SQL, guess at everything! Does your boss treat you with the same contempt? 
Think about how silly “_data” is AS an attribute property! What tables do you have without an data in them? You had to give this meta-data in 1970's operating systems, FORTRAN compilers, etc. but we do not do this today. I see you use “A”, “B”, etc for table
aliases This is the name of the disk or tape drive in a 1970 computer! In SQL we use helpful, meaningful table aliases so code is easier to maintain. I see you also use the old punch card trick of putting a comma at the front of each line of code. 
The column named “ord” is a mathematical fiction for ordinal sets. I think you meant to use it as a non-relational sequential number to let you keep writing 1970's COBOL is T-SQL. We have not used the old Sybase ISNULL() since we got COALESCE() in SQL-92. They
are actually different. 
First, let's post what you should have posted if you followed forum rules: 
CREATE TABLE Claims
(claim_id CHAR(5) NOT NULL,
 claim_amount DECIMAL (8,2) NOT NULL
 CHECK (claim_amount >= 0.00),
 change_month_name CHAR(10) NOT NULL
 REFERENCES Month_Period(month_name),
 creation_month_name CHAR(10) NOT NULL
 REFERENCES Month_Period(month_name),
 ord INTEGER NOT NULL, -- NO!! Awful design! 
 PRIMARY KEY (claim_id, ord)); --required by definition
Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML.
The report period table gives a name to a range of dates that is common to the entire enterprise. 
CREATE TABLE Month_Periods
(month_name CHAR(10) NOT NULL PRIMARY KEY
 CHECK (month_name LIKE '[12][0-9][0-9][0-9]-[01][0-9]-00'),
 month_start_date DATE NOT NULL,
 month_end_date DATE NOT NULL,
 CONSTRAINT date_ordering
 CHECK (month_start_date <= month_end_date),
etc);
These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601
data format required by Standard SQL and it is language independent. It has been proposed for the Standard, but is not yet part of it. The regular expression pattern for validation is simple. 
INSERT INTO Claims  -- a mess! 
VALUES
('054511', 300.00, '2013-04-00', '2013-04-00', 1)
('054511', 0.00, '2013-05-00', '2013-04-00', 2),
('120301', 250.00, '2015-02-00', '2015-02-00', 1),
('120624', 150.00, '2015-02-00', '2015-02-00', 1),
('120624', 0.00, '2015-03-00', '2015-02-00', 2);
Now, throw this mess out. It looks like your repeat the claim creation date over and over and over. It looks like you crammed a claim creation and a claim history together in one table. 
>> I want to isolate rows which appear in the table and do a update using below query (This query is already in a procedure, so I just looking for a suitable way to modify it to support my task). 
In the above e.g. ONLY row containing claim_id = '120301' needs to update (amount AS updated_amount)
>> I use following query, but it update other rows as well, I want a suitable query only to capture rows of nature claim_id = '120301' and do this update? <<
No. SQL is set oriented so an update applies to the whole table. You can filter out rows in a WHERE clause. 
CREATE TABLE Claims
(claim_id CHAR(5) NOT NULL PRIMARY KEY,
 claim_amount DECIMAL (8,2) NOT NULL
 CHECK (claim_amount >= 0.00),
claim_creation_month_name CHAR(10) NOT NULL
 REFERENCES Month_Period(month_name));
See how this works? 
INSERT INTO Claims  -- a mess! 
VALUES
('054511', 300.00, '2013-04-00'),
('120301', 250.00, '2015-02-00'),
('120624', 150.00, '2015-02-00');
Normalize the table: 
CREATE TABLE Claim_Changes
(claim_id CHAR(5) NOT NULL 
  REFERENCES Claims(claim_id)
  ON DELETE CASCADE,
 change_month_name CHAR(10) NOT NULL
  REFERENCES Month_Period(month_name),
 PRIMARY KEY (claim_id, change_month_name),
 change_amount DECIMAL (8,2) NOT NULL
 CHECK (change_amount >= 0.00)
INSERT INTO Claims_Changes
VALUES
('054511', 0.00, '2013-05-00'),
('120624', 0.00, '2015-03-00');
Now you just add another change row to the history. There is no update. If you want to the delta and so forth, use LAG() and other window functions. 
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL

Similar Messages

  • How does select stmt with for all entries uses Indexes

    Hello all,
    I goes through a number of documents but still confused how does select for all entries uses indexes if fields are not in sequences. i got pretty much the same results if i take like two cases on Hr tables HRP1000 and HRP1001(with for all entries based upon hrp1000). Here is the sequence of index fields on hrp1001 (MANDT, OTYPE, OBJID, PLVAR, RSIGN, RELAT, ISTAT, PRIOX, BEGDA, ENDDA, VARYF, SEQNR). in second case objid field is in sequence as in defined Index but i dont see significant increase in field even though the number of records are around 30000. My question is does it make a differrence to use field sequence (same as in table indexes) in comparison to redundant field sequence (not same as defined in table indexes), secondly how we can ge tto know if table index is used in Select for entries query i tried Explain in ST05 but its not clear if it uses any index at all in hrp1001 read.
    here is the sample code i use to get test results.
    test case 1
    REPORT  zdemo_perf_select.
    DATA: it_hrp1000 TYPE STANDARD TABLE OF hrp1000 WITH HEADER LINE.
    DATA: it_hrp1001 TYPE STANDARD TABLE OF hrp1001 WITH HEADER LINE.
    DATA: it_hrp1007 TYPE STANDARD TABLE OF hrp1007 WITH HEADER LINE.
    DATA: it_pa0000 TYPE STANDARD TABLE OF pa0000 WITH HEADER LINE.
    DATA: it_pa0001 TYPE STANDARD TABLE OF pa0001 WITH HEADER LINE.
    DATA: it_pa0002 TYPE STANDARD TABLE OF pa0002 WITH HEADER LINE.
    DATA: it_pa0105_10 TYPE STANDARD TABLE OF pa0105 WITH HEADER LINE.
    DATA: it_pa0105_20 TYPE STANDARD TABLE OF pa0105 WITH HEADER LINE.
    DATA: t1 TYPE timestampl,
          t2 TYPE timestampl,
          t3 TYPE timestampl 
    SELECT * FROM hrp1000 CLIENT SPECIFIED  INTO TABLE it_hrp1000 bypassing buffer
                WHERE mandt EQ sy-mandt AND
                      plvar EQ '01' AND
                      otype EQ 'S'AND
                      istat EQ '1' AND
                      begda <= sy-datum AND
                      endda >= sy-datum AND
                      langu EQ 'EN'.
    GET TIME STAMP FIELD t1.
    SELECT * FROM hrp1001 CLIENT SPECIFIED INTO TABLE it_hrp1001 bypassing buffer
                FOR ALL ENTRIES IN it_hrp1000
                 WHERE mandt EQ sy-mandt AND
                        otype EQ 'S' AND
    *                    objid EQ it_hrp1000-objid and
                        plvar EQ '01' AND
                        rsign EQ 'B' AND
                        relat EQ '007' AND
                        istat EQ '1' AND
                        begda LT sy-datum AND
                        endda GT sy-datum and
                        sclas EQ 'C' and
                        objid EQ it_hrp1000-objid.
    *                    %_hints mssqlnt 'INDEX(HRP1001~0)'.
    *delete it_hrp1001 where sclas ne 'C'.
    GET TIME STAMP FIELD t2.
    t3 = t1 - t2.
    WRITE: 'Time taken - ', t3.
    test case 2
    REPORT  zdemo_perf_select.
    DATA: it_hrp1000 TYPE STANDARD TABLE OF hrp1000 WITH HEADER LINE.
    DATA: it_hrp1001 TYPE STANDARD TABLE OF hrp1001 WITH HEADER LINE.
    DATA: it_hrp1007 TYPE STANDARD TABLE OF hrp1007 WITH HEADER LINE.
    DATA: it_pa0000 TYPE STANDARD TABLE OF pa0000 WITH HEADER LINE.
    DATA: it_pa0001 TYPE STANDARD TABLE OF pa0001 WITH HEADER LINE.
    DATA: it_pa0002 TYPE STANDARD TABLE OF pa0002 WITH HEADER LINE.
    DATA: it_pa0105_10 TYPE STANDARD TABLE OF pa0105 WITH HEADER LINE.
    DATA: it_pa0105_20 TYPE STANDARD TABLE OF pa0105 WITH HEADER LINE.
    DATA: t1 TYPE timestampl,
          t2 TYPE timestampl,
          t3 TYPE timestampl 
    SELECT * FROM hrp1000 CLIENT SPECIFIED  INTO TABLE it_hrp1000 bypassing buffer
                WHERE mandt EQ sy-mandt AND
                      plvar EQ '01' AND
                      otype EQ 'S'AND
                      istat EQ '1' AND
                      begda <= sy-datum AND
                      endda >= sy-datum AND
                      langu EQ 'EN'.
    GET TIME STAMP FIELD t1.
    SELECT * FROM hrp1001 CLIENT SPECIFIED INTO TABLE it_hrp1001 bypassing buffer
                FOR ALL ENTRIES IN it_hrp1000
                 WHERE mandt EQ sy-mandt AND
                        otype EQ 'S' AND
                        objid EQ it_hrp1000-objid and
                        plvar EQ '01' AND
                        rsign EQ 'B' AND
                        relat EQ '007' AND
                        istat EQ '1' AND
                        begda LT sy-datum AND
                        endda GT sy-datum and
                        sclas EQ 'C'." and
    *                    objid EQ it_hrp1000-objid.
    *                    %_hints mssqlnt 'INDEX(HRP1001~0)'.
    *delete it_hrp1001 where sclas ne 'C'.
    GET TIME STAMP FIELD t2.
    t3 = t1 - t2.
    WRITE: 'Time taken - ', t3.

    Mani wrote:
    Thank you for your answer, its very helpful but i am still nor sure how does parameter rsdb/max_blocking_factor affect records size.
    Hi,
    The blocking affects the size of the statement and the memory structures for returning the result.
    So if your itab has 500 rows and your blocking is 5, the very same statement will be executed 100 times.
    Nothing good or bad about this so far.
    Assume, your average result for an inlist 5 statement is 25 records with an average size of 109 bytes.
    You average result size will be 2725 byte plus overhead which will nearly perfectly fit into two 1500 byte ethernet frames.
    Nothing to do in this case.
    Assume your average result for an inlist 5 statement is 7 records with an average size of 67 bytes.
    You average result size will be ~ 470 byte plus overhead which will only fill 1/3 of a 1500 byte ethernet frame.
    In this case, setting the blocking to 12 ... 15 will give you 66% network transfer performance gain,
    and reduces the number of calls to the DB by 50%, giving additional benefit.
    Now this is an extreme example. The longer the average row length is, the lower will be the average loss in the network.
    You have the same effects in memory structures, but on that layer you are fighting single micro seconds instead of
    hundreds of these, so in real life it is rarely measurable.
    Depending on table-statistics, oracle might decide for short inlists to use a concatanation instead of an inlist.
    This is supposed to be more costy, but I never had a case where I could proove a big difference.
    Values from 5 to 15 for blocking seem to be ok for me. If you have special statements in customer coding,
    it #might# be benefitial to do the mentioned calculations and do some network tracing to see if you can squeeze your
    network efficiency by tuning the blocking.
    If you have jumbo frames enabled, it might be worth to be analyzed as well.
    If you are only on a DB-CI system that is loopback connected to the DB, I doubt there might be a big outcome.
    Hope this helps
    Volker

  • How to select only rows which column has a null value?

    Hi,
    I adds rows to my table with data field set to null;
    "INSERT INTO administrator.subzamowienia (subzamowienia.id_zamowienia,subzamowienia.id_transakcji,subzamowienia.nazwa_o,subzamowienia.nazwa_produktu,subzamowienia.ilosc,subzamowienia.nip_d,subzamowienia.data_realizacji) VALUES (administrator.id_zamowienia.curval,'0','PRAGA','Witaminy','2','43224','0');"
    Then i would like to update this field after a time with new date.
    To do this i need to know which rows has still data field set to null, i try sth like this but it doesn't work
    "SQL> select * from administrator.subzamowienia where subzamowienia.data_realizacji='null';
    select * from administrator.subzamowienia where subzamowienia.data_realizacji='null'
    B&#321;&#260;D w linii 1:
    ORA-01841: (pe&#322;ny) rok musi by&#263; pomi&#281;dzy -4713 i +9999 i nie mo&#380;e by&#263; 0
    or
    "SQL> select * from administrator.subzamowienia where subzamowienia.data_realizacji='0';
    select * from administrator.subzamowienia where subzamowienia.data_realizacji='0'
    B&#321;&#260;D w linii 1:
    ORA-01840: warto&#347;&#263; wej&#347;ciowa jest za krótka dla formatu daty
    how to choose this rows?

    Hi,
    VALUES (administrator.id_zamowienia.curval,'0','PRAGA','Witaminy','2','43224','0');I don't see null value...
    select * from administrator.subzamowienia where subzamowienia.data_realizacji='null'Should be :
    select * from administrator.subzamowienia where subzamowienia.data_realizacji is nullNicolas.

  • Problem with "SELECT...FOR UPDATE OF..." and "POST command" combination

    Problem with "SELECT...FOR UPDATE OF..." and "POST command" combination
    Problem in committing transactions in Multiple Forms (Oracle Forms) with POST built-in command:
    Consider that the following statements are written in WHEN-WINDOW-CLOSED trigger of a called form.
    Statements in called form (Form name: FORM_CHILD):
    go_block('display_block') ;
    do_key('execute_query') ;
    -- Data from table_b will be populated in this block, based on the value of COLUMN_1 obtained
    -- from TABLE_A.
    -- Example: If the value of COLUMN_1 is 10, then all the matching records from TABLE_B, which
    -- are inserted with value 10 in TABLE_B.COLUMN_1 will be fetched and shown here.
    if user_choice = 'YES' then
    commit ;
    else
    rollback ;
    end if ;
    Statements in calling forms:
    There are two calling forms having following statements and it is going to call the above said called form.
    CALLING FORM 1
    Statements in KEY-COMMIT trigger:
    post;
    call_form(form_child, no_activate) ;
    Statements in ON-INSERT trigger:
    select column_1
    from table_a
    for update of column_1
    where column_2 = 'X' ;
    update table_a
    set column_1 = column_1 + 1
    where column_2 = 'X' ;
    insert into table_b ...;
    insert into table_b ...; Statements in KEY-COMMIT trigger:
    post;
    call_form(form_child, no_activate) ;
    CALLING FORM 2:
    Statements in ON-INSERT trigger:
    select column_1
    from table_a
    for update of column_1
    where column_2 = 'X' ;
    update table_a
    set column_1 = column_1 + 1
    where column_2 = 'X' ;
    insert into table_b ...;
    insert into table_b ...;
    insert into table_b ...;
    Our understanding:
    Assume that both the forms are running from two different machines/instances, issuing commit at the same time. In this case, forms will start executing the statements written in ON-INSERT trigger, the moment POST command is executed. Though the commit is issued at the same time, according to oracle, only one of the request will be taken for processing first. Assume that calling form 1 is getting processed first.
    So, it fetches the value available in COLUMN_1 of TABLE_A and locks the row from further select, update, etc. as SELECT...FOR UPDATE command is used (note that NOWAIT is not given, hence the lock will be released only when COMMIT or ROLLBACK happens) and proceed executing further INSERT statements. Because of the lock provided by the SELECT...FOR UPDATE command, the statements in calling form 2 will wait for the resource.
    After executing the INSERT statements, the FORM_CHILD is called. The rows inserted in to TABLE_A will be queried and shown. The database changes will be committed when user closes the window (as COMMIT is issued in its WHEN-WINDOW-CLOSED trigger). Then the SELECT...FOR UPDATE lock will be released and calling form 2's statements will be executed.
    Actual happenings or Mis-behavior:
    Calling form 2 starts executing INSERT statements instead of waiting for SELECT...FOR UPDATE lock. Also, the value selected from TABLE_A.COLUMN_1 is same in both the calling forms, which is wrong.
    The rows inserted into TABLE_B are having similar COLUMN_1 values in calling form 2 and they are fetched and shown in the called form FORM_CHILD.
    Note that in calling form 2 also POST only is issued, but the changes posted there are accessible in calling form 1 also, which is wrong.
    Kindly suggest us as to how to fix above problem. It will be much use, if you can send us the information regarding the behavior of Oracle Forms POST built-in also.
    Our mail ID: [email protected]
    Thanks a lot in advance.

    You have several problems:
    1. On-Insert will ONLY run if you have created a new record in a base-table block. If you haven't done that, then the POST command will not cause it to run.
    2. Select for update without a "no wait" will lock records for the first form, but when the second form tries this, it will hit the ORA-00054 exception, and will NOT wait. The only way you could make it wait is to issue an UPDATE sql command, which is not such a good way to go.
    All POST does is issues SQL insert or update commands for any changes the user has made to records in a form's base-table blocks, without following with a Commit command.
    Also understand that Commit is the same as Commit_Form, and Rollback is the same as Clear_Form. You should read up on these in the Forms help topics.

  • Re: Transactions and Locking Rows for Update

    Dale,
    Sounds like you either need an "optimistic locking" scheme, usually
    implemented with timestamps at the database level, or a concurrency manager.
    A concurrency manager registers objects that may be of interest to multiple
    users in a central location. It takes care of notifying interested parties
    (i.e., clients,) of changes made to those objects, using a "notifier" pattern.
    The optimistic locking scheme is relatively easy to implement at the
    database level, but introduces several problems. One problem is that the
    first person to save their changes "wins" - every one else has to discard
    their changes. Also, you now have business policy effectively embedded in
    the database.
    The concurrency manager is much more flexible, and keeps the policy where
    it probably belongs. However, it is more complex, and there are some
    implications to performance when you get to the multiple-thousand-user
    range because of its event-based nature.
    Another pattern of lock management that has been implemented is a
    "key-based" lock manager that does not use events, and may be more
    effective at managing this type of concurrency for large numbers of users.
    There are too many details to go into here, but I may be able to give you
    more ideas in a separate note, if you want.
    Don
    At 04:48 PM 6/5/97 PDT, Dale "V." Georg wrote:
    I have a problem in the application I am currently working on, which it
    seems to me should be easily solvable via appropriate use of transactions
    and database locking, but I'm having trouble figuring out exactly how to
    do it. The database we are using is Oracle 7.2.
    The scenario is as follows: We have a window where the user picks an
    object from a dropdown list. Some of the object's attributes are then
    displayed in that window, and the user then has the option of editing
    those attributes, and at some point hitting the equivalent of a 'save'button
    to write the changes back to the database. So far, so good. Now
    introduce a second user. If user #1 and user #2 both happen to pull up
    the same object and start making changes to it, user #1 could write back
    to the database and then 15 seconds later user #2 could write back to the
    database, completely overlaying user #1's changes without ever knowing
    they had happened. This is not good, particularly for our application
    where editing the object causes it to progress from one state to the next,
    and multiple users trying to edit it at the same time spells disaster.
    The first thing that came to mind was to do a select with intent to update,
    i.e. 'select * from table where key = 'somevalue' with update'. This way
    the next user to try to select from the table using the same key would not
    be able to get it. This would prevent multiple users from being able to
    pull the same object up on their screens at the same time. Unfortunately,
    I can think of a number of problems with this approach.
    For one thing, the lock is only held for the duration of the transaction, so
    I would have to open a Forte transaction, do the select with intent to
    update, let the user modify the object, then when they saved it back again
    end the transaction. Since a window is driven by the event loop I can't
    think of any way to start a transaction, let the user interact with the
    window, then end the transaction, short of closing and re-opening the
    window. This would imply having a separate window specifically for
    updating the object, and then wrapping the whole of that window's event
    loop in a transaction. This would be a different interface than we wanted
    to present to the users, but it might still work if not for the next issue.
    The second problem is that we are using a pooled DBSession approach
    to connecting to the database. There is a single Oracle login account
    which none of the users know the password to, and thus the users
    simply share DBSession resources. If one user starts a transaction
    and does a select with intent to update on one DBSession, then another
    user starts a transaction and tries to do the same thing on the same
    DBSession, then the second user will get an error out of Oracle because
    there's already an open transaction on that DBSession.
    At this point, I am still tossing ideas around in my head, but after
    speaking with our Oracle/Forte admin here, we came to the conclusion
    that somebody must have had to address these issues before, so I
    thought I'd toss it out and see what came back.
    Thanks in advance for any ideas!
    Dale V. Georg
    Indus Consultancy Services [email protected]
    Mack Trucks, Inc. [email protected]
    >
    >
    >
    >
    ====================================
    Don Nelson
    Senior Consultant
    Forte Software, Inc.
    Denver, CO
    Corporate voice mail: 510-986-3810
    aka: [email protected]
    ====================================
    "I think nighttime is dark so you can imagine your fears with less
    distraction." - Calvin

    We have taken an optimistic data locking approach. Retrieved values are
    stored as initial values; changes are stored seperately. During update, key
    value(s) or the entire retieved set is used in a where criteria to validate
    that the data set is still in the initial state. This allows good decoupling
    of the data access layer. However, optimistic locking allows multiple users
    to access the same data set at the same time, but then only one can save
    changes, the rest would get an error message that the data had changed. We
    haven't had any need to use a pessimistic lock.
    Pessimistic locking usually involves some form of open session or DBMS level
    lock, which we haven't implemented for performance reasons. If we do find the
    need for a pessimistic lock, we will probably use cached data sets that are
    checked first, and returned as read-only if already in the cache.
    -DFR
    Dale V. Georg <[email protected]> on 06/05/97 03:25:02 PM
    To: Forte User Group <[email protected]> @ INTERNET
    cc: Richards* Debbie <[email protected]> @ INTERNET, Gardner*
    Steve <[email protected]> @ INTERNET
    Subject: Transactions and Locking Rows for Update
    I have a problem in the application I am currently working on, which it
    seems to me should be easily solvable via appropriate use of transactions
    and database locking, but I'm having trouble figuring out exactly how to
    do it. The database we are using is Oracle 7.2.
    The scenario is as follows: We have a window where the user picks an
    object from a dropdown list. Some of the object's attributes are then
    displayed in that window, and the user then has the option of editing
    those attributes, and at some point hitting the equivalent of a 'save' button
    to write the changes back to the database. So far, so good. Now
    introduce a second user. If user #1 and user #2 both happen to pull up
    the same object and start making changes to it, user #1 could write back
    to the database and then 15 seconds later user #2 could write back to the
    database, completely overlaying user #1's changes without ever knowing
    they had happened. This is not good, particularly for our application
    where editing the object causes it to progress from one state to the next,
    and multiple users trying to edit it at the same time spells disaster.
    The first thing that came to mind was to do a select with intent to update,
    i.e. 'select * from table where key = 'somevalue' with update'. This way
    the next user to try to select from the table using the same key would not
    be able to get it. This would prevent multiple users from being able to
    pull the same object up on their screens at the same time. Unfortunately,
    I can think of a number of problems with this approach.
    For one thing, the lock is only held for the duration of the transaction, so
    I would have to open a Forte transaction, do the select with intent to
    update, let the user modify the object, then when they saved it back again
    end the transaction. Since a window is driven by the event loop I can't
    think of any way to start a transaction, let the user interact with the
    window, then end the transaction, short of closing and re-opening the
    window. This would imply having a separate window specifically for
    updating the object, and then wrapping the whole of that window's event
    loop in a transaction. This would be a different interface than we wanted
    to present to the users, but it might still work if not for the next issue.
    The second problem is that we are using a pooled DBSession approach
    to connecting to the database. There is a single Oracle login account
    which none of the users know the password to, and thus the users
    simply share DBSession resources. If one user starts a transaction
    and does a select with intent to update on one DBSession, then another
    user starts a transaction and tries to do the same thing on the same
    DBSession, then the second user will get an error out of Oracle because
    there's already an open transaction on that DBSession.
    At this point, I am still tossing ideas around in my head, but after
    speaking with our Oracle/Forte admin here, we came to the conclusion
    that somebody must have had to address these issues before, so I
    thought I'd toss it out and see what came back.
    Thanks in advance for
    any
    ideas!
    Dale V. Georg
    Indus Consultancy Services [email protected]
    Mack Trucks, Inc. [email protected]
    ------ Message Header Follows ------
    Received: from pebble.Sagesoln.com by notes.bsginc.com
    (PostalUnion/SMTP(tm) v2.1.9c for Windows NT(tm))
    id AA-1997Jun05.162418.1771.334203; Thu, 05 Jun 1997 16:24:19 -0500
    Received: (from sync@localhost) by pebble.Sagesoln.com (8.6.10/8.6.9) id
    NAA11825 for forte-users-outgoing; Thu, 5 Jun 1997 13:47:58 -0700
    Received: (from uucp@localhost) by pebble.Sagesoln.com (8.6.10/8.6.9) id
    NAA11819 for <[email protected]>; Thu, 5 Jun 1997 13:47:56 -0700
    Received: from unknown(207.159.84.4) by pebble.sagesoln.com via smap (V1.3)
    id sma011817; Thu Jun 5 13:47:43 1997
    Received: from tes0001.macktrucks.com by relay.macktrucks.com
    via smtpd (for pebble.sagesoln.com [206.80.24.108]) with SMTP; 5 Jun
    1997 19:35:31 UT
    Received: from dale by tes0001.macktrucks.com (SMI-8.6/SMI-SVR4)
    id QAA04637; Thu, 5 Jun 1997 16:45:51 -0400
    Message-ID: <[email protected]>
    Priority: Normal
    To: Forte User Group <[email protected]>
    Cc: "Richards," Debbie <[email protected]>,
    "Gardner," Steve <[email protected]>
    MIME-Version: 1.0
    From: Dale "V." Georg <[email protected]>
    Subject: Transactions and Locking Rows for Update
    Date: Thu, 05 Jun 97 16:48:37 PDT
    Content-Type: text/plain; charset=US-ASCII; X-MAPIextension=".TXT"
    Content-Transfer-Encoding: quoted-printable
    Sender: [email protected]
    Precedence: bulk
    Reply-To: Dale "V." Georg <[email protected]>

  • When should SELECT ..FOR UPDATE be used?

    DB Version:10gR2
    This is what 10gR2 PL/SQL documentation says about SELECT...FOR UPDATE
    With the SELECT FOR UPDATE statement, you can explicitly lock specific rows of a
    table to make sure they do not change after you have read them. That way, you
    can check which or how many rows will be affected by an UPDATE or DELETE
    statement before issuing the statement, and no other application can change the
    rows in the meantime
    But i don't see SELECT...FOR UPDATE much in our production codes. Is SELECT ..FOR UPDATE used when huge amount of rows need to be updated?

    Its maily used for locking table with differnt node , commely this concpet where used in
    ERP products, bcoz different user can acces same table at time and do some DML
    operation, using FOR UPDATE will protected ,
    before that read this
    hb venki

  • Urgent :  how to select the rows in the ALV Grid Control

    How to select the rows in the ALV Grid control,
    I am facing the situation where i need to select the row/rows in the Grid control and then to lock the entries,
    If anyone have the solution please help me out
    <b>Its very Urgent</b>

    Hi Bharath,
    Go through this hope u can understand.
    SEL_MODE. Selection mode, determines how rows can be selected. Can have the following values:
    A Multiple columns, multiple rows with selection buttons.
    B Simple selection, listbox, Single row/column
    C Multiple rows without buttons
    D Multiple rows with buttons and select all ICON
    Setting and getting selected rows (Columns) and read line contents
    You can read which rows of the grid that has been selected, and dynamic select rows of the grid using methods get_selected_rows and set_selected_rows. There are similar methods for columns.
    Note that the grid table always has the rows in the same sequence as displayed in the grid, thus you can use the index of the selected row(s) to read the information in the rows from the table. In the examples below the grid table is named gi_sflight.
    Data declaration:
    DATA:
    Internal table for indexes of selected rows
    gi_index_rows TYPE lvc_t_row,
    Information about 1 row
    g_selected_row LIKE lvc_s_row.
    Example 1: Reading index of selected row(s) and using it to read the grid table
      CALL METHOD go_grid->get_selected_rows
        IMPORTING
          et_index_rows = gi_index_rows.
      DESCRIBE TABLE gi_index_rows LINES l_lines.
      IF l_lines = 0.
        CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
             EXPORTING
                  textline1 = 'You must choose a valid line'.
        EXIT.
      ENDIF.
      LOOP AT gi_index_rows INTO g_selected_row.
         READ TABLE gi_sflight INDEX g_selected_row-index INTO g_wa_sflight.
        ENDIF.
      ENDLOOP.
    Example 2: Set selected row(s).
      DESCRIBE TABLE gi_index_rows LINES l_lines.
      IF l_lines > 0.
        CALL METHOD go_grid->set_selected_rows
            exporting
              it_index_rows = gi_index_rows.
      ENDIF.
    Reward points if helpful.
    Thanks
    Naveen khan

  • How to select a row in Jtable at runtime

    how to select a row in Jtable at runtime.

    use
    setRowSelectionInterval(int fromRowIndex, int toRowIndex);example if your table has 10 rows then u want to select the rows from 4 to 8 then use
    setRowSelectionInterval(3, 7);if you want to select just one row for example 5 then use
    setRowSelectionInterval(5, 5);

  • Select...for update and close()

    I am seeing unexpected behaviour when working with select for update, and I hope to get some clarification about it. I have RTFM and checked out any online information I could find, but didn't see too much about row level locks. See below for the problem I am encountering
    We have an application with lots of threads on multiple nodes that uses a combination of row level locking and status flags to mark a row as being processed.
    We are locking the rows, and then after completing the select ... for update statement we are closing the JDBC statement.
    On executing the statement.close() it appears as though the row level lock is lost. Can anyone confirm this behaviour, or tell me that I am misreading the behaviour that I am seeing.
    Thanks in advance,
    Robert Dawson

    I'd expect this behavior regardless of whether autocommit was on. If a statement is closed, any locks held by that statement must be released. If they weren't, no other statement would have permission to remove the locks, so the rows would remain locked indefinitely.
    Justin

  • Unecessary use of SELECT...FOR UPDATE causing Deadlock

    DB version:10gR2
    We are getting a deadlock issue frequently. When i looked at the trace file i found that the <em>Rolled back SQL</em> and the <em>Successfull SQL</em> are both <strong>SELECT FOR UPDATE</strong> statements. They both are declared before UPDATE statments.
    Both of these PL/SQL codes are developed by a C++ guy. When is it appropriate to use SELECT ..FOR UPDATE. I've seen SELECT ...FOR UPDATE very rarely in the codes developed by PL/SQL gurus in our firm. Why didn't they use SELECT..FOR UPDATE to lock rows before UPDATE/DELETE/INSERT in their codes?

    For update is a rowlevel locking. like
    Session 1
    SQL> conn scott/tiger
    Connected.
    SQL> select * from emp;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
          7839 KING       PRESIDENT            17-NOV-81       5000                    10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
          7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7900 JAMES      CLERK           7698 03-DEC-81        950                    30
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
    14 rows selected.
    SQL> select * from emp where empno = 7369 for update;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20
    SQL>
    Session 2
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    SQL> update emp set sal = 1000 where empno = 7900
      2  /
    1 row updated.
    SQL> commit;
    Commit complete.
    SQL> update emp set sal = 1000 where empno = 7369
      2
    SQL> /In session 1 we have lock row where empno = 7369 & in session 2 we are updating salary where empno = 7900 it is updating successfully, but we are updating a empno = 7369 then session 2nd hangs for waiting for the next session.
    Also read the Link (http://www.oracle.com/technology/oramag/oracle/05-nov/o65asktom.html)

  • 'Missing select' error for update statement using WITH clause

    Hi,
    I am getting the below error for update statement using WITH clause
    SQL Error: ORA-00928: missing SELECT keyword
      UPDATE A
      set A.col1 = 'val1'
         where
      A.col2 IN (
      WITH D AS
      SELECT col2 FROM
      (SELECT col2, MIN(datecol) col3 FROM DS
      WHERE <conditions>
        GROUP BY PATIENT) D2
      WHERE
      <conditions on A.col4 and D2.col3>

    Hi,
    The format of a query using WITH is:
    WITH  d  AS
        SELECT  ...  -- sub_query
    SELECT  ...   -- main query
    You don't have a main query.  The keyword FROM has to come immediately after the right ')' that ends the last WITH clause sub-query.
    That explains the problem based on what you posted.  I can't tell if the real problem is in the conditions that you didn't post.
    I hope this answers your question.
    If not, post a complete test script that people can run to re-create the problem and test their ideas.  Include a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
    In the case of a DML operation (such as UPDATE) the sample data should show what the tables are like before the DML, and the results will be the contents of the changed table(s) after the DML.
    Explain, using specific examples, how you get those results from that data.
    Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • How to select a row in datagrid by checking the check box in that row

    how to select a row in datagrid by checking the check box in that row.
    Im using <html:checkbox> tag, and also a VO which is in request scope. i wanna display the values in the VO in a row and corresponding to this i want a checkbox..
    Thanx in advance
    Message was edited by: me
    Hemanth@SA
    Message was edited by:
    Hemanth@SA

    Hello,
    I got the solution:
    final int pRow = row;
    final int pCol = column;
    final JTable myTable = mytable;
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    myTable.requestFocusInWindow();
    myTable.changeSelection(pRow, pCol, true, true);

  • The lock for "select ... for update"

    1. select * from tbl for update;
    2. SELECT p.spid, a.serial#, c.object_name, b.session_id, b.oracle_username, b.os_user_name FROM v$process p, v$session a, v$locked_object b, all_objects c WHERE p.addr = a.paddr AND a.process = b.process AND c.object_id = b.object_id
    Why there is two records with the same session_id:
    1 14728 11961 TBL 290 STAR bil
    2 14732 50904 TBL 290 STAR bil
    The SQL "select ... for update" will generate two locks?

    I get one lock:
    SQL> SELECT p.spid, a.serial#, c.object_name, b.session_id, b.oracle_username, b.os_user_name FROM v
    $process p, v$session a, v$locked_object b, all_objects c WHERE p.addr = a.paddr AND a.process = b.p
    rocess AND c.object_id = b.object_id;
    no rows selected
    SQL> select * from emp for update;
         EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 17-12-1980 00:00:00        800                    20
          7499 ALLEN      SALESMAN        7698 20-02-1981 00:00:00       1600        300         30
          7521 WARD       SALESMAN        7698 22-02-1981 00:00:00       1250        500         30
          7566 JONES      MANAGER         7839 02-04-1981 00:00:00       2975                    20
          7654 MARTIN     SALESMAN        7698 28-09-1981 00:00:00       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-05-1981 00:00:00       2850                    30
          7782 CLARK      MANAGER         7839 09-06-1981 00:00:00       2450                    10
          7788 SCOTT      ANALYST         7566 09-12-1982 00:00:00       3000                    20
          7839 KING       PRESIDENT            17-11-1981 00:00:00       5000                    10
          7844 TURNER     SALESMAN        7698 08-09-1981 00:00:00       1500          0         30
          7876 ADAMS      CLERK           7788 12-01-1983 00:00:00       1100                    20
          7900 JAMES      CLERK           7698 03-12-1981 00:00:00        950                    30
          7902 FORD       ANALYST         7566 03-12-1981 00:00:00       3000                    20
          7934 MILLER     CLERK           7782 23-01-1982 00:00:00       1300                    10
    14 rows selected.
    SQL> SELECT p.spid, a.serial#, c.object_name, b.session_id, b.oracle_username, b.os_user_name FROM v
    $process p, v$session a, v$locked_object b, all_objects c WHERE p.addr = a.paddr AND a.process = b.p
    rocess AND c.object_id = b.object_id;
    SPID            SERIAL# OBJECT_NAME                    SESSION_ID ORACLE_USERNAME                OS_USER_NAME
    2536                  4 EMP                                    25 HOEK                           hoek
    1 row selected.What database version are you using?
    (the result of: select * from v$version; )

  • Help, question about "select ... for update nowait"

    There is a proc code. In the beginning of the code, I used a SQL "select ... for update nowait" in order to prevent from another proc executing at the same time. When the case happens, "-54, ORA-00054: resource busy and acquire with NOWAIT specified" will be printed in the screen.
    But there is a question: I need to print sth to indicate "another proc is running". I used "if (sqlca.sqlcode == -54)" as precondition, such as:
    if (sqlca.sqlcode == -54) {
    printf("There is another proc running.\n");
    However, this line will not be printed. I doubt that the code quits directly when using "select ... for update nowait" so as not to set value (-54) to sqlca.sqlcode.
    So, could you suggest whether there is another way that I can use to print "There is another proc running" when another proc is running?
    Thx a lot for your kindly reply.

    Yes, that link. Scroll down a bit and you will see:
    The calling application gets a PL/SQL exception, which it can process using the error-reporting functions SQLCODE and SQLERRM in an OTHERS handler. Also, it can use the pragma EXCEPTION_INIT to map specific error numbers returned by raise_application_error to exceptions of its own, as the following Pro*C example shows:
    EXEC SQL EXECUTE
    /* Execute embedded PL/SQL block using host
    variables v_emp_id and v_amount, which were
    assigned values in the host environment. */
    DECLARE
    null_salary EXCEPTION;
    /* Map error number returned by raise_application_error
    to user-defined exception. */
    PRAGMA EXCEPTION_INIT(null_salary, -20101);
    BEGIN
    raise_salary(:v_emp_id, :v_amount);
    EXCEPTION
    WHEN null_salary THEN
    INSERT INTO emp_audit VALUES (:v_emp_id, ...);
    END;
    END-EXEC;
    This technique allows the calling application to handle error conditions in specific exception handlers.

  • "SELECT .. FOR UPDATE" lock event

    Hi All,
    The application will send "SELECT .. FOR UPDATE" statement when end-user click one module on screen.
    Now the system have many end-user need to use this module more than before and elapsed time is 15 seconds per session if have 10 sessions click this module that mean the maximum locked time is 150 seconds (15*10) and source code cannot change.
    My question is which oracle parameter can control about lock timeout in this case or not?
    I tried to check all parameter about lock and timeout of oracle parameter that cannot to solve it but I may missing something.
    Thansks
    Hiko

    taohiko wrote:
    Hi All,
    The application will send "SELECT .. FOR UPDATE" statement when end-user click one module on screen.
    Now the system have many end-user need to use this module more than before and elapsed time is 15 seconds per session if have 10 sessions click this module that mean the maximum locked time is 150 seconds (15*10) and source code cannot change.
    My question is which oracle parameter can control about lock timeout in this case or not?
    I tried to check all parameter about lock and timeout of oracle parameter that cannot to solve it but I may missing something.
    Thansks
    Hikohttp://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/static.htm#LNPLS603
    By default, the SELECT FOR UPDATE statement waits until the requested row lock is acquired. To change this behavior, use the NOWAIT, WAIT, or SKIP LOCKED clause of the SELECT FOR UPDATE statement.
    If you are adamant as you say that the application cannot be changed (almost never a truthful statement) ... then you are out of luck.

Maybe you are looking for