Insert & Update while Locking tables

Update & Insert
Is it possible for me to insert a record and update 2 fields in another record, whil locking the database tables for the process to take place?
If so, how?
Thanks
Pete

I don't know which database server you are using. But in MySQL, you can do something like this:
<?php
function get_next_number( $field = 'id' )
     $id = 0;
     //Lock the table for write
     mysql_query('LOCK TABLES name_of_table WRITE');
     $sql = 'SELECT '.$field.' FROM name_of_table ORDER BY '.$field.' DESC LIMIT 1';
     $query = mysql_query($sql);
     if ( mysql_num_rows($query) > 0 )
          $row = mysql_fetch_row($query);
          //Get the $field value
          $id = $row[0];
     //Unlock tables
     mysql_query('UNLOCK TABLES');
     return ++$id;
?>
I didn't test this function, but the idea is that
Check if you are connected to MySQL server
Regards,
Rafael

Similar Messages

  • XML PARASE and insert /update in corresponding tables

    Hi,
    I have a XML in which master details tables 3 tables.I want to separate element and insert/update in corresponding tables dynamically.
    Table invlovled in A,B,C
    Thanks
    Reena

    Reena
    The easiest way to do this is along the following lines..
    (1) register an XML Schema that describes the XML you wish to process with theh database.
    (2). create a relational view for the top level elements and for each repeating element (element where maxOccurs > 1 in the XML Schema)
    (3) Use 'insert as select' or other SQL type processing to update your relational tables from the contents of the view
    It is normally recommended that you add the following annotations to the XML Schema before registering it with the database
    (1) At the schema level set xdb:storeVarrayAsTable="true".
    (2) At the complexType level set xdb:maintainDOM="fasle".
    If you search the forum you will find a lot of examples of creating relational views of XML content stored in the XML database. Typically these examples will include key words like 'create or replace view' and 'table(xmlsequence(extract' or 'xmltable'.
    Hope this helps...
    -Mark

  • Inserts/Updates on replicated tables Logical Standby Database ??

    Hello all,
    We have a Logical standby database on 10.2.0.5. Can you please suggest if there is a way we can do data inserts/updates on replicated tables ?
    Can this be done by doing a alter database guard none; or alter session disable|enable guard; ?? Even if it completes succesfully , will this have any effect on replication later ?

    Hello all,
    We have a Logical standby database on 10.2.0.5. Can you please suggest if there is a way we can do data inserts/updates on replicated tables ?
    Can this be done by doing a alter database guard none; or alter session disable|enable guard; ?? Even if it completes succesfully , will this have any effect on replication later ?

  • Can not insert/update data from table which is created from view

    Hi all
    I'm using Oracle database 11g
    I've created table from view as the following command:
    Create table table_new as select * from View_Old
    I can insert/update data into table_new by command line.
    But I can not Insert/update data of table_new by SI Oject Browser tool or Oracle SQL Developer tool .(read only)
    Anybody tell me, what's happend? cause?
    Thankyou
    thiensu
    Edited by: user8248216 on May 5, 2011 8:54 PM
    Edited by: user8248216 on May 5, 2011 8:55 PM

    I can insert/update data into table_new by command line.
    But I can not Insert/update data of table_new by SI Oject Browser tool or Oracle SQL Developer tool .(read only)so what is wrong with the GUI tools & why posting to DATABASE forum when that works OK?

  • Using Case statement to insert,update,delete  the tables

    Hi All,
    I have to check the databse ,
    if it is developement then
    insert/update/delete values in tables;
    if it staging then
    insert/update/delete values in tables;
    if it is production then
    insert/update/delete values in tables;
    thers is function available to check the current database
    For doing the about i am trying to write CASE statement like this
    SELECT function,
    case
    when fun = 'developement' then insert into table1 values ('abcd','1234')
    when fun = 'staging' then insert into table1 values ('abcd','1234')
    when fun= 'production' then insert into table1 values ('abcd','1234')
    else null
    from dual
    its throughing me an error
    please help
    Thanks,

    Hi,
    You can use CASE staement any place where an expression is expected.
    For example, in:
    UPDATE  table_a
    SET     col1 = exp1
    ,       col2 = exp2
    WHERE   exp3 = exp4;all the expressions are labled lke expn.
    Note that table_a, col1 and col2 are not expressions: you must hard-code these names, or use dynamic SQL.
    So it's okay to say:
    UPDATE  table_a
    SET     col1 = CASE
                       WHEN  db = 'development'  THEN  0
                       WHEN  db = 'staging'      THEN  1
                   END
    ,       col2 = CASE
                       WHEN  db = 'development'  THEN  NULL
                       WHEN  db = 'staging'      THEN  col2
                   END
    WHERE   db != 'production';In this example:
    in the development database, col1 is set to 0 and col2 is set to NULL
    in the staging database, col1 is set to 1 and col2 is unchanged (that is, set to what it already was)
    in the production database, nothing is changed (the WHERE condition is always FALSE)

  • Interfaces used for insert/update data in tables

    for providing security the procedures are not allowed to directly insert/update the values in tables.some one suggested that use interfaces for this .i want infomation about this interfaces. how this works and how to use

    An interface is an intermediate set of procedures/functions that do the job for you.
    You haven't explained what it is you're trying to achieve exactly, but here's an example scenario.
    We don't want to allow our developers to query, or write to the tables directly so they are set up with certain database users that have no permissions granted on the tables for such things.
    Instead they have been granted permission to call procedures and functions belonging to another schema, and it is those procedures and functions that do the required table access and/or pass information/data back or write to the tables.
    The developers have to write their applications to call these procedures to do any access to the tables. This means that they are limited to the functionality provided by these interface procedures so, if there is internal auditing or columns of data etc. on the tables then it may be that they can't access this data, but only the "user facing" data.
    The interface can also ensure that data meets certain requirements before being written to tables, allowing for business or technical level restrictions to be implemented.

  • Fetch Insert / Update Query From Table Trigger

    Hi everyone !,
    I have a situation, is there any way where I can get insert/update query by before-insert / after insert trigger when a user inserts/updates any row in the table.
    Plz....help me....champs.....
    Regards,
    Naushad

    That was a nice thing but it works only on some oracle 9i with DML statements.
    On database versions 9.2.0.1 to 9.2.0.6 ora_sql_text works and returns the calling text for dml triggers, where as starting from 9.2.0.7 the behavior has changed and returns NULL.
    Cause
    This issue had surfaced from 9.2.0.7.0 patchset. After discussions in Bug 4171597 which was closed as a duplicate of Bug 4230721 it was concluded that it was the expected behavior i.e ORA_SQL_TXT should return null when dml triggers are used.
    ORA_SQL_TXT is a "System defined event attribute" and is supposed to work only with "System triggers". This is also what the Documentation says - "Application Developers Guide : Fundamentals(9.2)
    Chapter 16 Working with System events".
    Bye Alessandro

  • Insert / update in same table

    SQL> select * from test;
    TT NAME
    1 hi
    1 re
    1 go
    2 rw
    2 gh
    2 lk
    6 rows selected.
    This is just a dummy table,but I would like to know how can I update the values of TT=2 to make it identical with the values of TT=1
    The table should look like
    SQL> select * from test;
    TT NAME
    1 hi
    1 re
    1 go
    2 hi
    2 re
    2 go
    In reality the records go into lacs

    One way:
    (untested)
    Delete from Test where TT=2;
    Insert into test t1
    select 2, t2.name from test t2 where tt=1;Added where clause, just in case if there are more values for TT.
    Message was edited by:
    Sven W.

  • Oracle Spatial - inserts/updates to geometry tables

    I am getting the following errors when inserting into an SDO_GEOMETRY object type:
    java.sql.SQLException: ORA-29877: failed in the execution of the ODCIINDEXUPDATE routine
    ORA-13000: dimension number is out of range
    ORA-06512: at "MDSYS.SDO_INDEX_METHOD", line 8
    ORA-06512: at "MDSYS.SDO_IDX", line 86
    ORA-06512: at "MDSYS.SDO_INDEX_METHOD", line 83
    ORA-06512: at line 1
    I have looked in the Oracle Spatial User Guide for 8i and have not been able to find much on the information above.
    Any help from inviduals with more expertise in this area would be great. If I need to provide more information please do indicate this as well
    Thanks,
    Faroon.

    Hi,
    The number of dimensions in the object being inserted may be
    out of the valid range ( 2 to 4).
    Or the diminfo in the user_sdo_geom_metadata for this
    layer has been changed the after the index is created.
    Check these two places first to make sure the dimensionality
    is in the valid range.
    The error is raised on index updated when the dimensionality
    of the object being updated is not in the valid range.
    siva

  • How to update two database tables when using DatabaseProvider

    I'm a begginer and I've made an application which uses a database. In one page, I have a table which I've took it from "woodstock basic" palette. I was using DatabaseProvider to populate the table with data from a database table called "copii". My code is:
    //i'm check all the rows in the table to see if the field "IDCOPIIC" =0 and if true I will put the value of "newCopiiId"
    do {
    if (copiiDataProvider.getValue("IDCOPIIC").equals
    (new Long(0))) {
    copiiDataProvider.setValue("IDCOPIIC", new Long(newCopiiId));
    newCopiiId++;
    } while (copiiDataProvider.cursorNext());
    copiiDataProvider.commitChanges();
    My problem comes now because I have another database table tided with a foreign key by this one and I want to update this one too when I'm updating the "copii" table. I don't know how to do it ...I've tried in many ways but is impossible. Can anyone help me ! I'm desperate...I cannot figure it out how I could insert, update in two tables in the same time when I'm using DataProvider .
    Thanks in advance!

    Hmm.
    Not sure where your going with the <select> menu.
    The description you made:
    "Both have an id column, which is presumably the record's primary key, but you need to create a relationship by storing the primary key of one table as a foreign key in the child (related) table. For example, you should insert the primary key of the first table in the second table as user_id. That enables you to identify which username and password are related to an individual listed in the second table"
    Seems to be the direction that I'm looking for.
    Now my question is how do I insert the primary key of the first table in the second table as user_id?
    >
    >"For example, you should insert the primary key of the first table in the second table as user_id"
    Since both tables have an id column and they both are the primary key, do I just change the name of the second tables id column to "user_id" then?
    Or is there an actual "insert the primary key to another table" function that I don't know about?

  • SQL SERVER BULK FETCH AND INSERT/UPDATE?

    Hi All,
           I am currently working with C and SQL Server 2012. My requirement is to Bulk fetch the records and Insert/Update the same in the other table with some  business logic?
           How do i do this?
           Thanks in Advance.
    Regards
    Yogesh.B

    > is there a possibility that I can do a bulk fetch and place it in an array, even inside a stored procedure ?
    You can use Temporary tables or Table variables and have them indexes as well
    >After I have processed my records, tell me a way that I will NOT go, RECORD by RECORD basis, even inside a stored procedure ?
    As i said earlier, you can perform UPDATE these temporary tables or table variables and finally INSERT/ UPDATE your base table
    >Arrays are used just to minimize the traffic between the server and the program area. They are used for efficient processing.
    In your case you will first have to populate the array (Using some of your queries from the server) which means you will first load the arrary, do some updates, and then send them back to server therefore
    network engagement
    So I just gave you some thoughts I feel could be useful for your implementation, like we say, there are many ways so pick the one that works good for you in the long run with good scalability
    Good Luck! Please Mark This As Answer if it solved your issue. Please Vote This As Helpful if it helps to solve your issue

  • RFC calls with SAP table insert - lock tables

    Hi,
    I have an external server program, which is calling a rfc function many times the same time with different transaction types. There are transaction types, which are downloading information from SAP (only send data back to the caller), and there are transaction types which are uploading data to SAP (inserting/updating data on SAP).
    The function calls are synchronous as the server needs to get feedback directly. The parallel work processes for RFC calls are limited, so a system overload can´t happen.
    The problem now is, if the server is down for while and going to be restarted, it´s opening parallel threads and calls the function in SAP at nearly the same time. That means, it will call the function with upload requests (table must be locked), and download requests the same time.
    In the upload requests, the table has to be locked, that it can´t occur that we get wrong entries...
    I can only enqueue the table with just two fields... I know, that is almost like locking the whole table, but it´s not possible to lock it in a different way.
    I tried to use the enqueue function with the parameter WAIT, but it didn´t help, as there where too many parallel calls and after some seconds, they were ending up with an error (Because the table has been locked by another call).
    It seems that it´s trying to lock the table again for all parallel calls at exactly the same time....
    The calls has to be synchronous, as the server needs to get the feedback directly. Any ideas how top solve this, that it handles all incoming calls parallel and waits for the table is unlocked again?
    Thank you for your help!

    Hi,
    thank you for your answer!
    I have investigated something new yesterday:
    I thought at the beginning, that the problem just occurs, when the system work processes are reached. The system has for example set up 15 dialog processes and the external server is calling the function 20 times in parallel. Then we normally need 5 dialog processes more. The system is taking then all 15 dialog processes and the locks got stucked.
    That means, that I´ll maybe only get 4 or 5 uploads, which have really been updated the tables. All the others couldn´t get the table locked for their process.
    But if I now let the server call the function just about 13 times in parallel, nearly all uploads have been updated the table!
    From my point of view, the lock from SAP gets stucked, when the limit of dialog processes is reached. It´s not working in the right way anymore...
    The same is happening, when you set up in the system, that only 10 work proecesses can be used by RFC. If I have more than 10, it´s going to be critical with the locks....
    WEIRD!
    Can anybody help me out of trouble?
    Thank you!

  • Getting Error While inserting a record in table PDSNR through Tcode PA30

    Hi Experts,
    Im doing a transaction in  TCode PA30 by entering a pernr and infty as 14 and wage type.
    My need is when im entering a particular wage type in PA30 with infotype 14 , for that particular wage type and infotype i need to assign a different cost centre. This requirement was doing now manually by changing the cost centre form EDIT - > Maintain Cost Assignment.
    But my Client Needs to do it automatically. by comparing the wage type and infotype the cost centre needs to get automatically updated.
    So i use the Enhancement EXIT_SAPFP50M_002 * INCLUDE ZXPADU02.* and wrote coding like below
    CASE innnn-infty.
    To check whether the record belong to pa0014
    If so,check the wage type for the updation
    If so update the values of cost center assignments in the table assob and asshe
    when '0014'.
    if innnn-subty eq '2211'.
    Select the last updated sequence number in the table pdsnr
    if sy-ucomm eq 'UPD' or sy-ucomm eq 'INS'.
    select single * from pa0001 into wa_0001 where pernr eq INNNN-pernr and
                                                   endda eq '99991231'.
      if sy-subrc = 0.
    call function 'ENQUEUE_EPPRELE'
    EXPORTING
       MODE_PREL            = 'E'
       MANDT                = SY-MANDT
       PERNR                = INNNN-pernr
       INFTY                = INNNN-INFTY
       SUBTY                = INNNN-SUBTY
    EXCEPTIONS
       FOREIGN_LOCK         = 1
       SYSTEM_FAILURE       = 2
       OTHERS               = 3
    if sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    endif.
    CLEAR : lv_pdsnr.
    select MAX( pdsnr ) from pdsnr into lv_pdsnr.
    lv_pdsnr = lv_pdsnr + 1.
        ls_assob-pdsnr = lv_pdsnr.
        ls_assob-pernr = INNNN-pernr.
        ls_assob-bukrs = wa_0001-bukrs. "'1001'.
        ls_assob-gsber = wa_0001-gsber. "'0001'.
        ls_assob-kokrs = wa_0001-kokrs. "'1000'.
        ls_assob-kostl = '1-1361'.
        ls_asshr-pdsnr = lv_pdsnr.
        ls_asshr-pernr = INNNN-pernr.
        ls_asshr-infty = innnn-infty.
        ls_asshr-subty = innnn-subty.
        ls_asshr-begda = sy-datum.
        ls_asshr-endda = '99991231'.
        ls_PDSNR-pdsnr = lv_pdsnr.
        ls_PDSNR-PDGRP = '5B'.
    insert into pdsnr values ls_pdsnr.
    insert into asshr values ls_asshr.
    insert into assob VALUES ls_assob.
    call function 'DEQUEUE_EPPRELE'
    EXPORTING
       MODE_PREL       = 'E'
       MANDT           = SY-MANDT
       PERNR           = INNNN-pernr
       INFTY           = INNNN-INFTY
       SUBTY           = INNNN-SUBTY.
        ENDIF.
    ENDIF.
    ENDIF.
    COMMIT WORK.
    ENDCASE.
    This is to update the three tables PDSNR, ASSOB,ASSHR.
    Its getting Updated correctly, But when i goto EDIT - > Maintain Cost Assignment to do some changes and while saving the data i get the following Error Message :
    You Cannot insert data in table : PDSNR.
    Message no. P2715
    Can Anyone please suggest me how to solve this issue.
    Thanks,
    Vijayan.R

    Hi. Were you able to fix the error?

  • Insert & update nested table

    Hi
    I have created one nested table with the follwing columns. I have to insert records into the nested table. And also I have to update the nested table. Please find below my table and getting error message.
    Please advose...!!
    SQL> create type details as object(
      2  basic number(7,2),
      3  da number(6,2),
      4  hra number(6,2),
      5  pf number(6,2),
      6  it number(6,2),
      7  gross number(7,2),
      8  ded number(6,2),
      9  net number(8,2));
    10  /
    Type created.
    SQL> create type details_t is table of details;
      2  /
    Type created.
    SQL> ed
    Wrote file afiedt.buf
      1  create table emp_tab(empno number(4),name varchar2(10),details_tab details_t)
      2* nested table details_tab store as empl_details
    SQL> /
    Table created.
    SQL> ed
    Wrote file afiedt.buf
      1* insert into emp_tab values(&empno,'&name',details_t(details(&da,&hra,&pf,&it,null,null,null)))
    SQL> /
    Enter value for empno: 1
    Enter value for name: asdf
    Enter value for da: 120
    Enter value for hra: 130
    Enter value for pf: 120
    Enter value for it: 120
    old   1: insert into emp_tab values(&empno,'&name',details_t(details(&da,&hra,&pf,&it,null,null,null
    new   1: insert into emp_tab values(1,'asdf',details_t(details(120,130,120,120,null,null,null)))
    insert into emp_tab values(1,'asdf',details_t(details(120,130,120,120,null,null,null)))
    ERROR at line 1:
    ORA-02315: incorrect number of arguments for default constructorCan I use '&' while inserting records into nested table? yes / no ?
    I have to update gross, ded, net columns also..!!
    Please help me..!!
    Regards
    A

    zep@dev>
    zep@dev> create type details as object
          2  (
          3      basic number(7, 2),
          4      da    number(6, 2),
          5      hra   number(6, 2),
          6      pf    number(6, 2),
          7      it    number(6, 2),
          8      gross number(7, 2),
          9      ded   number(6, 2),
         10      net   number(8, 2)
         11  )
         12  /
    Type created
    zep@dev> create type details_t is table of details
          2  /
    Type created
    zep@dev> create table emp_tab(empno number(4),name varchar2(10),details_tab details_t)
          2     nested table details_tab store as empl_details
          3  /
    Table created
    zep@dev> insert into emp_tab
          2  values
          3      (1,
          4       'asdf',
          5       details_t(details(120, 130, 120, 120, 1, 2, 3, 4)));
    1 row inserted
    zep@dev>
    zep@dev>   select *
          2      from table (select details_tab
          3                    from emp_Tab t
          4                   where t.empno = 1);
        BASIC       DA      HRA       PF       IT     GROSS      DED        NET
       120,00   130,00   120,00   120,00     1,00      2,00     3,00       4,00
    zep@dev> -- second object in the same empno = 1
    zep@dev>    insert into table
          2         (select details_tab
          3            from emp_Tab t
          4           where t.empno = 1)
          5     values
          6         (details(200, 230, 220, 220, 10, 11, 12, 13));
    1 row inserted
    zep@dev>
    zep@dev> select *
          2    from table (select details_tab
          3                  from emp_Tab t
          4                 where t.empno = 1);
        BASIC       DA      HRA       PF       IT     GROSS      DED        NET
       120,00   130,00   120,00   120,00     1,00      2,00     3,00       4,00
       200,00   230,00   220,00   220,00    10,00     11,00    12,00      13,00
    zep@dev>
    zep@dev> update table (select details_tab
          2                  from emp_Tab t
          3                 where t.empno = 1)
          4     set gross = nvl(basic,0) + nvl(da,0) + nvl(hra,0),
          5         ded   = nvl(pf,0) + nvl(it,0),
          6         net   = nvl(gross,0) - nvl(ded,0)
          7   where basic = 120;
    1 row updated
    zep@dev>
    zep@dev> select *
          2    from table (select details_tab
          3                  from emp_Tab t
          4                 where t.empno = 1);
        BASIC       DA      HRA       PF       IT     GROSS      DED        NET
       120,00   130,00   120,00   120,00     1,00    370,00   121,00      -1,00
       200,00   230,00   220,00   220,00    10,00     11,00    12,00      13,00
    zep@dev>

  • Lock table for update in adf

    dear all ,
    am using Jdeveloper 11.1.1.3 with oracle database 10g,
    now what i need to do is when inserting a new row in one of my entities i need to lock the entire database table until the commit happen...
    i'll explain to u why I need to do this,,, let say that the table x contains the following columns
    x_pk number, ( which is the primary key )
    x_serial number,
    x_type varchar2 );
    now am getting x_pk from a sequence on a database so its not a problem,
    but for x_serial it needs to be serialized per x_type , now in oracle forms developer i used to do the following in per-insert trigger on the block level-->
    lock table for update then selecting the max serial per the type then use it for setting the :x_serial that is in the same block...
    now in ADF , i should do this in the do_dml when the operation==DML_INSERT but am not sure what to write and if it will be working same as it was in the form developer
    thanks ,
    Lama

    Delta,
    I wrote a [url http://stegemanoracle.wordpress.com/2006/03/15/using-updatable-views-with-adf/]blog post some time ago for an earlier version of JDeveloper that implemented some custom locking behaviour for EOs - perhaps you can adapt the technique there to your needs?
    John

Maybe you are looking for

  • In FAX & Print of PO attachment not coming

    Hi all, We have designed our own PO Smartform. Whatever doc we are attaching in SC for vendor is going in the email with PO. But when sending the PO by FAX or by Print media, the attachment is not coming. Any idea how to achieve this. Regards, Smita

  • IDOC to multiple file with dynamic file names with same content

    Hi, My scenario is from IDOC to file. The number of files which i need to create is dynamic, which depends on the number of plants. I have a Z-table where i will maintin list of all plants ( non SAP plant names). I need to fetch the data from this z-

  • Set Windows Time Zone

    I am trying to write a VI that will set the system time zone (in Windows). The user needs to be given the option to pick which time zone they are in. Then whatever changes are needed, must be written back out to Windows. I want Windows to handle all

  • Giving a component with incremental color range in CSS

    Is there a way to use the background-color : #E6E5E5 to #F0F0F0 ; which fills my component from #E6E5E5  then incrementing till #F0F0F0. i can achieve this using a similar type image as background, but i need to know is that possible in CSS. Pls help

  • Pop up from Apple won't disappear

    I recently got a new iPod touch 4th generation.  I backed up my iCloud onto it and a pop-up came up saying "Your iPod touch's setting have been restored.  Your purchased apps and media will now be automatically downloaded".  I tried pressing "OK" to