Question on Update table

DECLARE
vid number;
Hello,
i need to update a table which already has data in it. we added another coulmn which will be a unique key. This is basically a sequence. how do i upate the existing table with the data for this new column to generate the sequence.
i wrote the following code: but its creating new rows with the new sequence i want to upate the existing rows with the sequnce for the newly added column.
declare
vid number;
BEGIN
for c in ( select * from test)
loop
select test_SEQ.nextval into vid from dual;
insert into test (SEQ_NUM)
values (vid);
end loop;
commit;
end;
could someone help.
Thanks.

Hi,
789287 wrote:
DECLARE
vid number;
Hello,
i need to update a table which already has data in it. we added another coulmn which will be a unique key. This is basically a sequence. how do i upate the existing table with the data for this new column to generate the sequence.
i wrote the following code: but its creating new rows with the new sequence i want to upate the existing rows with the sequnce for the newly added column.INSERT means create new rows. You don't want to INSERT here.
UPDATE means to change values on existing rows. You want to UPDATE here.
declare
vid number;
BEGIN
for c in ( select * from test)
loop
select test_SEQ.nextval into vid from dual;
insert into test (SEQ_NUM)
values (vid);
end loop;
commit;
end;
could someone help.
Thanks.Try this:
UPDATE  test
SET     seq_num = test_seq.NEXTVAL;You don't need PL/SQL for this, but if you need PL/SQL for some other reason, the UPDATE statement above will work in PL/SQL
Edited by: Frank Kulash on Oct 20, 2011 3:45 PM

Similar Messages

  • Question about Update Tables

    Hello Gurus,
    I have a question about "update table" entries. I read somewhere that an entry in update table  is done at the time of the OLTP transaction. Is this correct? If so, does this happen on a V1 update or V2 update? Please clarify. Similarly, an entry in the "extraction queue" (incase you are using "queued delta" will happen on V1 update. I just want to get a clarification on both these methods. Any help in this matter is highly appreciated.
    Thanks,
    Sreekanth

    Hi
    update tables are temporary table that are refreshed by v3 job.update table is like buffer, it gets filled after updation of application table and statistical table through v1 v2 update .  we can bypass this process by delta methods.
    M Kalpana

  • Performance issue and functional question regarding updates on tables

    A person at my site wrote some code to update a custom field on the MARC table that was being copied from the MARA table.  Here is what I would have expected to see as the code.  Assume that both sets of code have a parameter called p_werks which is the plant in question.
    data : commit_count type i.
    select matnr zfield from mara into (wa_marc-matnr, wa_marc-zfield).
      update marc set zfield = wa_marc-zfield
         where werks = p_werks and matnr = wa_matnr.
      commit work and wait.
    endselect.
    I would have committed every 200 rows instead of every one row, but here's the actual code and my question isn't around the commits but something else.  In this case an internal table was built with two elements - MATNR and WERKS - could have done that above too, but that's not my question.
                DO.
                  " Lock the record that needs to be update with material creation date
                  CALL FUNCTION 'ENQUEUE_EMMARCS'
                    EXPORTING
                      mode_marc      = 'S'
                      mandt          = sy-mandt
                      matnr          = wa_marc-matnr
                      werks          = wa_marc-werks
                    EXCEPTIONS
                      foreign_lock   = 1
                      system_failure = 2
                      OTHERS         = 3.
                  IF sy-subrc <> 0.
                    " Wait, if the records not able to perform as lock
                    CALL FUNCTION 'RZL_SLEEP'.
                  ELSE.
                    EXIT.
                  ENDIF.
                ENDDO.
                " Update the record in the table MARC with material creation date
                UPDATE marc SET zzdate = wa_mara-zzdate
                           WHERE matnr = wa_mara-matnr AND
                                 werks = wa_marc-werks.    " IN s_werks.
                IF sy-subrc EQ 0.
                  " Save record in the database table MARC
                  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                    EXPORTING
                      wait   = 'X'
                    IMPORTING
                      return = wa_return.
                  wa_log-matnr   = wa_marc-matnr.
                  wa_log-werks   = wa_marc-werks.
                  wa_log-type    = 'S'.
                  " text-010 - 'Material creation date has updated'.
                  wa_log-message = text-010.
                  wa_log-zzdate  = wa_mara-zzdate.
                  APPEND wa_log TO tb_log.
                  CLEAR: wa_return,wa_log.
                ELSE.
                  " Roll back the record(un save), if there is any issue occurs
                  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
                    IMPORTING
                      return = wa_return.
                  wa_log-matnr   = wa_marc-matnr.
                  wa_log-werks   = wa_marc-werks.
                  wa_log-type    = 'E'.
                  " 'Material creation date does not updated'.
                  wa_log-message = text-011.
                  wa_log-zzdate  = wa_mara-zzdate..
                  APPEND wa_log TO tb_log.
                  CLEAR: wa_return, wa_log.
                ENDIF.
                " Unlock the record from data base
                CALL FUNCTION 'DEQUEUE_EMMARCS'
                  EXPORTING
                    mode_marc = 'S'
                    mandt     = sy-mandt
                    matnr     = wa_marc-matnr
                    werks     = wa_marc-werks.
              ENDIF.
    Here's the question - why did this person enqueue and dequeue explicit locks like this ?  They claimed it was to prevent issues - what issues ???  Is there something special about updating tables that we don't know about ?  We've actually seen it where the system runs out of these ENQUEUE locks.
    Before you all go off the deep end and ask why not just do the update, keep in mind that you don't want to update a million + rows and then do a commit either - that locks up the entire table!

    The ENQUEUE lock insure that another program called by another user will not update the data at the same time, so preventing database coherence to be lost. In fact, another user on a SAP correct transaction, has read the record and locked it, so when it will be updated your modifications will be lost, also you could override modifications made by another user in another luw.
    You cannot use a COMMIT WORK in a SELECT - ENDSELECT, because COMMIT WORK will close each and every opened database cursor, so your first idea would dump after the first update. (so the internal table is mandatory)
    Go through some documentation like [Updates in the R/3 System (BC-CST-UP)|http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCCSTUP/BCCSTUP_PT.pdf]
    Regards

  • Question on Updating the rows of a table using a SELECT Statement

    Hi,
    I am trying to set all of the R_IDs in one table to the ID from another table using a join.
    Here is the SQL I came up with. I don't this is correct though.
    UPDATE TABLEA A
    SET R_ID=(Select Id from TABLEB B
    WHERE A.DEE=B.DEE AND
    B.RNUM=1);
    Does this make sense?

    You can try the join view.
    UPDATE ( SELECT table_a.id   AS a_id,
                    table_b.id   AS b_id,
                    table_a.col1 AS a_col1,
                    table_b.col1 AS b_col1,
                    table_a.col2 AS a_col2,
                    table_b.col2 AS b_col2,
                    table_a.col3 AS a_col3,
                    table_b.col3 AS b_col3
               FROM table_a,
                    table_b
              WHERE table_a.id = table_b.id)
       SET a_col1 = b_col1,
           a_col2 = b_col2,
           a_col3 = b_col3;
    However it may result in
    ORA-01779: cannot modify a column which maps to a non key-preserved tableThis is because the key-preserving property of a table does not depend on the actual data in the table. It is, rather, a property of its schema. For example, if in table_b there was at most one record for each id value, then table_a.id would be unique in the result of a join of table_a and table_b, but table_a would still not be a key-preserved table. By adding the unique/primary key on the id column of table_b we can assure that if table_b.id were part of the result set of the join view then it would be unique. This makes table_a key preserved.
    Thus, in order to update a join view we must assure that the columns involved in the join view from the source table are unique. We do this by creating a unique or primary key on those columns.
    However, if you do not mind taking responsibility for making sure that there is one-to-one cardinality between the source and destination rows, then you might try the correlated subquery
    UPDATE table_a
       SET (col1, col2, col3 ) =
              ( SELECT col1, col2, col3
                  FROM table_b
                 WHERE table_b.id = table_a.id )
           WHERE EXISTS
               ( SELECT col1, col2, col3
                   FROM table_b
                   WHERE table_b.id = table_a.id )

  • Updating table with more than 50 million records

    Hi Team,
    Updating three columns in a table which has 60 millions of records is taking a lot of time. How to improve the performance of the query. The table has to be updated based on the values from different database tables.
    Thanks,
    Eshwar.
    Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users.

    Split them into small batches
    when you are using multiple tables, have them with JOINs
    have the corresponding indexes for joining columns
    disable triggers if any (if possible and doesnt affect your logic - wud be least favourable step ..)
    also these shud help:
    http://blogs.msdn.com/b/repltalk/archive/2011/10/10/lessons-learned-updating-100-millions-rows.aspx
    http://www.sqlservergeeks.com/blogs/AhmadOsama/personal/450/sql-server-optimizing-update-queries-for-large-data-volumes
    http://stackoverflow.com/questions/7344984/update-large-number-of-rows-sql-server-2005
    Thanks,
    Jay
    <If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>

  • Need a Query to update table from another table.

    I have two tables Table A and Table B , till now in table “A”. I have a column which consist of IDs and duplicate IDs( which are basically formed by Original Ids) , I have another table i.e. table “B” , which gives a mapping between original ids and duplicate Ids
    TABLE A:
    ID/DUPLICATEID      NAME
    1     Rahul
    1_CAD     Pawan
    2     Nikhil
    3     TOM
    3_CAD     Ravi
    3_MQ     Puneet
    TABLE B:
    ORIGINALID     DUPLICATEID
    1     1_CAD
    3     3_CAD
    3     3_MQ
    Now I want to have another column in Table “A” , which will give me the mapping between the original Id and duplicate Id as shown in updated table “A”.
    UPDATED TABLE A:
    ID/DUPLICATEID     NAME     ORIGINAL_ID
    1     Rahul     
    1_CAD     Pawan     
    2     Nikhil     
    3     TOM     
    3_CAD      Ravi     
    3_MQ     Puneet     
    Now I want to write a Query in which I can update this column (ORIGINAL_ID) of Table “A”, from the table B(basically want to update mulitple rows using single query), because table B already has this mapping. Can any one help me in this. I am basically a Java guy , so I don’t know much about it. I hope to get a positive response from you people, Thanks in advance!

    Here you go...
    <pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%">
    <code>
    SQL&gt; CREATE TABLE A (ID VARCHAR2(10), NAME VARCHAR2(10), NEW_ID VARCHAR2(10));
    Table created.
    SQL&gt; INSERT INTO A VALUES ('1', 'Rahul', '');
    1 row created.
    SQL&gt; INSERT INTO A VALUES ('1_CAD', 'Pawan', '');
    1 row created.
    SQL&gt; INSERT INTO A VALUES ('2', 'Nikhil', '');
    1 row created.
    SQL&gt; INSERT INTO A VALUES ('3', 'TOM', '');
    1 row created.
    SQL&gt; INSERT INTO A VALUES ('3_CAD', 'Ravi', '');
    1 row created.
    SQL&gt; INSERT INTO A VALUES ('3_MQ', 'Puneet', '');
    1 row created.
    SQL&gt; CREATE TABLE B (ID VARCHAR2(10), NAME VARCHAR2(10));
    Table created.
    SQL&gt; INSERT INTO B VALUES ('1', '1_CAD');
    1 row created.
    SQL&gt; INSERT INTO B VALUES ('3', '3_CAD');
    1 row created.
    SQL&gt; INSERT INTO B VALUES ('3', '3_MQ');
    1 row created.
    SQL&gt; COMMIT;
    Commit complete.
    SQL&gt; UPDATE A
    2 SET NEW_ID = NVL((SELECT B.ID FROM B WHERE A.ID = B.NAME),A.ID)
    3 /
    6 rows updated.
    SQL&gt; COMMIT;
    Commit complete.
    SQL&gt; SELECT * FROM A;
    ID NAME NEW_ID
    1 Rahul 1
    1_CAD Pawan 1
    2 Nikhil 2
    3 TOM 3
    3_CAD Ravi 3
    3_MQ Puneet 3
    6 rows selected.
    </code></pre>
    Note: While asking question do give us DML/DDL script. It will make peoples life better.
    Karthick.
    http://www.karthickarp.blogspot.com/

  • Update table all null values to 0 single query

    hi dear ;
    How Can I do , update table all null values to 0 using single query or procedure

    declare @tableName nvarchar(100)
    declare @querys varchar(max)
    set @querys = ''
    set @tableName = 'YOUR TABLE NAME HERE'
    select @querys = @querys + 'update ' + @tableName + ' set ' +
    QUOTENAME(t.[name]) + '=0 where ' + QUOTENAME(t.[name]) + ' is null;'
    from (SELECT [name] FROM syscolumns
    WHERE id = (SELECT id
    FROM sysobjects
    WHERE type = 'U'
    AND [NAME] = @tableName))t
    select @querys
    execute sp_executesql @sqlQuery
    Reference:
    http://stackoverflow.com/questions/6130133/sql-server-update-all-null-field-as-0
    -Vaibhav Chaudhari
    this code is return update TABLE set [FIELD]=isnull([FIELD],''),update TABLE set [FIELD2]=isnull([FIELD2],'')
    I want to use UPDATE TABLE SET FIELD1=ISNULL(FIELD1,0),FIELD2=ISNULL(FIELD2,0),FIELD3=ISNULL(FIELD3,0)  So CUT another update and set statement .

  • Can insert but cannot update table.

    Hi,
    I am finding trouble in something that I thought would be very easy. I have writen some code that simply updates some rows in one table (update table set column = value where critery; commit;). This table isn't associated to any Forms block. The code is properly executed except for forms 40401 exception, but I have dealt with it by changing the message level, as it was suggested in this forum.
    My problem is that in spite of getting no error the table isn't updated. I find this pretty weird. I thought it might be some problem related with database permissions, but the same command launched on PL/SQL Developer under the same database connection works fine. I have also checked that insert statement over the same table works properly.
    Do you have any idea?
    Thanks.

    Hi,
    Thank you very much for your replies. I finally managed to solve the issue and I must apologize for having posted this thread. After all, it was quite an easy solution, as I suspected. A data format question. In Spain, decimal numbers are written with comma - ',' and the data I was trying to update always consisted of numbers. These numbers were coming from character variables, and the problem was behind the types conversion. Things worked properly if I wrote UPDATE BUQUES B SET B.B_ESLORATOTAL=TO_NUMBER('12,23'), but they didn't when I used a variable p_Valor where '12,23' value was stored. So was done in my Forms procedure and it seems it was crashing although I didn't get any error.
    I finally managed solved the issue by using FORMS_DDL command and building up the update statement in a string. As soon as I checked what I was storing in this string I noticed the mistake.
    Again, thank you very much for your time.
    Regards,
    Fernando

  • Update table cepc_bukrs

    Hello Expert,
       Could you please tell me which t-code update table cepc_bukrs. We have some missing entries in this table.
    Regards,
    Amit

    Hi,
    When you create a "Profit center" using transaction KE51, there is a tab called "caompany code". Here you select which company code you want to profit center to assing to.
    Let me know if you have any question.
    Regards,
    RS

  • Update table ADRC

    Hi All
    I have to update table ADRC from another internal table of type ADRC.
    My question is by which Function Module i can put a lock on ADRC  and then release after update.
    Regards
    Jai

    <b>Lock table</b>
    CALL FUNCTION 'ENQUEUE_E_TABLE'
    EXPORTING
    tabname = table_name
    EXCEPTIONS
    foreign_lock = 1
    system_failure = 2
    OTHERS = 3.
    <b> Unlock Table</b>
    CALL FUNCTION 'DEQUEUE_E_TABLE'
    EXPORTING
    tabname = table_name
    chk this BAPI
    <b>BAPI_BUPA_ADDRESS_CHANGE</b>
    Message was edited by:
            Chandrasekhar Jagarlamudi

  • Updata table

    Hello to all, First time in here and recent with sql issues so not sure which forum ia had to choose.
    I need to create a query that updates
    table as follows:
    -- tables involved
    [Parts]
    [InternationalParts]
    [InternationalSuppliers]
    Important columns
    [Parts].[Type]
    [Parts].[InternationPartsID]
    [Parts].[SupplierNumber]
    [Parts].[SupplierName]
    [Parts].[SupplierLongName]
    [InternationalParts].[InternationalPartID]
    [InternationalSupplier].[Name]
    [InternationalSupplier].[LongName]
    [InternationalSupplier].[Number]
    Objective --
    - I need to find all International Parts from the [Parts] table
    that are not present in the [InternationalParts]
    And where the
    [Parts].[SuppliersName] <> [InternationalSuppliers].[Name] or
    [Parts].[SuppliersLongName] <> [InternationalSuppliers].[LongName]
    Then we want to update the [Parts].[SuppliersName] and
    [Parts].[SuppliersLongName] columns WITH the values respectively from
    [InternationalSuppliers].[Name] and [InternationalSuppliers].[LongName]
    that
    we found.
    TIPS:to know what are the Parts from the [Parts] table
    which are International types:
    [Part].[Type] = 1
    TIPS: [Parts] and
    [InternationalParts] are linked with [Parts].[InternationPartsID] =
    [InternationalParts].[InternalPartID]
    TIPS: [Parts] and
    [InternationalSuppliers] are linked with [Parts].[SupplierNumber] =
    [InternationalSupplier].[Number]
    ===================
    SQL SOLUTION
    so far ...
    ===================
    UPDATE [Parts]
    SET
    [Parts].[SupplierName] = [InternationalSupplier].[Name]
    [Parts].[SupplierLongName] = [InternationalSupplier].[LongName]
    FROM (
    SELECT [Parts].[SupplierName], [Parts].[SupplierLongName],
    [InternationalSupplier].[Name], [InternationalSupplier].[LongName]
    from
    [InternationalSupplier]
    INNER JOIN [Parts]
    ON
    [InternationalSupplier].[Number] = [Part].[SupplierNumber]
    WHERE
    [Part].[SupplierName] <> [InternationalSupplier].[ShortName]
    and
    [Part].[SupplierName] <>
    [InternationalSupplier].[LongName]
    QUESTION:
    I'm not sure how to
    and where to code the other part of the condition which "International Parts
    from the [Parts] table that ARE NOT PRESENT in the [InternationalParts]"
    and
    to know what are the Parts from the [Parts] table which are International
    types:
    [Part].[Type] = 1
    Thanks in advance for any valuable tips
    Sanchoniathon

    Hi, 
    First check the SELECT statement based on your provided logic if it is fine you can run the UPDATE statement. 
    SELECT
    Parts.SuppliersName
    ,InternationalSuppliers.Name
    ,Parts.SuppliersLongName
    ,InternationalSuppliers.LongName
    FROM
    dbo.Parts
    INNER JOIN dbo.InternationalSupplier ON Parts.InternationPartsID = InternationalParts.InternalPartID
    WHERE
    Parts.Type = 1
    AND (ISNULL(Parts.SuppliersName, '') <> ISNULL(InternationalSuppliers.Name, '') OR ISNULL(Parts.SuppliersLongName, '') <> ISNULL(InternationalSuppliers.LongName, ''))
    AND NOT EXISTS (SELECT 1 FROM dbo.InternationalParts WHERE Parts.InternationPartsID = InternationalParts.InternationalPartID)
    UPDATE statement:
    UPDATE Parts
    SET
    SuppliersName = (CASE WHEN ISNULL(SuppliersName, '') <> ISNULL(InternationalSuppliers.Name, '') THEN ISNULL(InternationalSuppliers.Name, '') END)
    ,SuppliersLongName = (CASE WHEN ISNULL(Parts.SuppliersLongName, '') <> ISNULL(InternationalSuppliers.LongName, '') THEN ISNULL(InternationalSuppliers.LongName, '') END)
    FROM
    dbo.Parts
    INNER JOIN dbo.InternationalSupplier ON Parts.InternationPartsID = InternationalParts.InternalPartID
    WHERE
    Parts.Type = 1
    AND (ISNULL(Parts.SuppliersName, '') <> ISNULL(InternationalSuppliers.Name, '') OR ISNULL(Parts.SuppliersLongName, '') <> ISNULL(InternationalSuppliers.LongName, ''))
    AND NOT EXISTS (SELECT 1 FROM dbo.InternationalParts WHERE Parts.InternationPartsID = InternationalParts.InternationalPartID)
    Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered.

  • "Error in updating Table J_1iexchdr"

    Hi ,
    When I am creating the excise invoice, It's terminated the program and gives the following error
    "Error in updating Table J_1iexchdr"
    What can be the reason?
    Regards
    Prem

    Hi prem
    Look if these threads can help
    RG23C Update Problem
    Problem in SAP Script
    billing document not creating
    return delivery
    ************Reward points if usefull**************

  • Error in updating table J_1PART2

    Hi,
       I am getting an error while saving the excise invoice as 'error in updating table J_1PART2'.
    What would have gone wrong?????? please help!!!
    Thanks ,
    Ramya

    Discussion moved from SAP ERP Sales and Distribution (SAP SD) to Internationalization and Unicode
    Please use Internationalization and Unicode for you CIN related queries

  • Error in updating table J_1IEXCHDR,J_1IEXCDTL in tcode J1IJ

    Hi,
    while cancelling excise invoice in Tcode J1IJ ,
    we are getting 'error in updating table J_1IEXCHDR,J_1IEXCDTL'.
    what is missing or tell the procedure to resolve the issue.
    Regards,

    Discussion moved from SAP ERP Sales and Distribution (SAP SD) to Internationalization and Unicode
    Please use Internationalization and Unicode for you CIN related queries

  • Bapi : BAPI_ALM_ORDER_MAINTAIN not updating table AFVU-USR08

    Hi,
    I have a requirement to update table AFVU-USR08.I am actualy dealing with tcode IW32/IW33 where in we need to update operation-->enhancement tab.I am using BAPI : BAPI_ALM_ORDER_MAINTAIN to do this.I am testing the BAPI directly( Not called in any program ),I can see a message like so and so order saved under notification,But there is no entry in table AFVU-USR08.
    If any one have come across this prob. and got the sol. Pl. share it with me.
    Your solution will be more helpful.
    Thanks,
    Bharani.

    Hi Ferry,
    1. I have also tried ur given code before BAPI_TRANSACTION_COMMIT.
        l_fname = 'SOBKZ'.
        l_fvalue = 'U'.
        CALL FUNCTION 'CO_BH_MOD_SINGLE_FIELDS'
          EXPORTING
            aufnr_imp = wa_meth-objectkey(12)
            field1    = l_fname
            value1    = l_fvalue.
    but this program is giving dump for only this particular Value saying that This is PROTECTED field.
    Can anyone help me out on How to Update Special Stock Indicator on Components Tab of Maintenance Order (IW32).
    If anyone has faced such type of problem & solved it then please let me also.
    2. Which BADI to use for Updating Special Stock Indicator when we press enter on Components tab in Transaction IW32 ?..
    Thanks in advance,
    Hope to get the solution soon from all SAP gurus.
    and help will be appriciated .
    Gaurav.

Maybe you are looking for

  • Dynamic TaskFlow Gets Refreshed on Catching a Contextual Event

    Hi, I am using Contextual events in my application. I have 2 task flows in my parent page. One TF is raising an event and the other is catching it. As my catching TF is a dynamic TF, I have specified the event map in it only. Everything is working as

  • Paragraph Alignment Issue

    This is probably a simple issue, but I just can't seem to figure it out. I have a caption with two lines on all the photos, On one caption I need it to be three lines, but I want The line that is usually second to stay in the same place without movin

  • Time Machine stopped backup

    Time Machine has stopped the backup procedure and claims that there is not sufficient space. The disk has 750 GB out of which 580 GB are used and 169 GB free. The error message claims 48,76 GB would be needed, but only 30,02 are available. I am reall

  • Logic Studio loses audio interface (RME Fireface 800)

    Have have fully installed Logic Studio 8.0.1 and the Fireface drivers. When i'm working with some audio programm (mostly waveburner and Logic) it can happen, that Leopard suddenly loses the interface and when I try shut the Fireface settings and Fire

  • Recursive BOM

    Dear Gurus How to calcualte Standard cost of a material (CK40N) through Recursive BOM What are the steps to be followed and what are the precautions to be taken Regards Bala