Delete and update records in Stored procedures

I AM NOW STUDYING A STORED PROCEDURE ,AND I ENCOUNTER SOME PROBLEM WITH
UPDATE AND DELETE SOME RECORD WHICH ONE IS UPDATED OR DELETED,
THIS IS MY CODE:
PACKAGE table_of_array IS
type emp_rec is record(
     empno emp.empno%type,
     ename emp.ename%type,
     sal          emp.sal%type,
     deptno emp.deptno%type
type arr is table of emp_rec
index by binary_integer;
procedure get_data(a in out table_of_array.arr);
procedure do_insert(a in out table_of_array.arr);
procedure do_update(a in out table_of_array.arr);
procedure do_delete(a in out table_of_array.arr);
END;
PACKAGE BODY table_of_array IS
procedure get_data(a in out table_of_array.arr)
is
i number:=0;
begin
     for cur in (select empno,ename,sal,deptno from emp) loop
          a(i):=cur;
          i:=i+1;     
     end loop;
end;
procedure do_insert(a in out table_of_array.arr)
is
     i number:=0;
     cnt number:=a.count;
begin
     for i in 1..cnt loop
          insert into emp(empno,ename,sal,deptno)
          values(a(i).empno,a(i).ename,a(i).sal,a(i).deptno);
     end loop;
end;
procedure do_update(a in out table_of_array.arr)
is
begin
     /* WHICH RECORD AND ITEM HAD BEEN UPDATED */
end;
     procedure do_delete(a in out table_of_array.arr)
     is
     begin
     /* WHICH RECORD HAD BEEN DELETED */      
     end;
END;

when you want to update, delete or insert records with a stored procedure you have to provide a procedure that locks the records
i'll send you an example of an update procedure and a lock procedure
PROCEDURE p_upd_devices ( resultset IN devtab) IS
BEGIN
FOR i IN 1..resultset.COUNT LOOP
          IF resultset(i).zone <> resultset(i).zone_ref THEN
          p_update_zone(resultset(i).zone, resultset(i).mdv_seq);
          END IF;
     END LOOP;
EXCEPTION WHEN OTHERS THEN
mecoms_general.logging(0, sqlerrm, 'pck$fill_error_devices.p_upd_devices');
END; -- p_upd_devices
PROCEDURE p_update_zone (pi_zone in metering_devices.zne_code%type, pi_device in metering_devices.seq%type) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
          UPDATE metering_devices
               SET zne_code = pi_zone
               WHERE seq = pi_device;
               commit;
EXCEPTION WHEN OTHERS THEN
mecoms_general.logging(0, sqlerrm, 'pck$fill_error_devices.p_update_zone');
END;
PROCEDURE p_lock_devices( resultset IN OUT devtab) IS
BEGIN
null;
END;

Similar Messages

  • Deleting and updating records in a database table

    dear all ,
    i have created a databse table to which i have to update and delete records thru my program whixh i am unable to do so plz help me.

    Hi Sonu,
    To delete and update the records in your database table, you can create a Function Group and all the necessary function modules for it. This will a good approach as this will separate the database interface logic and the business logic. You need to use the DELETE and UPDATE ABAP keywords to delete and update the records of the database tables. Have a look at the ABAP Keyword documentation for a complete details of the usage.
    Have a look at the following link:
    DELETE
    http://help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3aef358411d1829f0000e829fbfe/frameset.htm
    UPDATE
    http://help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3aef358411d1829f0000e829fbfe/frameset.htm
    Hope this will help.
    Thanks,
    Samantak.

  • How can I  delete and update records using where conditions?

    I want to delete and update the coherence records with some conditions, I describe it to use SQL as follows:
    delete from "contacts" where getStreet() = "dsada";
    update contacts set getStreet() = "dddd" where getCity() = "ssss";
    Can I use the filter like query to achieve this requirement as follows:
    ValueExtractor::View vHomeStateExtractor = ChainedExtractor::create(
    ChainedExtractor::createExtractors("getHomeAddress.getState"));
    Object::View voStateName = String::create("MA");
    Set::View setResults = hCache->entrySet(
    EqualsFilter::create(vHomeStateExtractor, voStateName));
    I know I can use get and put to achieve this requirement , but it Requires a two-interaction between the client and coherence server. Does it have And another way?
    Thanks very much, and please Forgive my English is not very good.

    Hi,
    You have a couple of options for updating or deleting using a Filter.
    For deleting you can use an Entry Processor and the cache invokeAll method. Using "out of the box" Coherence you can use the ConditionalRemove entry processor. I'm a Java person so the C++ below might not be exactly right but you should get the idea.
    ValueExtractor::View vHomeStateExtractor = ChainedExtractor::create(
    ChainedExtractor::createExtractors("getHomeAddress.getState"));
    Object::View voStateName = String::create("MA");
    hCache->invokeAll(EqualsFilter::create(vHomeStateExtractor, voStateName),
    ConditionalRemove::create(AlwaysFilter.getInstance());For update you would either need to write custom Entry Processor implementations that perform the updates you require or you can use out of the box POF or Reflection ValueUpdaters that update specific fields of the entries in the cache. These valueUpdaters would be wrapped in an UpdaterProcessor so the call would be very similar to the code above.
    JK

  • Drop and recreate table in stored procedure

    Hi all
    When creating tables using Transact-SQL scripts, I have always preferred to drop the table if it exists and then create it explicitly using CREATE TABLE.  For two reasons:
    1) It does not matter if it is the first time the SP is run ie. if I create the table manually in the first instance and just use TRUNCATE TABLE it could fail if the table is deleted
    2) I have control over the data types of the table fields
    Just recently though I discovered the error that can occur when dropping and creating a table in the same batch (see link below)
    Microsoft Website
    This causes me a problem when dropping and creating tables in stored procedures, as I understand that a stored procedure is in itself a single batch?
    Can I avoid this error in a stored procedure whilst continuing to drop and create tables?  Or should I be taking a different approach?
    Coding best practice advice would be greatly appreciated.
    Thank you

    Thanks Ronen
    Please see my second post immediately before your reply.
    Given that I need to store the data output in a physical table for use in QlikView, would you suggest truncating the table each time the SP runs?  And then having a script that handles both dropping and creating the physical table, and also creating
    the SP?
    >> QlikView
    QlikView is an Israeli company, right?
    In any case I am not familiar with QlikView's application, therefore I can only give you general information, based on assumptions regarding the application, and facts regarding the SQL Server.
    >> for use in QlikView
    I assume that external application use specific database structure (table
    structure) and it is change only in rare situations (for example CMS interface might change the tables if and when a module s update/install). In this case there is no need to drop the table and recreate it and TRUNCATE is the solution.
    >> would you suggest truncating the table each time the SP runs
    I am sorry but i cant recommend on TRUNCATE a table each time you execute SP, without know the exact reason for this logic. It sound to me, at this point of time (with the information that we have), that this
    is very bad logic (application architecture). As I wrote above, basing your application on TRUNCATING the table each time mean that you have problems with multi users. Thins about 2 people that try to execute the same SP at almost the same time. Think about
    locking in the SQL Server and bad data (one truncate while the other already inserted the new data and get no rows, if there is no locking).
    But TRUNCATE is much better in this case probably then DROP and DELETE, since it is faster, and locking will be shorter (hopefully the application use the correct locking). There are other reasons why TRUNCATE is better, and other people already mentioned
    most of them, but time in this scenario might be critical.
    >> having a script that handles both dropping and creating the physical table, and also creating the SP?
    I do not undestand what is this second step. we said that you truncate the table, so why do you need to
    dropping and creating the physical table and who
    creating the SP?
    Are you meaning that the application create the tables and SP?
    There are lot of application that during installation create the database structure. is this what you mean?
      Ronen Ariely
     [Personal Site]    [Blog]    [Facebook]

  • Insert record using stored procedure

    Dear all,
    I want to insert record using stored procedure in form6i,
    any help or suggestion will be appreciated.
    regards
    Kashif Ali

    Kashif,
    Could you please explain what you are trying to accomplish? Inserting records into a table from Forms is as simple as executing an INSERT statement in a Forms trigger or as complicated as overriding the Forms default INSERT functionality by defining your own On-Insert trigger. Your requirements will dictate where and how you do your INSERT.
    Craig...

  • Need help to insert and update records in MDM

    Hi ,
    I am trying to develop an webdynpro application which can create and update records in tables of a repository of MDM . For example .. I want to insert values and later update values in Vendor table.
    I am new to webdynpro and MDM. If any one can help step by step or can send a sample code which I can be ready to use that would be great help.
    If anyone can have a sample code .. kindly mail to "[email protected]"
    It is urgent. Please help.
    Regards,
    Niraj
    Edited by: Niraj Kumar on May 23, 2008 6:50 AM

    Hi Niraj,
    Are u going to work with webdynpro Java/ABAP?
    some materials which are found useful are sent.
    Cheers,
    Mary

  • Insert,  Delete and Update options in Table control

    Experts,
    I have writen code for Insert,  Delete and Update options in Table control. They are not working properly...
    can any one send the code for the above please...
    Thanks in advance..

    Hi,
    Following steps will help you.
    1.TOP-INCLUDE
    DATA: ITAB1 LIKE KNA1 OCCURS 0 WITH HEADER LINE.
    DATA: ITAB2 LIKE KNA1 OCCURS 0 WITH HEADER LINE.
    DATA: WA LIKE KNA1.
    DATA: ANT TYPE I,CUR TYPE I.
    DATA: OK_CODE TYPE SY-UCOMM.
    CONTROLS: TABCTRL TYPE TABLEVIEW USING SCREEN 100.
    IN FLOWLOGIC
    PROCESS BEFORE OUTPUT.
    LOOP AT ITAB1 CURSOR CUR WITH CONTROL TABCTRL.
    ENDLOOP.
    PROCESS AFTER INPUT.
    MODULE CLEAR_DATA.
    LOOP AT ITAB1.
    MODULE MOVE_DATA.
    ENDLOOP.
    ADD “OK_CODE” IN ELEMENT LIST. CLICK ON LAYOUT AND  ADD TABLE CONTROL(name it as TABCTRL) AND PUSHBUTTONS AS FOLLOWS.
    SELECT THE FIELDS FROM PROGRAM. SAVE CHECK AND ACTIVATE.
    CLICK ON FLOWLOGIC EDITOR FROM APPLICATION TOOL BAR.
    DOUBLE CLICK ON MODULE “CLEAR_DATA”.
    write the in this module as below.
    CLEAR ITAB2. REFRESH ITAB2.
    DOUBLE CLICK ON MODULE “MOVE_DATA”.
    write the code in this module as below.
    APPEND ITAB1 TO ITAB2.
    ACTIVATE PAI AND WRITE THE CODE AS BELOW.
    CASE OK_CODE.
    WHEN 'FETCH'.
    SELECT * FROM KNA1 INTO TABLE ITAB1 UP TO 20 ROWS.
    TABCTRL-LINES = SY-DBCNT.
    WHEN 'ADD'.
    GET CURSOR LINE CNT.
    CNT = TABCTRL-TOP_LINE + CNT - 1.
    CLEAR WA.
    INSERT WA INTO ITAB1 INDEX CNT.
    WHEN 'MODIFY'.
    GET CURSOR LINE CNT.
    READ TABLE ITAB2 INDEX CNT.
    LOOP AT ITAB2.
    MODIFY KNA1 FROM ITAB2.
    ENDLOOP.
    SELECT * FROM KNA1 INTO TABLE ITAB1.
    WHEN 'EXIT'.
    LEAVE PROGRAM.
    ENDCASE.
    SAVE,CHECK AND ACTIVATE ALL.
    CREATE TCODE AND EXECUTE.
    contact if u hv any issues regarding this code.
    reward points,if it is useful.
    Thanks,
    Chandu.

  • Go to detail page and update record at the same time

    Hi guys,
    I`m very new to dreamweaver and have no coding skills, so
    please be gentle.
    I`m have created a mail box and it all works fine, the
    problem i`m having is that I want to be able to tag wether a
    message has been read or not.
    So the subject has to be a link so when someone clicks the
    subject you are redirected to the read message page and it displays
    the message in full.
    My question is there a way to go to detail page and update
    record at the same time?
    I`m using ASP and the mysql read or not field is a SET

    "mrmidjam" <[email protected]> wrote in
    message
    news:fghrpf$7s2$[email protected]..
    > Hi guys,
    >
    > I`m very new to dreamweaver and have no coding skills,
    so please be
    > gentle.
    >
    > I`m have created a mail box and it all works fine, the
    problem i`m having
    > is
    > that I want to be able to tag wether a message has been
    read or not.
    >
    > So the subject has to be a link so when someone clicks
    the subject you are
    > redirected to the read message page and it displays the
    message in full.
    >
    > My question is there a way to go to detail page and
    update record at the
    > same
    > time?
    You can use the Update Record ZerverBehavrior and then have
    it redirect to
    the detailpage.
    Joris

  • Update primary key/delete and reinsert records

    Hi, I have been told to update some fields. The database is new to me, and now I see the field is the primary key (and it has child tables with that as foreign key). The person who told me isn't reachable until next week...
    ...wasn't a bad idea to update pk's? I see it's possible, deferring constraints...
    update primary key
    Re: Update Primary Key
    ...but it's better/safer the script deletes and later re-insert the records, right?
    Thanks

    Always is better be able to undo any change .
    But ¡, why would you update pk's? it's possible but you colud find few problems like:
    - many levels of foreign keys exists.
    - not validate pk
    Look at:
    Re: Novalidate primary key

  • Return records from Stored Procedure to Callable Statement

    Hi All,
    I am createing a web application to display a students score card.
    I have written a stored procedure in oracle that accepts the student roll number as input and returns a set of records as output containing the students scoring back to the JSP page where it has to be put into a table format.
    how do i register the output type of "records" from the stored function in oracle in the "registerOutParameter" method of the "callable" statement in the JSP page.
    if not by this way is there any method using which a "stored function/procedure" returning "record(s)" to the jsp page called using "callable" statement be retrieved to be used in the page. let me know any method other that writing a query for the database in the JSP page itself.

    I have a question for you:
    If the stored procedure is doing nothing more than generating a set of results why are you even using one?
    You could create a view or write a simple query like you mentioned.
    If you're intent on going the stored procedure route, then I have a suggestion. Part of the JDBC 2.0 spec allows you to basically return an object from a CallableStatement. Its a little involved but can be done. An article that I ran across a while back really helped me to figure out how to do this. There URL to it is as follows:
    http://www.fawcette.com/archives/premier/mgznarch/javapro/2000/03mar00/bs0003/bs0003.asp
    Pay close attention to the last section of the article: Persistence of Structured Types.
    Here's some important snippets of code:
    String UDT_NAME = "SCHEMA_NAME.PRODUCT_TYPE_OBJ";
    cstmt.setLong(1, value1);
    cstmt.setLong(2, value2);
    cstmt.setLong(3, value3);
    // By updating the type map in the connection object
    // the Driver will be able to convert the array being returned
    // into an array of LikeProductsInfo[] objects.
    java.util.Map map = cstmt.getConnection().getTypeMap();
    map.put(UDT_NAME, ProductTypeObject.class);
    super.cstmt.registerOutParameter(4, java.sql.Types.STRUCT, UDT_NAME);
    * This is the class that is being mapped to the oracle object. 
    * There are two methods in the SQLData interface.
    public class ProductTypeObject implements java.sql.SQLData, java.io.Serializable
        * Implementation of method declared in the SQLData interface.  This method
        * is called by the JDBC driver when mapping the UDT, SCHEMA_NAME.Product_Type_Obj,
        * to this class.
        * The object being returned contains a slew of objects defined as tables,
        * these are retrieved as java.sql.Array objects.
         public void readSQL(SQLInput stream, String typeName) throws SQLException
            String[] value1 = (String[])stream.readArray().getArray();
            String[] value2 = (String[])stream.readArray().getArray();
         public void writeSQL(SQLOutput stream) throws SQLException
    }You'll also need to create Oracles Object. The specification for mine follows:
    TYPE Detail_Type IS TABLE OF VARCHAR2(1024);
    TYPE Product_Type_Obj AS OBJECT (
      value1  Detail_Type,
      value2 Detail_Type,
      value3 Detail_Type,
      value4 Detail_Type,
      value5 Detail_Type,
      value6 Detail_Type,
      value7 Detail_Type,
      value8 Detail_Type);Hope this helps,
    Zac

  • Sender JDBC adapter : Update SQL Statement : stored procedure

    Hi,
    Can we use a stored procedure in the sender jdbc adapter in 'Update SQL Statement'.
    The problem i am facing is like, we are selecting data from two tables in 'SQL statement for query' and then in 'Update SQL Statement' , we need to delete that data from these two tables.
    Please let me know if it is possible.
    Thanks,
    Rohit

    you can use a Stored procedure in the
    Query SQL Statement
    You have the following options:
    ·        Specify a valid SQL SELECT statement to select the data to be sent from the specified database.
    ·        Specify an SQL EXECUTE statement to execute a stored procedure, which contains exactly one SELECT statement.
    The expression must correspond to the SQL variant supported by the relevant JDBC driver. It can also contain table JOINs.
    so have your whole select and update as part of this single Stored procedure

  • Web form not updating database with stored procedure

    Hello
    i have a problem with the web form updating the database i have a stored procedure which i need to connect to. If i execute the procedure in the SQL it will update the database but when i run the web form i get my catch error "could not update database".
    I have read so much on the net and my code seem ok but i,m just so lost.
    stored procedure
    PROCEDURE [dbo].[UpdateCustomer]
    @ID INT,
    @Firstname VARCHAR(30),
    @Surname VARCHAR(30),
    @Age INT
    AS
    BEGIN
    UPDATE Customer
    SET Firstname = @Firstname,
    Surname = @Surname,
    Age = @Age
    WHERE CustID = @ID
    END
    update code
    try
    SqlCommand command = new SqlCommand();
    command.Connection = conn;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "UpdateCustomer";
    command.Connection.Open();
    SqlParameter param = new SqlParameter();
    param.ParameterName = "@ID";
    param.SqlDbType = SqlDbType.Int;
    param.Direction = ParameterDirection.Input;
    param.Value = txtCustID.Text;
    command.Parameters.Add(param);
    command.Parameters.AddWithValue("@CustID", txtCustID.Text.ToString());
    command.Parameters.AddWithValue("@Firstname", txtFirstname.Text);
    command.Parameters.AddWithValue("@Surname", txtSurname.Text);
    command.Parameters.AddWithValue("@Gender", Gender.Text.ToString());
    command.Parameters.AddWithValue("@Age" ,txtAge.Text.ToString());
    command.Parameters.AddWithValue("@Address1", txtAddress1.Text.ToString());
    command.Parameters.AddWithValue("@Address2", txtAddress2.Text.ToString());
    command.Parameters.AddWithValue("@City", txtCity.Text.ToString());
    command.Parameters.AddWithValue("@Phone", txtPhone.Text.ToString());
    command.Parameters.AddWithValue("@Mobile", txtMobile.Text.ToString());
    command.Parameters.AddWithValue("@Email", txtEmail.Text.ToString());
    command.ExecuteNonQuery();
    lblMessage.Text = "Your Record(s) Have been Updated";
    command.Connection.Close();
    catch
    lblMessage.Text = "Your Record was not updated please try again";
    Thank you for your help

    To expand on Mike's advice.
    Change your catch to:
    catch(Exception ex)
    { // Break point here
    lblMessage.Text = "Your Record was not updated please try again";
    Put a break point in where the comment says.
    Run it.
    Hover over ex or add a quickwatch ( right click it ) and see what the error and inner exception is.
    I see several problems though.
    You have way too many parameters and Age should be int.
    They are objects  - they have a type.
    It'll be a string with your code there.
    Something more like
    command.Parameters.Add("@Age", SqlDbType.Int);
    command.Parameters["@Age"].Value = Convert.ToInt32(txtAge.Text);
    Although that might not cut and paste, it's air code intended to give you the idea.
    Hope that helps.
    Technet articles: Uneventful MVVM;
    All my Technet Articles

  • Parent-Child Table Update via Single Stored Procedure

    Uses: Oracle 9i Server via a Remote Connection with restriction, Oracle 9i client, .NET 2.0+System.Data.Oracle namespace, Delphi+BDE;
    I have an entry form which consists of a parent and a child table. I'm so keen in knowing whether there is any possibilities of updating the records via a single stored procedure? I mean if parent have a single record and corresponding to it, child have few records (more than 1 rows). I need to take the parent's value and child's value send it to the stored procedure and update it.
    Regards,

    The entry form gets the raw data from the user. So yes the data is captured front end but not updated nor processed. Once the user clicks "Save" or what ever the values are taken to the SP. In the SP the operations are done either as update or insert. Lets concentrate on Inserts only at the moment.
    I.e. there are 2 tables as:
    tbl_Payment(_PaymentID_, PaymentMode,PaymentDate, TotalPaymentValue);
    tbl_PaymentAccount(_PaymentID,AccountID_,PaymentValue);
    So the form has a top part which is bounded to the Parent Table which is "tbl_Payment" to few text boxes, and below it has a Datagrid, bounded to Child Table which is "tbl_PaymentAccount". And assume the the Single Stored Procedure as "sp_payment_addchg".
    I can pass newly entered values to the SP as:
    Command.Text = "sp_payment_addchg";
    /*so all the parent table bounded values are updated as*/
    Command.Parameter[0].value = text1.text;
    Command.Parameter[1].value = text2.text;
    and so on
    Since the DataGrid may have multiple rows, I cant figure it out how to get those rows into a single parameter.
    Regards,

  • How to handle a number of records in stored procedure?

    i wanna handle a number of records in a stored procedure one by one.
    what should i do?
    can any one give me some sample about the following question?
    Q:
    tb_main,tb_attach are two tables.
    i want to create a procedure to write off a record in tb_main. before do that, i want to write off all records in tb_attach which is related with the record to be written off in tb_main. and a procedure named pr_write_off_attach for writing off a record in tb_attach has been created already.
    what should i do?
    help!!
    null

    Dg.dataProvider.length is the number of records in the ArrayCollection
    Dg.rowCount is the number of visible rows.
    Alex Harui
    Flex SDK Developer
    Adobe Systems Inc.
    Blog: http://blogs.adobe.com/aharui

  • Problem with Update Select in Stored procedure

    I am using Oracle 8. I'm writing a StoredProcedure and Oracle doesn't like the statement:
    update
    Leave_Coverages
    set
    Rate_Monthly = Rate_Monthly + (select Rate_Monthly from Leave_coverages where Leave_Coverage_ID = 10800)
    where
    Leave_Coverage_ID = 10799;
    When I run the above statement from the command line - I have no problem. This statement in the stored procedure works:
    update
    Leave_Coverages
    set
    Rate_Monthly = Rate_Monthly + 4
    where
    Leave_Coverage_ID = 10799;
    So essentially, I'm having trouble using a select in an update statement, but only in a Stored Procedure.

    Denis,
    This question was answered on this forum in the last week or so but I wasn't quickly able to locate this post.
    Basically Oracle prior to version 9 had an SQL parser and a separate PL/SQL parser. The PL/SQL parser had to be updated each time new features were added to the SQL parser; often it lagged behind so that there were things you could do in plain SQL but weren't supported when using the same SQL in a cursor or directly in PL/SQL (with an INTO clause or RETURNING ... INTO clause).
    From Oracle 9 these two parsers have been rolled into one so that new features introduced into SQL automatically also become available when used from PL/SQL.
    So the answer to your curiousity on whether it will work in 9 or 10 is: if it works in SQL it should work just fine from PL/SQL.
    Cheerio,
    Colin

Maybe you are looking for