Insert within select

hi,
i want to insert a row in project table with fieldd
ProjId , desc, lastdate, userid from same table by passing the projectId
I mean I want to create a duplicate row in the same table with different projectid (that max of projId in that table) and insert into the same table.
Any help is appreaciated
Regards
infyniti

Since I do not know the relation between project and projectDetails, I am going to provide 3 examples
-- Assuming that you have only 1 records in projectDetails.
INSERT INTO project_tbl
(lastdate, userid, proj_id, DESC)
SELECT proj.lastdate
,proj.userid
,(SELECT MAX(proj_id)+1 FROM project) projId
,(SELECT projdetails.DESC||'SOMETHING' FROM ProjectDetails projdetails) DESC
FROM project proj
WHERE proj.proj_id = 1234
-- Getting information from projectDetails
INSERT INTO project_tbl
(lastdate, userid, proj_id, DESC)
SELECT projdetails.lastdate
,projdetails.userid
,(SELECT MAX(proj_id)+1 FROM project) projId
,projdetails.DESC||'SOMETHING' DESC
FROM ProjectDetails projdetails
WHERE projdetails.proj_id = 1234
-- Getting information from project and projectDetails
INSERT INTO project_tbl
(lastdate, userid, proj_id, DESC)
SELECT proj.lastdate
,proj.userid
,(SELECT MAX(proj_id)+1 FROM project) projId
,projdetails.DESC||'SOMETHING' DESC
FROM project proj
,ProjectDetails projdetails
WHERE proj.proj_id = projdetails.proj_id
AND proj.proj_id = 1234

Similar Messages

  • Locking problem w/ INSERT..SELECT using sequences.

    I have a scenario where an INSERT..SELECT statement that includes incrementing a sequence value is causing Exclusive DX locks on the SYS.I_SEQ1 index and the SYS.RESOURCE_COST$ table. When the statement is executing from within a COM+ application using distributed transactions the behavior is exhibited. If the same statement is executed from SQLPlus these locks are not acquired. The statement is similar to:
    insert into table1 ( pkey, column2)
    select pkey_seq.nextval, value2
    from table2;
    Anyone have any idea why this behavior is seen in a distributed transaction?
    Thanks,
    Sam

    Hi Ari,
    Thanks for the tip. I noticed that approach in earlier posts, but these people were using Oracle 9 and below. The feature I am trying to use is a JDBC 3.0 feature and I believe was introduced in recent JDBC drivers (10.1.0).
    I suspect the driver inserts a RETURNING clause anyway. I got this impression from this post (mid-way down the first paragraph):
    http://archives.postgresql.org/pgsql-jdbc/2003-05/msg00040.php
    Although this post is about to postgresql's JDBC driver, I guess the Oracle driver would probably do a similar thing? If so, then the problem I am having could be that the final SQL (my SQL + inserted RETURNING clause) is invalid.
    Anyway, I don't have a problem with this anymore because I changed my design (for the better) and I don't have to do the insert select statement.
    Regards,
    Simon.

  • Querying for a script insert multiple selected objects...

    Is there a script or plugin which insert multiple selected objects in one new text frame with one click?
    And is there a script or plugin which extract the content of anchored text frame out it's frame and replace it with it's frame. and extract selected text and insert it inside a new anchored text frame in it's place? (like convert text to table - convert table to text, but instead table we use text frame)

    Hi,
    Using OMB scripting to set attribute properties in a data mapping sort of defeats the purpose of utilizing a graphical user interface to define and set properties for a data mapping? Surely the GUI data mapping tool was created to get away from writing scripts and scripting would also require that you know the name of the data mapping, table operator and the set of attribute names for which you have to write one line of script to set each property value, i.e. 90 lines to set 90 attribute values.
    Cheers,
    Phil

  • ORDER BY not allowed in INSERT..SELECT ?

    I'm trying to INSERT..SELECT into a table, the SELECT should be ordered by a clause, but Oracle (8.1.7) does not permit that.
    INSERT INTO SumTable
    SELECT Field1,Field2
    FROM FullTable
    WHERE field1 = 1
    ORDER BY field2;
    I get: ORA-00933: SQL command not properly ended
    It doesn't matter if the "order by" field is part of the SELECT or not. When I remove the ORDER BY the INSERT works perfectly. Oracle docs do not mention this limitation. Is this known issue? Can I make it to work?
    I do have a solution in PL/SQL, open a cursor for the SELECT with the ORDER BY, INSERT one row at a time in the cursor loop, but its too slow when I have more than 10K records to insert.
    Thanks,
    Yoram Ayalon

    I have to take issue with this. rows are inserted based on the blocks on the freelist.
    Watch.
    We will create a feeder table.
    SQL> create table feeder (ID) pctfree 0 as select rownum from all_objects;
    Table created.
    SQL> create table FRED (mycol integer) pctfree 0 pctused 99;
    {99 so that free space is reused almost immediately for demo only }
    Table created.
    SQL> analyze table fred compute statistics;
    Table analyzed.
    SQL> select blocks from user_tables where table_name='FRED';
    BLOCKS
    0
    SQL> insert into fred (mycol) select id from feeder where id <=32000;
    32000 rows created.
    { So we now have a tightly packed table. Blocksize 8k by- the-by )
    SQL> analyze table fred compute statistics;
    SQL> select blocks from user_Segments where segment_name='FRED';
    BLOCKS
    48
    { Ok : 48 blocks.  Now we will get rid of 1/2 of the rows scattered throughout the 48 }
    SQL> delete from fred where mod(mycol,2) =0;
    16000 rows deleted.
    SQL> analyze table fred compute statistics;
    Table analyzed.
    SQL> select blocks from user_Segments where segment_name='FRED';
    BLOCKS
    48
    { Ok : Still 48 blocks since we haven't been inserting only deleting }
    [ if we looked at user_Tables now it would show most if not all of the blocks on the freelist given the 99 PCTUSED }
    { now let's chuck in another 16000 ordered rows }
    SQL> insert into fred (mycol) select id from feeder where id <=16000 order by id
    16000 rows created.
    SQL> analyze table fred compute statistics;
    Table analyzed.
    SQL> select blocks from user_Segments where segment_name='FRED';
    BLOCKS
    48
    { look ! no extra blocks ! 
    -> it must have filled the non-contiguous holes in the blocks
    -> the rows are not physically ordered.

  • Number of rows inserted is different in bulk insert using select statement

    I am facing a problem in bulk insert using SELECT statement.
    My sql statement is like below.
    strQuery :='INSERT INTO TAB3
    (SELECT t1.c1,t2.c2
    FROM TAB1 t1, TAB2 t2
    WHERE t1.c1 = t2.c1
    AND t1.c3 between 10 and 15 AND)' ....... some other conditions.
    EXECUTE IMMEDIATE strQuery ;
    These SQL statements are inside a procedure. And this procedure is called from C#.
    The number of rows returned by the "SELECT" query is 70.
    On the very first time call of this procedure, the number rows inserted using strQuery is *70*.
    But in the next time call (in the same transaction) of the procedure, the number rows inserted is only *50*.
    And further if we are repeating calling this procedure, it will insert sometimes 70 or 50 etc. It is showing some inconsistency.
    On my initial analysis it is found that, the default optimizer is "ALL_ROWS". When i changed the optimizer mode to "rule", this issue is not coming.
    Anybody faced these kind of issues?
    Can anyone tell what would be the reason of this issue..? any other work around for this...?
    I am using Oracle 10g R2 version.
    Edited by: user13339527 on Jun 29, 2010 3:55 AM
    Edited by: user13339527 on Jun 29, 2010 3:56 AM

    You have very likely concurrent transactions on the database:
    >
    By default, Oracle Database permits concurrently running transactions to modify, add, or delete rows in the same table, and in the same data block. Changes made by one transaction are not seen by another concurrent transaction until the transaction that made the changes commits.
    >
    If you want to make sure that the same query always retrieves the same rows in a given transaction you need to use transaction isolation level serializable instead of read committed which is the default in Oracle.
    Please read http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10471/adfns_sqlproc.htm#ADFNS00204.
    You can try to run your test with:
    set  transaction isolation level  serializable;If the problem is not solved, you need to search possible Oracle bugs on My Oracle Support with keywords
    like:
    wrong results 10.2Edited by: P. Forstmann on 29 juin 2010 13:46

  • Commit for every 1000 records in  Insert into select statment

    Hi I've the following INSERT into SELECT statement .
    The SELECT statement (which has joins ) has around 6 crores fo data . I need to insert that data into another table.
    Please suggest me the best way to do that .
    I'm using the INSERT into SELECT statement , but i want to use commit statement for every 1000 records .
    How can i achieve this ..
    insert into emp_dept_master
    select e.ename ,d.dname ,e.empno ,e.empno ,e.sal
       from emp e , dept d
      where e.deptno = d.deptno       ------ how to use commit for every 1000 records .Thanks

    Smile wrote:
    Hi I've the following INSERT into SELECT statement .
    The SELECT statement (which has joins ) has around 6 crores fo data . I need to insert that data into another table.Does the another table already have records or its empty?
    If its empty then you can drop it and create it as
    create your_another_table
    as
    <your select statement that return 60000000 records>
    Please suggest me the best way to do that .
    I'm using the INSERT into SELECT statement , but i want to use commit statement for every 1000 records .That is not the best way. Frequent commit may lead to ORA-1555 error
    [url http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:275215756923]A nice artical from ASKTOM on this one
    How can i achieve this ..
    insert into emp_dept_master
    select e.ename ,d.dname ,e.empno ,e.empno ,e.sal
    from emp e , dept d
    where e.deptno = d.deptno       ------ how to use commit for every 1000 records .
    It depends on the reason behind you wanting to split your transaction into small chunks. Most of the time there is no good reason for that.
    If you are tying to imporve performance by doing so then you are wrong it will only degrade the performance.
    To improve the performance you can use APPEND hint in insert, you can try PARALLEL DML and If you are in 11g and above you can use [url http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/d_parallel_ex.htm#CHDIJACH]DBMS_PARALLEL_EXECUTE to break your insert into chunks and run it in parallel.
    So if you can tell the actual objective we could offer some help.

  • A query with "insert..select" run with dqp?

    I would like to know if a query with "insert cluase select clause" is operated with the function of the dqp.
    I think that a query portion with "select clause" is possible to run with dqp.
    if anybody knows about this case, please let me know.
    Thanks in advance,
    Dokon Kim

    There is no such thing as pl/sql 5.1.4, at that time pl/sql didn't even exist.
    You also state it works in Sql*plus but it doesn't work in PL/SQL as PL/SQL is using OCI7. Which isn't true.
    Please stop playing games and withhelding information.
    Post the real info. Not what you think is important.
    Why are you using a (desupported) 9.0.1 client against a 10GR2 database?}
    Sybrand Bakker
    Senior Oracle DBA

  • Mechanism of a insert into select

    Hi
    I am using a insert with select in a batch program which is supposed to run just a little before midnight.
    My question is, when a insert into a table occurs, does oracle select all the rows before the insert starts, OR, does the insert occur simultaneously with the select.
    eg. table B has 4 rows
    data is being inserted into A from B
    does oracle select row1 from B and insert row1 into table A and then move to row2 in B and insert row2 into A
    OR
    does oracle select rows1 thro 4 in B before the first insert into A starts?
    If anybody could point me to a document/reference about that would be aweosme.
    --vj                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    There is no need to know this kind of internals. A statement is atomic, so it either completes in total or not at all. Other sessions cannot see intermediate results, even your own session is not able to. If you would try, for example by using database triggers, you'll get a mutating table error to prevent that.
    Regards,
    Rob.

  • HOw to improve insert/update/select  for nested table.

    Hi All,
    I think this is my second thread for nested table.
    i just want to know what are the different ways available to improve the insert/update/select operation on Nested table.
    Thanks in advance.

    By not using a nested table for data storage in the first place...
    http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:8135488196597
    Perhaps Parallel Query/DML might give some relief.

  • Insert with Select while Using Merge

    Actual Query
    MERGE INTO schema1.employees D
       USING (SELECT employee_id, salary, department_id FROM schema2.employees
       WHERE department_id = 80) S
       ON (D.employee_id = S.employee_id)
       WHEN MATCHED THEN
       UPDATE
       SET D.bonus = D.bonus + S.salary*.01
       WHERE (S.salary > 8000)
       WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)
       VALUES (S.employee_id, S.salary*.01)
       WHERE (S.salary <= 8000) Is there a possible way to write a select Query in Insert Statement while using Merge.
      WHEN NOT MATCHED THEN
       INSERT
       SELECT * FROM schema2.employees
       WHERE S.salary <= 8000;

    it works:
    MERGE INTO fifapps.tes2
    using dual
    on (1=2)
    when not matched then insert values(59,'yes');
    commit;or
    MERGE INTO fifapps.tes2
    using dual
    on (1=2)
    when not matched then insert values ((select 99 from dual),(select'weii' from dual));
    commit;Edited by: ʃʃp on Jun 13, 2012 2:03 AM
    Edited by: ʃʃp on Jun 13, 2012 2:03 AM

  • How to write select within select  in ABAP

    Hi Guys
    I want to write a select statement in ABAP to select from 2 tables below is my code I want to write select within select statment in --->A and replace with ---> B
    FORM sales_production_order  USING    p_vbeln TYPE vbeln_va
                                 CHANGING p_aufnr TYPE aufnr
                                          p_rtcod TYPE sysubrc.
      CLEAR: p_rtcod,
             p_aufnr.
    A *******
      SELECT aufnr FROM aufk INTO p_aufnr UP TO 1 ROWS
        WHERE kdauf EQ p_vbeln .
    B *******
      SELECT kdauf from aufk into p_aufnr UP TO 1 ROWS
        WHERE KDAUF IN ( SELECT VBELN FROM VBAP where vbeln EQ p_vbeln) .
      ENDSELECT.
      p_rtcod = sy-subrc.
    ENDFORM.                    " sales
    T

    Hi Eslam,
    In select statements, ENDSELECT must be added when single line returned without the addition 'SINGLE'.
    The innner query in your code, returns a single record but with the addition 'UP TO 1 ROWS'. The outer query also like this only. But the outer query ended with 'ENDSELECT', not for inner query.
    Else you can add 'SINGLE' keyword.
      SELECT aufnr FROM aufk INTO p_aufnr UP TO 1 ROWS
        WHERE kdauf EQ '10' .
        SELECT SINGLE kdauf from aufk into p_aufnr
        WHERE KDAUF IN ( SELECT VBELN FROM VBAP where vbeln EQ p_vbeln) .
      ENDSELECT.
      SELECT aufnr FROM aufk INTO p_aufnr UP TO 1 ROWS
        WHERE kdauf EQ '10' .
    ***** It assigns Nth record's KDAUF to p_aufnr provided N records satisfy the condition.
        SELECT  kdauf from aufk into p_aufnr UPTO 1 ROWS
        WHERE KDAUF IN ( SELECT VBELN FROM VBAP where vbeln EQ 'p_vbeln) .
        ENDSELECT.
      ENDSELECT.
    Regards,
    R.Nagarajan.

  • Urgent : Performance Issue DELETE , INSERT INTO SELECT, UPDATE

    Hi,
    NEED ASSISTANCE TO OPTIMIZE THE INSERT STATEMENT (insert into select):
    =================================================
    We have a report.
    As per current design following steps are used to populate the custom table whcih is used for reporting purpose:
    1) DELETE all the recods from the custom table XXX_TEMP_REP.
    2) INSERT records in custom table XXX_TEMP_REP (Assume all the records related to type A)
    using
    INSERT..... INTO..... SELECT.....
    statement.
    3) Update records in XXX_TEMP_REP
    using some custom logic for the records populated .
    4) INSERT records in custom table XXX_TEMP_REP (Records related to type B)
    using
    INSERT..... INTO..... SELECT.....
    statement.
    Stats gathered related to Insert statement are:
    Event Wait Information
    SID 460 is waiting on event : db file sequential read
    P1 Text : file#
    P1 Value : 20
    P2 Text : block#
    P2 Value : 435039
    P3 Text : blocks
    P3 Value : 1
    Session Statistics
    redo size : 293.84 M
    parse count (hard) : 34
    parse count (total) : 1217
    user commits : 3
    Transaction and Rollback Information
    Rollback Used : 35.1796875 M
    Rollback Records : 355886
    Rollback Segment Number : 12
    Rollback Segment Name : _SYSSMU12$
    Logical IOs : 1627182
    Physical IOs : 136409
    RBS Startng Extent ID : 14
    Transaction Start Time : 09/29/10 04:22:11
    Transaction_Status : ACTIVE
    Please suggest how this can be optimized.
    Regards,
    Narender

    Hello,
    Is there any relation with the Oracle Forms tool ?
    Francois

  • Inserting and Selecting LONG with PRO*C

    Is there any special hints in order to use
    the LONG datatype with pro*c ? Can I insert
    and select this kind of type like any other
    CHAR/VARCHAR/VARCHAR2, even if this field
    has a length of about 65536 chars ?

    Well, random because it is not always the same nor the error code.
    - Sometimes I get segmentation fault on "sqlcxt";
    - "ORA-01024: invalid datatype in OCI call" on queries that usually work
    - "ORA-03114: not connected to ORACLE" on queries that usually work
    I have run valgrind and the only "place" where there could be an issue is reported to be caused by oracle libs:
    ==27055== 8,214 bytes in 1 blocks are possibly lost in loss record 193 of 206
    ==27055== at 0x40046FF: calloc (vg_replace_malloc.c:279)
    ==27055== by 0x43E2975: nsbal (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x42F04E6: nsiorini (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x4300FD2: nsopenalloc_nsntx (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x42FFD73: nsopenmplx (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x42F91FD: nsopen (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x42BDAFE: nscall1 (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x42BB0D7: nscall (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x435F653: niotns (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x43F9E40: nigcall (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x436AD4B: osncon (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)
    ==27055== by 0x41EAA31: kpuadef (in /home/oracle/app/oracle/product/11.1.0/db_1/lib/libclntsh.so.11.1)

  • LONG - instr & insert..select

    Hi,
    Two queries:
    a) How to search for a character/combination of characters in a LONG column
    b) How to take a backup of a table which contains LONG column (might contain columns of other datatypes like number, varchar etc.)
    I tried using INSTR and CREATE(INSERT)..SELECT options but both failed with errors.
    SQL> CREATE TABLE T1(C1 LONG);
    Table created
    SQL> INSERT INTO T1 VALUES (';Z00:101CGEZZP1904300M5');
    1 row inserted
    SQL> SELECT INSTR(C1,'ZP') FROM T1;
    SELECT INSTR(C1,'ZP') FROM T1
    ORA-00932: inconsistent datatypes: expected NUMBER got LONG
    SQL> CREATE TABLE T2 AS SELECT * FROM T1;
    CREATE TABLE T2 AS SELECT * FROM T1
    ORA-00997: illegal use of LONG datatype
    SQL>

    Hi,
    LONG is an old format with a bunch of limitations which will make your life miserable. If you need to copy your table to another one, use this opportunity to convert LONG to CLOB using to_lob() function.
    create table t1(id long);
    create table t2 (id clob);
    insert into t2 (id) select to_lob(id) from t1;Best regards,
    Nikolay

  • Inserting multiple selection from checkbox in to one column of the database

    Hi,
    how to insert multiple selection from checkbox into one column of the database.(I select array of values from checkbox ,then how to insert tat array of values iinto single column name).
    Anyone can u reply me
    Thanx

    hhhmmm.... is this what you mean?
    lets say you hava a checkbox1 with values value1, value2, value3 and you selected all there of them? then you want then to be stored in the database in one column?
    now the question is:
    Is it going to be one column one row?
    datafield
    value1,value2,value3
    Or one column multiple row?
    datafield
    value1
    value2
    value3
    Which is it?

Maybe you are looking for

  • I can no longer crop and scale my photos to fit my screen as wallpaper with the OS7 update.

    As stated above.

  • A simple text file in a StringBuffer

    Hello , I am new in Java. I want to read a file and put it in a StringBuffer so the entire file can be edited as a simple bunch string. How to process efficiently ?? Thanks for help

  • Change ip in oracle workflow

    Hello, In OWB 11g I create a schema owf_mgr, and I've migrated the server, and has another IP. In OWB i try to change IP from Locations/Process flow and schedules/Oracle workflow/WORKFLOW_MANAGER , but the fields are disabled for editing. How to chan

  • 1.93 Gb of Other storage

    I've been having trouble converting a couple of videos from quicktime to iPad format. I thought the conversion worked but when I tried to play them on the iPad they wouldn't play. I'm going to redo the conversion so that isn't really my question. But

  • Contact Role in Customer Master

    I have assigned following roles to the 'Customer Contact' in Customer Master. Could someone help me what all the notifications customer contact will get from OM/any other module if he has this role assigned? Or does it depend on how OM implementation