QUESTION ON INSERT STATEMENT WITH SELECT

Hi
I have a table which contains three fields
Table name : USERS
ID NUMBER(10),
NAME VARCHAR2(50),
EMAIL VARCHAR2(50)
SEQUENCE by name SEQ_ID
I am trying to insert values into USERS from EMPLOYEE by virtue
of insert into USERS (SELECT NAME, MAIL from EMPLOYEE) but i
want to have one id for all the rows fetched from my inner
query.
I would like to know if here is a way, by which i can fetch a
constant in my select statement which is not present in the
table and use it like
insert into USERS ( SELECT constant, NAME, MAIL FROM EMPLOYEE );
where constant is some number unique for a given insertion.
Thanks
Arun

Why not just try it? All you had to do was type
insert into USERS SELECT 1, NAME, MAIL FROM EMPLOYEE ;
and you would have had your answer immediately instead of
waiting for me to tell you.
rgds, APC

Similar Messages

  • INSERT statement with JSP

              Does anyone know how to create an INSERT statement with JSP using
              variables??
              I can do the insert if I code the values of the variables in the statement,
              but when I try to use variables to do the insert it tries to insert the
              variable name and not the value of the variable into the table.
              Thanks,
              Doug
              

    "Doug Schaible" <[email protected]> wrote in message news:<bicO7.1279268$[email protected]>...
              > I can do the insert if I code the values of the variables in the statement,
              > but when I try to use variables to do the insert it tries to insert the
              > variable name and not the value of the variable into the table.
              Doug,
              I think you missed the <%= blahblah %> syntax.
              Regards
              drit
              

  • Strange results with Insert statement having select query

    Hi all,
    I am facing a strange issue with Insert statement based on a select query having multiple joins.
    DB- Oracle 10g
    Following is the layout of my query -
    Insert into Table X
    Select distinct Col1, Col2, Col3, Col4, Function(Col 5) from Table A, B
    where trunc(updated_date) > = trunc(sysdate-3)
    and join conditions for A, B
    Union
    Select Col1, Col2, Col3, Col4, Function(Col 5) from Table C, D
    trunc(updated_date) > = trunc(sysdate-3)
    and join conditions for C, D
    Union
    .... till 4 unions. all tables are residing in the local Database and not having records more than 50,000.
    If I execute above insert in a DBMS job, it results into suppose 50 records where as if I execute the select query it gives 56 records.
    We observed following things-
    a) no issue with size of tablespace
    b) no error while inserting
    c) since query takes lot of time so we have not used Cursor and PLSQL block for inserting.
    d) this discrepancy in number of records happens frequently but not everytime.
    e) examined the records left out from the insert, there we couldn't find any specific pattern.
    f) there is no constraint on the table X in which we are trying to insert. Also tables A, B, C....
    I went through this thread -SQL insert with select statement having strange results but mainly users are having either DB Links or comparison of literal dates, in my case there is none.
    Can somebody explain why is the discrepancy and what is the solution for it.
    Or atleast some pointers how to proceed with the analysis.
    Edited by: Pramod Verma on Mar 5, 2013 4:59 AM
    Updated query and added more details

    >
    Since I am using Trunc() in the where clause so timing should not matter much. Also I manually ruled out records which were updated after the job run.
    >
    The first rule of troubleshooting is to not let your personal opinion get in the way of finding out what is wrong.
    Actually this code, and the process it represents, is the most likely CAUSE of the problem.
    >
    where trunc(updated_date) > = trunc(sysdate-3)
    >
    You CANNOT reliably use columns like UPDATED_DATE to select records for processing. Your process is flawed.
    The value of that column is NOT the date/time that the data was actually committed; it is the date/time that the row was populated.
    If you insert a row into a table right now, using SYSDATE (8am on 3/5/2013) and don't commit that row until April your process will NEVER see that 3/5/2013 date until April.
    Here is the more typical scenario that I see all the time.
    1. Data is inserted/updated all day long on 3/4/2013.
    2. A column, for example UPDATED_DATE is given a value of SYSDATE (3/4/2013) in a query or by a trigger on the table.
    3. The insert/update query takes place at 11:55 PM - so the SYSDATE values are for THE DAY THE QUERY BEGAN
    4. The data pull begins at 12:05 am (on 3/5/2013 - just after midnight)
    5. The transaction is COMMITTED at 12:10 AM (on 3/5/2013); 5 minutes after the data pull began.
    That data extract in step 4 will NEVER see those records! They DO NOT EXIST when the data pull query is executed since they haven't been committed.
    Even worse, the next nights data pull will not see them either! That is because the next pull will pull data for 3/5/2013 but those records have a date of 3/4/2013. They will never get processed.
    >
    Job timing is 4am and 10pm EST
    >
    Another wrinkle is when data is inserted/updated from different timezones and the UPDATED_DATE value is from the CLIENT pc or server. Then you can get even more data missed since the client dates may be hours different than the server date used for the data pull process.
    DO NOT try to use UPDATED_DATE type columns to do delta extraction or you can have this issue.

  • Insert statement with loop function

    Hello all,
    I'm new with the loop function
    What need to do is to use a select statement with a condition
    This output needs to be inserted in a table
    The values of the condition are stored also in a table
    It will be something like this:
    BEGIN
      FOR i IN (select nrs from table1)  LOOP
        INSERT INTO table_out_loop
        SELECT * FROM bigtable_vw
        WHERE nr = i
      END LOOP ;
    END ;
    I cant use a direct insert_into function in this case so I need the plsql to 'look' for the values 1 by 1 from the table1
    Any help would be appreciated

    Can't you just:
        insert into table_out_loop
        select *
        from   bigtable_vw
        where  nr in (select nrs from table1);
    If not, then post a better example and/or rephrase the question...

  • Insert statement with Date, time into Oracle

    Hi,
    I've got the following statement that I'm trying to insert into Oracle. Actually had to change up the format to dd-mm-yy to get it to insert the date correctly, not sure why, but I've got it "supposedly" formatted to also insert the hours, min., seconds into the db record, but it's not catching it.
    My code:
    INSERT INTO VOTETBL
    (CHANGE_CTRL_ID,BUSVP,BRANCH,VOTE,VOTE_TIMESTAMP)
    VALUES (?, ?,?,?,to_date(sysdate, 'dd-mm-yy hh24:mi:ss'));Shouldn't the sysdate capture all those elements sufficiently? The day, month, year get inserted correctly with this, but not so for the time portion.
    Any suggestions is welcomed.
    Thx.

    ok, thanks.
    I tried making a result set with the following:
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rst = stmt.executeQuery("select to_char(sysdate) from dual");
    Date dualtimestamp = (rst.getDate("sysdate"));But it makes the servlet blow up! Not sure why either, because if I enter that exact statement in Oracle, it pulls back the date and time just like I aim to. But not embedded in the result set statement within the servlet.
    It breaks up somehow and doesn't get that result!

  • Insert statement with timezone asking for parameter

    I have an insert statement in a file.
    When the whole file is executed it inserts around 300 rows in to table.
    One of the insert statement is failing. This insert statement inserts a query into column of a table.
    It is as following
    Insert into hr.TABLE_PATH
    (PATH, TABLE_OWNER_NAME, TABLE_NAME, PATH_NAME, TABLE_ACCESS_PATH_DESC, TABLE_PATH_SQL_TXT, PROCESS_CODE, DELETE_IND, ACCESS_ID)
    Values
    (290, HR, EMPLOYEES, 'Reconc', 'XYZ', 'SELECT COUNT(*) FROM (SELECT hr.product, TO_TIMESTAMP (TO_CHAR (FROM_TZ (sm.product_tmstp, 'GMT') AT TIME ZONE 'US/Central', 'YYYY-MM-DD HH24:MI:SS.FF6'), 'YYYY-MM-DD HH24:MI:SS.FF6') AS product_tmstp FROM ( SELECT DISTINCT(product) FROM hr.updated_table) upr, RH.product AS OF SCN :1 spr, RH.milestone AS OF SCN :1 sm WHERE spr.product = upr.product AND sm.product = spr.product MINUS SELECT prf.product, mi.product_tmstp FROM ( SELECT MAX(execute_id) exeute_id FROM hr.slave_execute ) cmr, hr.prod prf, hr.hr_info mi WHERE prf.execute_id = cmr.execute_id AND mi.milestone_group_id = prf.milestone_group_id )', 'N ', 'N', 2);
    The problem is with this piece of insert:
    TO_TIMESTAMP (TO_CHAR (FROM_TZ (sm.product_tmstp, 'GMT') AT TIME ZONE 'US/Central', 'YYYY-MM-DD HH24:MI:SS.FF6'), 'YYYY-MM-DD HH24:MI:SS.FF6') AS product_tmstp
    : --> is being considered as parameter.
    Where as the whole sql thing needs to get inserted into the table.

    If you are inserting a string, then any quotes inside the string should be replaced with two single quotes,
    try this,
    INSERT INTO hr.TABLE_PATH (PATH,
                               TABLE_OWNER_NAME,
                               TABLE_NAME,
                               PATH_NAME,
                               TABLE_ACCESS_PATH_DESC,
                               TABLE_PATH_SQL_TXT,
                               PROCESS_CODE,
                               DELETE_IND,
                               ACCESS_ID)
    VALUES (
             290,
             HR,
             EMPLOYEES,
             'Reconc',
             'XYZ',
             'SELECT COUNT(*) FROM (SELECT hr.product, TO_TIMESTAMP (TO_CHAR (FROM_TZ (sm.product_tmstp, ''GMT'') AT TIME ZONE ''US/Central'', ''YYYY-MM-DD HH24:MI:SS.FF6''), ''YYYY-MM-DD HH24:MI:SS.FF6'') AS product_tmstp FROM ( SELECT DISTINCT(product) FROM hr.updated_table) upr, RH.product AS OF SCN :1 spr, RH.milestone AS OF SCN :1 sm WHERE spr.product = upr.product AND sm.product = spr.product MINUS SELECT prf.product, mi.product_tmstp FROM ( SELECT MAX(execute_id) exeute_id FROM hr.slave_execute ) cmr, hr.prod prf, hr.hr_info mi WHERE prf.execute_id = cmr.execute_id AND mi.milestone_group_id = prf.milestone_group_id )',
             'N ',
             'N',
             2
           );Note the
    TO_TIMESTAMP (TO_CHAR (FROM_TZ (sm.product_tmstp, ''GMT'') AT TIME ZONE ''US/Central'', ''YYYY-MM-DD HH24:MI:SS.FF6''), ''YYYY-MM-DD HH24:MI:SS.FF6'') AS product_tmstp First off, How are you inserting the data? using sqlldr or utl_file?
    If you are inseting using a script from Sqlplus
    use
    SET DEFINE OFFjust to make sure nothing is treated as a parameter.
    G.
    Edited by: Ganesh Srivatsav on Apr 6, 2011 5:20 PM

  • Query in Insert statement with JDBC Rx adapter

    Hi,
    This is a FILE to JDBC scenario.
    My target datatype is like this with sample payload values
    statement_s
    statement_s
       action='INSERT"
      access
        A=1234
        B=1002
    key
       A=1234
       B ="10%"
         compareOperation= LIKE
    A(1..1) is primary key in DB and B(0..1) colunm is an optional one.
    I want to know if such kind of Insert query with(LIKE operator) is possible.
    Please reply me ASAP.
    Edited by: ram pranav on Feb 18, 2009 10:28 PM

    Hi Ram,
    IN case of "UPDATE_INSERT", you can use the KEY tag in your structure. Also you can provide LIKE as an attribute to the KEY element as follows :
    <key1>
         <col4 compareOperation=u201DLIKEu201D>val%</col4>
    </key1>
    But when you are trying to insert the data_, the <key> tags will be ignored and only <access> will be considered._
    action=UPDATE_INSERT
    The statement has the same format as for the UPDATE action. Initially, the same action is executed as for UPDATE. If no update to the database table can be made for this action (the condition does not apply to any table entry), values of the table described in the <access> element are inserted in accordance with the description of the action INSERT. <key> elements are ignored in this case.
    The response document has the following format; one of the two values is always 0 because either an UPDATE or an INSERT action is always executed:
    Go through the link I have provided earlier.
    Thanks,
    Pooja Pandey

  • Creating Multiple INSERT statements with SQL

    is there a way to create multiple INSERT staements with SQL .
    Example scenario : This is only example but i have lot of data in the real time.
    sql : Select Emplid from Table A where Emplid between 100 and 350 will retun me 50 rows . i want to insert those rows into another table.
    I am looking for output like below instead of Giving output just as EMPLIDs
    Insert into PS_LM_DATA ( EMPLID ) values ( 123 )
    Insert into PS_LM_DATA ( EMPLID ) values ( 234 )
    Insert into PS_LM_DATA ( EMPLID ) values ( 334 )
    and so on....
    thanks ,
    Karu

    If you are inserting into another table, you could use
    insert into PS_LM_DATA ( EMPLID ) 
    select Emplid from Table A where Emplid between 100 and 350Example:
    SQL> insert into emp2(empno) select empno from emp;
    14 rows created.
    SQL> insert into emp2(empno) select empno from emp where empno between 7369 and 7788;
    8 rows created.

  • INSERT INTO with SELECT invalid column name error

    I have a problem where I wish to update a table with only records that have changed or insert into a table with only new records. The Source table is queried using OPENQUERY as we have no staging db for this.
    I have the following code (adapted from a working example):
    INSERT INTO dbo.Contact
    SELECT contactID, lastUpdatedDateTime
    FROM
    MERGE INTO dbo.Contact AS DT
    USING (SELECT * FROM OPENQUERY(LINKED_SERVER,'SELECT contactID, lastUpdatedDateTime FROM Contact')) AS DS
    ON (DT.ContactID = DS.ContactID)
    WHEN NOT MATCHED THEN
    INSERT VALUES (DS.contactID, DS.lastUpdatedDateTime)
    WHEN MATCHED AND (DT.lastUpdatedDateTime <> DS.lastUpdatedDateTime) THEN
    UPDATE SET DT.contactID = DS.contactID, DT.lastUpdatedDateTime = DS.lastUpdatedDateTime
    OUTPUT $Action Action_Out
    AS MERGE_OUT
    WHERE MERGE_OUT.Action_Out = 'UPDATE';
    GO
    When I run this query I am getting the following errors referring to the columns in the SELECT:
    Msg 207, Level 16, State 1, Line 2
    Invalid column name 'contactID'.
    Msg 207, Level 16, State 1, Line 2
    Invalid column name 'lastUpdatedDateTime'.
    Am I missing something obvious as I can't quite work out why? The Target and source definitely exist with the specified columns present in both...

    Hello,
    The issue is because you are not outputting those values from the inner merge statement. All you are currently outputting is the action, so the error is correct as those columns do not exist.
    Change your output portion to be:
    OUTPUT inserted.contactID, inserted.lastUpdatedDateTime, $Action Action_Out
    Then it should work.
    Sean Gallardy | Blog | Microsoft Certified Master

  • INSERT INTO with SELECT CASE

    Hi all,
    I've this problem: insert into Table_A some values from Table_B... with one derivated/calculated filed.
    Example
    INSERT INTO Table_A (id_part, vendtype, origin, proven, desc)
    SELECT id_p, vendt, origin, proven, <calculate/derivated_field_desc>
    FROM Table_BThe rules for calculate the field desc inserted on Table_A are 1. IF vendt='V' AND origin='N' AND proven!='04' THEN ---> desc='TRANSFER'
    2. IF vendt='M' AND origin='N' AND proven!='04' THEN ---> desc='IMPORTED'
    3. IF proven='04' THEN ---> desc='BAD'So what I use in <calculate/derivated_field_desc> on my INSERT INTO statement??
    Can I use a SELECT CASE WHEN... or only DECODE is a right solution?
    Edited by: trebbia on 15-nov-2010 12.51

    Ok,
    I prefer use select case when... because trasform my rules in DECODE statement is more difficult
    So I write: INSERT INTO Table_A (id_part, vendtype, origin, proven, desc)
    SELECT id_p, vendt, origin, proven, SELECT CASE WHEN vendt='V' AND origin='N' AND proven!='04' THEN 'TRANSFER'
                                                    WHEN vendt='M' AND origin='N' AND proven!='04' THEN 'IMPORTED'
                                                    WHEN proven='04 THEN 'BAD'
                                                    END
    FROM Table_BObtain this error:
    40/56 PL/SQL: ORA-00936: missing expression
    33/1 PL/SQL: SQL Statement ignored
    PS: I use '<' '>' and not '!=' in my code... here not prompted correctly '<''>'
    Edited by: trebbia on 15-nov-2010 13.04
    Edited by: trebbia on 15-nov-2010 13.06

  • JDBC Receiver adapter ( INSERT/UPDATE with SELECT)

    Is it possible to have following kind of SQL Statement comming out JDBC Receiver Adatpter. If yes what would be the corresponding XML Document format for this.
    UPDATE suppliers 
    SET supplier_name = ( SELECT customers.name
    FROM customers
    WHERE customers.customer_id = suppliers.supplier_id)
    This condition also needs to be applied for Insert condition. Any pointer would be useful.
    Thanks,
    Samir

    Hi
    Refer this links,
    http://help.sap.com/saphelp_nw04/helpdata/en/22/b4d13b633f7748b4d34f3191529946/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/b0/676b3c255b1475e10000000a114084/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/2e/96fd3f2d14e869e10000000a155106/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/4d/8c103e05df2e4b95cbcc68fed61705/frameset.htm
    Regards,
    Suryanaryana

  • Insert Statement with JAVA

    I'm trying to insert Data into an access table. Moreover, I'm able to retrieve data on the table (to prove that I'm connecting fine)... I just can't insert Data
    try
                   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                   Connection con = DriverManager.getConnection(url);
                   System.out.println("Connection Successful");
                   Statement st = con.createStatement();
                   ResultSet rs = st.executeQuery("INSERT INTO Worker VALUES ('" + strDataName + "', '" + strDataDept + "', '" + dblDataHoursWrked + "', '" + dblDataHourlyWage + "', '" + dblDataPay + "')");
                   rs.close();
                   processQuery(con, st); // does what is nessesary to complete the current request
                   st.close();
                   con.close();
              catch (ClassNotFoundException e)
                   System.out.println("failed to load OBCD driver. ");
                   System.out.println ("Class errros: " + e);
              }For some reason, I don't have any errors Its just not inserting the data to the table.. any help would be greatly appreciated

    try
                   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                   Connection con = DriverManager.getConnection(url);
                   System.out.println("Connection Successful");
                   Statement st = con.createStatement();
                   //ResultSet rs = st.executeQuery("SELECT INTO Worker VALUES ('" + strDataName + "', '" + strDataDept + "', '" + dblDataHoursWrked + "', '" + dblDataHourlyWage + "', '" + dblDataPay + "')");
    Here use update query "executeUpdate("UPDATE TABLENAME SET COLUMN=VALUE,.........................WHERE CANDITION );
    rs.close();
                   processQuery(con, st); // does what is nessesary to complete the current request
                   st.close();
                   con.close();
              catch (ClassNotFoundException e)
                   System.out.println("failed to load OBCD driver. ");
                   System.out.println ("Class errros: " + e);
    U CHECK IT ,I THINK IT WILL WORK

  • Insert statement with using the value in struct

    Would like to clarify, i use cursor for selecting data from tableA and fetch the cusor into a struct. So the struct is holding all my data then i would like to insert all this data into tableB. Can i direct insert the data with the stuct?
    eg: INSERT INTO tableB VALUES (:mystruct);

    See http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/collections.htm#i20479
    chapter Inserting PL/SQL Records into the Database
    BTW Update you should use cautiously, not to update all PKses and FKses see http://www.oracle-developer.net/display.php?id=418 for some examples.
    Gints Plivna
    http://www.gplivna.eu

  • Error Running SQL Insert Statement with & in it

    Eclipse: 3.5 Galileo on Window Vista
    OEPE: 11gR1 11.1.14
    I get "invalid character" error when running this query in Eclipse:
    INSERT INTO RECIPE (recipeId, name1, name2, recipeCategoryId) VALUES (2, 'Chunky Chicken Egg Rolls', 'No take&ndash;out joint can compare!', 1);
    I've tried multiple ways trying to escape the & character, but nothing worked. What is the right way of executing SLQ via OEPE plugin that have special characters in them.
    FYI, the following worked fine in Oracle 10g iSQL*Plus:
    SET DEFINE ~;
    INSERT INTO RECIPE (recipeId, name1, name2, recipeCategoryId) VALUES (2, 'Chunky Chicken Egg Rolls', 'No take&ndash;out joint can compare!', 1);
    Thanks,
    Dmitri.

    Very interesting case. I tried your insert stmt and got the same error. I finally got it to work by removing the ";" in the end. It looks like the jdbc driver choked up while there is ";" in the middle of the pl/sql statement and ";" in the end. If you remove ";" in the "No take&ndash;out joint can compare!" string and leave the ";" in the end, it also works!
    I don't quite understand why Oracle JDBC dirver behaves like that. I'll investigate it more and we may need to do some preprocessing on the pl/sql statement before sending it to the driver. At the mean time, just remove the ";" in the end if your string literal contains ";".
    Please let me know whether this workaround works for you. Thanks!
    Shenxue

  • Insert Statement with Where Clause

    I m using this but giving error "Encounter the Symbol "Where" when expecting one of the following.
    Code is :-
    insert into dum (cost_no, c_size, cmt1, cmt2, cmt3)
    values (:sizecost.cost_no, :sizecost.c_size, :bottomcost.cmt1, :bottomcost.cmt2, :bottomcost.cmt3)
    where :sizecost.cost_no := :costmain.cost_no;

    Something like this ?
    insert into dum (cost_no, c_size, cmt1, cmt2, cmt3)
    select :sizecost.cost_no, :sizecost.c_size, :bottomcost.cmt1, :bottomcost.cmt2, :bottomcost.cmt3
    from dual
    where :sizecost.cost_no := :costmain.cost_no;Nicolas.
    : all these are items on form Ok, why not try to use the Form Forum ?
    Message was edited by:
    N. Gasparotto

Maybe you are looking for

  • What is the best way to sort and separate tv show episodes

    Hi Everyone, I have some tv shows converted for my apple tv but they go over a period of several  seasons, how can I merge and separate them by season year  and  have them in itunes as each season  with  only those shows for that season ..  any tips

  • Port sequence and file processing in SAP MDM

    Hi All, Can any one let me know how port sequence mechanism works in MDM 7.1? As per my understanding in SAP MDM no one files from different ports are processed simultaniously...Suppose if you have three different ports A,B,C and the scenairo is as b

  • How to rewind Movie Clip

    Is it possable to rewind a movie clip (play it backwards)? thaks in advance, Nick D.

  • Can you help with this interesting matter??

    I just put a few old music projects from my USB key on to my computer, then opened them into Logic pro 8 which has been working fine and dandy recently. as soon as i did this a problem occurred. I have subsequently deleted those projects but the proj

  • New InitialContext() gets String index out of bounds

    Context ctx = null; ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("jdbc/datasource"); connection = dataSource.getConnection(); I get: String index out of range: -1 weblogic.jndi.Environment.getContext(Environment.java:30