Dynamic sql to insert data in table

hi
I wish to insert 5 records in a table at runtime.
i.e the data should be entered at runtime.
I tried using a for loop but failed ..
I think it is possible thru dynamic sql ...
could someone help me out ..
regards
susmitha

If your database is 8 or above, you might want to try something like:
for i in 1..5 loop
execute immediate 'insert into table_name ' ||
'(your_field) values (your_value)';
end loop;
If your databasae is lower than 8, then you will have to use dbms_sql command instead. Hope this helps.

Similar Messages

  • Dynamic sql reurns no data when multiple values are passed.

    (Dynamic sql returns no data when multiple values are passed.)
    Hi,
    While executing the below dynamic sql in the procedure no data is returned when it has multiple input values.
    When the input is EMPID := '1'; the procedure works fine and returns data.Any suggestion why the procedure doen't works when input as EMPID := '1'',''2'; is passed as parameter?
    =======================================================
    create or replace PROCEDURE TEST(EMPID IN VARCHAR2, rc OUT sys_refcursor)
    IS
    stmt VARCHAR2(9272);
    V_EMPID VARCHAR2(100);
    BEGIN
    V_EMPID :=EMPID;
    stmt := 'select * from TEST123 where Empid is NOT NULL';
    IF V_EMPID <> '-1' THEN
    stmt := stmt || ' and Empid in (:1)';
    ELSE
    stmt := stmt || ' and -1 = :1';
    END IF;
    OPEN rc FOR stmt USING V_EMPID;
    END Z_TEST;
    ============================================================
    Script for create table
    ==================================================================
    CREATE TABLE TEST123 (
    EMPID VARCHAR2(10 BYTE),
    DEPT NUMBER(3,0)
    ===========================================
    Insert into PDEVUSER.TEST123 (EMPID,DEPT) values ('1',20);
    Insert into PDEVUSER.TEST123 (EMPID,DEPT) values ('2',10);
    Insert into PDEVUSER.TEST123 (EMPID,DEPT) values ('3',30);
    Insert into PDEVUSER.TEST123 (EMPID,DEPT) values ('3',30);
    Insert into PDEVUSER.TEST123 (EMPID,DEPT) values ('2',10);
    =============================================
    Select * from TEST123 where Empid in (1,2,3)
    EMPID DEPT
    1     20
    2     10
    3     30
    3     30
    2     10
    ===================================================================
    Any suggestion why the procedure doen't works when input EMPID := '1'',''2';?
    Thank you,

    The whole scenario is a little strange. When I tried to compile your procedure it couldn't compile, but I added the missing info and was able to get it compiled.
    create or replace PROCEDURE TEST (EMPID IN VARCHAR2, rc OUT sys_refcursor)
    IS
      stmt        VARCHAR2 (9272);
      V_EMPID     VARCHAR2 (100);
    BEGIN
      V_EMPID := EMPID;
      stmt := 'select * from TEST123 where Empid is NOT NULL';
      IF V_EMPID = '-1' THEN
        stmt := stmt || ' and Empid in (:1)';
      ELSE
        stmt := stmt || ' and -1 = :1';
      END IF;
      OPEN rc FOR stmt USING V_EMPID;
    END;If you pass in 1 as a parameter, it is going to execute because the statement that it is building is:
    select * from TEST123 where Empid is NOT NULL and -1 = 1Although the syntax is valid -1 will never equal 1 so you will never get any data.
    If you pass in 1,2 as a parameter then it is basically building the following:
    select * from TEST123 where Empid is NOT NULL and -1 = 1,2This will cause an invalid number because it is trying to check where -1 = 1,2
    You could always change your code to:
    PROCEDURE TEST (EMPID IN VARCHAR2, rc OUT sys_refcursor)
    IS
      stmt        VARCHAR2 (9272);
      V_EMPID     VARCHAR2 (100);
    BEGIN
      V_EMPID := EMPID;
      stmt := 'select * from TEST123 where Empid is NOT NULL';
      stmt := stmt || ' and Empid in (:1)';
      OPEN rc FOR stmt USING V_EMPID;
    END;and forget the if v_empid = '-1' check. If you pass in a 1 it will work, if you pass in 1,2 is will work, but don't pass them in with any tick marks.

  • Please recommend if we have options to read xml file and insert data into table without a temporary table.

    Please recommend if we have options to read xml file and insert data into table without a temporary table. 

    DECLARE @data XML;
    SET @data =N'<Root>
    <List RecordID="946236" />
    <List RecordID="946237" />
    <List RecordID="946238" />
    <List RecordID="946239" />
    <List RecordID="946240" />
    </Root>'
    INSERT INTO t (id) SELECT T.customer.value('@RecordID', 'INT') AS id
    FROM @data.nodes('Root/List')
     AS T(customer);
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Insert Data into Table from TXT or CSV file !!!!!

    Dear All,
    Recently I have started working on APEX. In Oracle Forms, we used to use TEXT_IO for inserting records into tables from txt files. Now I want to do the same thing in apex 3.2. How can I insert data into tables from txt of csv files.
    Waiting of your valuable suggestions.
    With kind regards,
    Yousuf.

    Yousuf
    wwv_flow_files is used by APEX to hold uploaded files.
    If you incorporate a file browse item on you page, browse for the selected file, then submit the page (having the PL/SQL process described as an on suibmit process) then what I detailed should work.
    If you don't need users to have access to this then just go to Home>Utilities>Data Load/Unload in APEX and there is a handy load utility there that does it all for you.
    Cheers
    Ben

  • Insert data into table from JSP page using Entity Beans(EJB 3.0)

    I want to insert data into a database table from JSP page using Entity Beans(EJB 3.0).
    1. I have a table 'FRIENDS', (in Oracle 10g database).
    2. It has two columns, 'NAME' and 'CITY'. Both have datatype strings(varchar2).
    3. Now from a JSP page, having two textfields, 'NAME' and 'CITY', I want to insert data into table 'FRIENDS'.
    4. In between JSP and database is a Entity Bean(EJB 3.0) and a stateless session bean.
    5. I am using JDev as editor.
    Please provide me code ASAP or link with similar example.
    Thank you.
    Anurag

    Hi,
    I am also trying that scenario. So u can
    Post the jsp form data to a Servlet which will act as a Controller.
    In the servlet invoke the business method.
    Similar kind of app is in www.roseindia.net
    Hope this would help u.
    Meanwhile if u get any optimal solution, pls post it.
    Thanks,
    Happy Java Coding.

  • How to insert data into table control

    hi experts,,
           i have inserted data into data base table through table control .
    now i want to insert data into table control through database table.
      how to delete data from table control for selected row

    Hi
    go through this link.
    http://www.****************/Tutorials/ABAP/TableControl/Demo.htm

  • How to Insert data from Table A to Table B

    Hi,
    I have to insert data from Table A into Table B.
    Table A is having 100 records and table B is having 10000 records.
    Condition is if data in Table A is not in table B then I have to insert into Table B or else I have to skip that.
    Can anyone help me with sample code.
    Thanks in advance and points are awarded for usefull answers.
    Thanks,
    Kumar.

    Hello Kumar
    Assuming that tables A and B have the same structure you can use the same logic as change documents are prepared. Assuming both of your itabs are of structure struc_a. Then define the following type:
    TYPES: BEGIN OF ty_s_itab_di. 
    INCLUDE TYPE struc_a.
    TYPES: CHIND  TYPE bu_chind.
    TYPES: END OF ty_s_itab_di.
    TYPES: ty_t_itab_di  TYPE STANDARD TABLE OF ty_s_itab_di                     
    WITH DEFAULT KEY.
    DATA:    gt_itab_old  TYPE ty_t_itab_di,
    gt_itab_new TYPE ty_t_itab_di.
    Fill itabs gt_Itab_old with the corresponding data of itab1 and gt_itab_new with the corresponding data of itab2.
    Very important: sort you itabs either by all key fields or by all fields.
    Call function <b>CHANGEDOCUMENT_PREPARE_TABLES</b> with the following parameters:
    - CHECK_INDICATOR = ' '
    - TABLE_NEW = gt_Itab_new
    - TABLE_OLD = gt_itab_old
    The function module will remove identical lines from both itabs. New entries in gt_itab_New will have CHIND = 'I' and deleted entries in gt_itab_old will have CHIND = 'D'. Modified entries are indicated by CHIND = 'U'.
    Read the documentation of the function module and play around with it. You will see that this a quite easy yet powerful approach for comparing itabs.
    Regards
    Uwe

  • Efficient Technique to Insert Data in Table ???

    Hi
    I want to implement the best efficient technique to insert data in Table. I have rows counts in millions in one db and I want to insert them in to another db with least time.
    >>
    I am implementing the technique of reading data from first db, apply some logics on it, insert the data in the log file (log4j) in the tab and line separated format and after completing the process insert log file data into the final table (using mysql load data local infile).
    >>
    Another technique that I use is to directly insert data into the final table after reading it from first db and applying logics on it.
    >>
    Another technique that I am going to use is using the JDBC Batch Update i.e. updating around (say 1000) rows in a final table after reading and applying logics on it.
    Bench Marks:
    The first technique takes 2 minutes for 60,000 rows
    The second technique takes 4.5 minutes for 60,000 rows
    The third technique is not implemented yet
    Do anyone have any other technique to implement this working in more efficient way?
    Regards
    Rizwan Ahmed (rizzz86)

    When optimizing JDBC code, there are several things you should look into:
    * use batch statements (reducing the number of roundtrips is really important!)
    * disable autoCommit and commit() on your own (reducing the number of roundtrips and the number of disk accesses on the DB server)
    * use PreparedStatements (avoid parsing the same statement again and again)
    Those three should buy you quite a bit of time.
    Edit: if it's still too slow (and only then!) you can look at the documentation of your JDBC driver. Sometimes there are options that can be enabled to speed things up (MySQL is especially bad here: there are some advanced features that are disabled by default, just because older server releases don't support them. I don't understand why they arent enabled conditionaly.).

  • Urgent!!!!!  Inserting data in table control of infotype.

    Hi Experts,
    I want to insert data in custom infotype.I am using FM HR_INFOTYPE_OPERATION for this purpose.But custom infotype contains a table control.
    Table control of infotype has 20 rows containing  fields name01 , addr01 upto name20 , addr20.
    How do i insert data in table control fields of infotype if i want to use FM HR_INFOTYPE_OPERATION .
    Pls suggest if there is another way to do it.
    Thanks.

    Thanks for your reply.
    I am Calling FM HR_INFOTYPE_OPERATION in a loop of a table.
    Table contains multiple employee numbers-PERNR.There can be all different PERNR or some records of same PERNR.
    Suppose if there are four records in the table.First two records are of the same PERNR.Then how would name01 or name02 will be assigned.
    Now, third record is  of new PERNR .Again it should be name01.
    So the question is how everytime in a loop i will assign nameNN for different PERNR.
    nameNN and addrNN was an example. I am sending my code here.
    Loop at it_data.
    gs_9000-PERNR = it_data-pernr..
    gs_9000-currentamount03 = it_data-curramt.
    gs_9000-mtdamount03 = it_data-mtd.
    gs_9000-qtdamount03 = it_data-qtd.
    gs_9000-ytdamount03 = it_data-ytd.
    gs_9000-roll12amount03 = it_data-roll.
    CALL FUNCTION 'BAPI_EMPLOYEE_ENQUEUE'
    EXPORTING
    NUMBER = gs_9000-pernr
    IMPORTING
    RETURN = RETURNE.
    CALL FUNCTION 'HR_INFOTYPE_OPERATION'
    EXPORTING
    INFTY = '9000'
    NUMBER = gs_9000-PERNR
    VALIDITYBEGIN = '20080801' 
    RECORD = gs_9000
    OPERATION = 'INS'
    TCLAS = 'A'
    DIALOG_MODE = '0'
    IMPORTING
    RETURN = RETURN
    KEY = KEY.
    IF RETURN IS NOT INITIAL.
    WRITE :/ 'Error Occurred'.
    ENDIF.
    CALL FUNCTION 'BAPI_EMPLOYEE_DEQUEUE'
    EXPORTING
    NUMBER = gs_9000-PERNR
    endloop.
    So in above code...
    gs_9000-currentamount03 = it_data-curramt.
    gs_9000-mtdamount03 = it_data-mtd.
    gs_9000-qtdamount03 = it_data-qtd.
    gs_9000-ytdamount03 = it_data-ytd.
    gs_9000-roll12amount03 = it_data-roll.
    these are table control fields , so how i wud i assign for
    gs_9000-currentamount04 = it_data-curramt.
    gs_9000-mtdamount04 = it_data-mtd.
    gs_9000-qtdamount04 = it_data-qtd.
    gs_9000-ytdamount04 = it_data-ytd.
    gs_9000-roll12amount04 = it_data-roll.
    in loop of a table.
    Thanks

  • How to insert data in table UI elements

    Hi Guys,
    Here is my questions. How to insert data in table UI elements.
    Here i try to insert data by using Add button
    data:
        Node_Item                           type ref to If_Wd_Context_Node.
        Node_Item = wd_comp_controller->get_data_node( ).
      data:
        lr_table_line type ref to ITEM.
       lr_table_line = Node_Item->create_element( ).
      field-symbols:
        <ls_table> type any.
      assign lr_table_line->* to <ls_table>.
      Node_Item->bind_element(
        new_item             = <ls_table>
        set_initial_elements = abap_false
        index                = 1 ).
    But i got syntax error the result type of the function method cannot not be converted in to the type of lr_table_line.
    And i set cardinality and selection as 0.n.
    Pls, let me know for the soulutions
    Chandru
    Message was edited by:
            chandrasekar muthuvelraj

    Chadru, another option is for you to bind the table UI field to an internal table and to populate the internal table. Then, automatically, your Table UI field will be populated with the data from the internal table. Here is the code that does that and works for me.  Good luck!
    method FILL_DATA .
    DATA: node_level1 type ref to if_wd_context_node,
          node_level2 type ref to if_wd_context_node,
          node_items type ref to if_wd_context_node,
          stru_ResItems type BAPI2093_RES_ITEM,
          tab_ResItems TYPE TABLE OF BAPI2093_RES_ITEM,
          n type i.
    node_level1 = wd_context->get_child_node( name = 'BAPI_RESERVATION_CRE' ).
    node_level2 = node_level1->get_child_node( name = 'CHANGING' ).
    node_items = node_level2->get_child_node( name = 'RESERVATIONITEMS' ).
    n = 2.
      do n times.
        insert stru_ResItems into table tab_ResItems.
      enddo.
    node_items->bind_table( tab_ResItems ).
    endmethod.

  • Insert data into table 1 but remove the duplicate data

    hello friends,
    i m trying to insert data into table tab0 using hints,
    query is like this..
    INSERT INTO /*+ APPEND PARALLEL(tab0) */ tab NOLOGGING
    (select /*+ parallel(tab1)*/
    colu1,col2
    from tab1 a
    where a.rowid =(select max (b.rowid) from tab2 b))
    but this query takes too much time around 5 hrs...
    bz data almost 40-50 lacs.
    i m using
    a.rowid =(select max (b.rowid) from tab2 b))....
    this is for remove the duplicate data..
    but it takes too much time..
    so please can u suggest me any ohter option to remove the duplicate data so it
    resolved the optimization problem.
    thanks in advance.

    In the code you posted, you're inserting two columns into the destination table. Are you saying that you are allowed to have duplicates in those two columns but you need to filter out duplicates based on additional columns that are not being inserted?
    If you've traced the session, please post your tkprof results.
    What does "table makes bulky" mean? You understand that the APPEND hint is forcing the insert to happen above the high water mark of the table, right? And you understand that this prevents the insert from reusing space that has been freed up because of deleted in the table? And that this can substantially increase the cost of full scans on the table. Did you benchmark the INSERT without the APPEND hint?
    Justin

  • Error in native SQL while inserting data into CORP table

    HI all,
    I am getting an exception while inserting records using native SQL into CORP table.
    PFB the code.
    LOOP AT gi_hrp1001 INTO wa_hrp1001.
      TRY.
            EXEC SQL.
              INSERT INTO misuser.Table_4
              VALUES (wa_hrp1001-otype , wa_hrp1001-objid ).
            ENDEXEC.
          CATCH cx_sy_native_sql_error.
            MESSAGE 'Connect - Error ' TYPE 'E'.
        ENDTRY.
      ENDLOOP.
    Please help.
    Thanks & Regards
    Nitesh

    I see two issue with your Insert. 1) you need to specify the field names in the table, for example otype and objid. 2) You need to indicate in the VALUES that you're using host variables by using the prefix :.
    I think the correct syntax is:
            EXEC SQL.
              INSERT INTO misuser.Table_4
             (otype, objid)
              VALUES (:wa_hrp1001-otype, :wa_hrp1001-objid)
            ENDEXEC.
    Good luck.

  • Dynamic SQL and GRANT CREATE ANY TABLE

    hi gurus,
    i have a dynamic SQL in a procedure where a table will be created from an existing table without data.
    strSQL:='create table ' || strTemp || ' as select * from ' || strArc || ' where 1=2';
    execute immediate strSQL;
    without GRANT CREATE ANY TABLE for the user, *"ORA-01031: insufficient privileges"* error during execution.
    Is there a way to tackle this issue without providing GRANT CREATE ANY TABLE privilige?
    many thanks,
    Charles

    ravikumar.sv wrote:
    The problem is not because of dynamic sql...It probably has something to do with dynamic SQL or, more accurately, dynamic SQL within a stored procedure.
    From a SQL*Plus command prompt, you can create a table if your account has the CREATE TABLE privilege either granted directly to it or granted to a role that has been granted to your account. Most people probably have the CREATE TABLE privilege through a role (hopefully a custom "developer role" that has whatever privileges you grant to users that will own objects but potentially through the default RESOURCE role). That is not sufficient to create tables dynamically via a definer's rights stored procedure. Only privileges that are granted directly to the user, not those granted via a role, are visible in that case.
    I expect that the DBAs are granting the CREATE ANY TABLE privilege directly to the account in question rather than through whatever role(s) are being used which is why that appears to solve the problem.
    Justin

  • Insert data into table

    When I try to insert data into the table, I get the error message:
    [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]The name 'uniqueID' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.
    What is wrong in the following code?
    String uniqueID, GenericType;
    float elevation, CalcTotalHeight, CalcTotalArea, CalcTotalVolume;
    stmt.executeUpdate(
    "INSERT INTO IFCData VALUES (uniqueID, GenericType, elevation, CalcTotalHeight, CalcTotalArea, CalcTotalVolume)" );

    No, sorry, now that I look at it for more than five seconds, your SQL is wrong.
    You don't have a list of columns that you are updating; I'm not sure whether this is bad SQL or whether the database will assume you meant to update all the columns in their declared order. But more to the point, the "values" you are assigning to those columns are not what you want. For example, the value you assign to the first column is uniqueid, meaning either the column named uniqueid or some SQL constant named uniqueid. There isn't any such thing, hence the message.
    I suppose you wanted to put the value of the Java variable "uniqueid" there. Of course the database and the JDBC driver don't know anything about the names of variables in your code, so they can't replace the string "uniqueid" by the value of the variable. The way to do that is to use a prepared statement, like this:PreparedStatement ps = conn.prepareStatement("INSERT INTO IFCData VALUES (?, ?, ?, ?, ?, ?)");
    // fix this SQL so you name the columns you are inserting into as well
    ps.setString(1, uniqueID);
    ps.setString(2, GenericType);
    ps.setFloat(3, elevation);
    // and so on
    ps.executeUpdate();PC&#178;

  • Error Message When Inserting Data Into Table

    Hi,
    I am trying to insert the following value into my customer table, however i get the following error on doing so.
    ERROR: date format picture ends before converting entire input string
    STATEMENT:
    insert into phonecall (start_date_time)
    values(10-JAN-2006 11:53:09)
    any ideas as to what might be wrong with my insert statement to bring up this error ??
    start_date_time is a DATE field by the way.

    Or
    SQL> alter session set nls_date_format='DD-MM-YYYY HH24:MI:SS';
    Session altered.
    SQL> create table aa (test date)
      2  /
    Table created.
    SQL> insert into aa values ('11-12-2006 12:11:22')
      2  /
    1 row created.
    SQL> select * from aa ;
    TEST
    11-12-2006 12:11:22
    SQL>

Maybe you are looking for