Inserting data into a column from 2 different tables

Hi,
I need to insert data into a table using 2 other tables. The tables that contain data have identical column names.
Is using a UNION statement the only option?
Also, if I need to insert data into columns from only one of the either tables, how do i do it?
Thanks.

For future reference, "doesn't seem to work" is a rather generic description... Posting the particular error message will be quite helpful, though I'm reasonably confident that I know the particular problem here.
First, if only for sanity, you probably want to explicitly list the columns of the destination table in your INSERT statement.
Second, it doesn't make sense to have DISTINCT clauses in queries that are UNION-ed together. A UNION has to do a sort to remove duplicates already.
Third, the two queries you are UNIONing together have to return the same number of columns, with the same names, in the same order.
You probably want something like
INSERT INTO new_table( col1, col2, col3, col4, col5 )
  SELECT 'ABC'  col1,
         a.colA col2,
         a.colB col3,
         a.colC col4
         a.colD col5
    FROM table1 a
  UNION
  SELECT 'ABC'  col1,
         b.colA col2,
         b.colB col3,
         b.colC col4
         NULL   col5
    FROM table2 bJustin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC

Similar Messages

  • HOW TO INSERT DATA INTO SQL SERVER FROM MS ACCESS TABLE??

    NEED TO INSERT DATA INTO SQL SERVER FROM MS ACCESS TABLE.

    this is another method
    http://www.mssqltips.com/sqlservertip/2484/import-data-from-microsoft-access-to-sql-server/
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to insert  data into BLOB column  using sql

    Hi all,
    How to insert data into BLOB column directly using sql .
    create  table temp
      a blob,
      b clob);
    SQL> /
    Insert into temp  values ('32aasdasdsdasdasd4e32','adsfbsdkjf') ;
    ERROR at line 1:
    ORA-01465: invalid hex number
    Please help in this.Thanks,
    P Prakash

    see this
    How to store PDF file in BLOB column without using indirect datastore

  • INSERTING DATA INTO A SQL SERVER 2005 TABLE, WHICH HAS A IDENTITY COLUMN

    Hi All,
    I have to insert the data into a SQL SERVER 2005 Database table.
    I am able to insert the data into a normal SQL Server table.
    When I am trying to insert the data into a SQL Server table, which has a identity column (i.e. auto increment column in Oracle) I am getting error saying that can't insert value explicitly when IDENTITY_INSERT is set to OFF.
    Had anybody tried this??
    There are some SRs on this issue, Oracle agreed that it is a bug. I am wondering if there is any workaround from any one of you (refer Insert in MS-SQL database table with IDENTITY COLUMN
    Thanks
    V Kumar

    Even I had raised a SR on this in October 2008. But didn't get any solution for a long time, finally I had removed the identity column from the table. I can't to that now :).
    I am using 10.1.3.3.0 and MS SQL SERVER 2005, They said it is working for MS SQL SERVER TABLE, if the identity column is not a primary key and asked me to refer to note 744735.1.
    I had followed that note, but still it is not working for me.
    But my requirement is it should work for a MS SQL SERVER 2005 table, which has identity column as primary key.
    Thanks
    V Kumar

  • I want to insert data into a gta from a nested table

    Hi,
    I want to insert data into a global temporary table from nested table.
    How do i do it?

      cursor cc is select * from t1;
       TYPE temp_rec_tab IS TABLE OF temp_rec_tap_cur%ROWTYPE;
       TYPE rec_num_tab1 is table of temp_records_tap.record_num%type;
       v_test_tab     temp_rec_tab;
    ----   v_rec_num      num_tab;
       v_rec_num rec_num_tab1;  
       v_filename     temp_records_tap.file_name%TYPE;
       v_error_code   tap_reject.ERROR_CODE%TYPE;
       v_rej_value    tap_reject.field_rej%TYPE;
       v_count        NUMBER;
    BEGIN
       OPEN cc;
       LOOP
          BEGIN
             FETCH cc
             BULK COLLECT INTO v_test_tab LIMIT 1000;
             FORALL i IN v_test_tab.FIRST .. v_test_tab.LAST SAVE EXCEPTIONS
                INSERT INTO tt2
                     VALUES v_test_tab (i)
                  RETURNING       record_num
                BULK COLLECT INTO v_rec_num;
          EXCEPTION
             WHEN OTHERS
             THEN
          NULL;
          END;
          EXIT WHEN temp_rec_tap_cur%NOTFOUND;
       FORALL i in  v_rec_num.first..v_rec_num.last
       INSERT INTO record_num_session values v_rec_num(i); ------- v_num(i) INVALID IDENTIFIER ??????
       END LOOP;
    END;

  • Best way to insert data into a non indexed partitioned table?

    Hi,
    I've a rather basic question that I haven't been able to find an answer to so hope that someone may be able to help.
    We need to insert sales data into a partitioned table that is range partitioned on sales date. The incoming sales data can span a number of days, e.g. 31-JUL-2007 through to 04-AUG-2007 inclusive. I've come up with two approaches and would like to know which you would recommend and why?
    Approach 1: Load data into each individual partition using a statement like:
    INSERT /*+ APPEND */ INTO sales PARTITION (sales_20070731) SELECT * FROM sales_new WHERE sales_date = TO_DATE('31-AUG-2007','DD-MON-YYYY')
    Approach 2: Load data into the entire table using a statement like:
    INSERT /*+ APPEND */ INTO sales SELECT * FROM sales_new
    Thanks

    You need to compare both approaches by creating simple test case.
    But there is no advantages in approach 1until you will have index on sales_date column.
    With index these approaches are comparable and you can choose whatever best fits to your requirements.
    Best Regards,
    Alex

  • How to insert data into local database from oracle server

    I am new in C#.
    I am trying to design a local database in my C# project and I am trying to sync my database with our oracle server. I can login using oracle server. But I can't insert data into my local database from oracle server.
    anybody can help me ...............
    thanks..............................

    You can use SSIS package for that
    Start a new Integration Services project in Business  Intelligence Development Studio/ SSDT. Add a new SSIS package with a data flow task. Add a OLEDB source to connect to Oracle server and add a OLEDB Destination to connect to your local database.
    Select tables for source and destination. On executing package source data will get transferred to your local database.
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to update multiple columns from different tables using cursor.

    Hi,
    i have two tables test1 and test2. i want to udpate the column(DEPT_DSCR) of both the tables TEST1 and TEST2 using select for update and current of...using cursor.
    I have a code written as follows :
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
         LOOP
              FETCH C1 INTO v_mydept1,v_mydept2;
              EXIT WHEN C1%NOTFOUND;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
         END LOOP;
         COMMIT;
    END;
    The above code when run says that it runs successfully. But it does not updates the desired columns[DEPT_DSCR].
    It only works when we want to update single or multiple columns of same table...i.e. by providing these columns after "FOR UPDATE OF"
    I am not sure what is the exact problem when we want to update multiple columns of different tables.
    Can anyone help me on this ?

    oops my mistake.....typo mistake...it should have been as follows --
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    Now here is the upated PL/SQL code where we are trying to update columns of different tables --
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
    LOOP
    FETCH C1 INTO v_mydept1,v_mydept2;
    EXIT WHEN C1%NOTFOUND;
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    END LOOP;
    COMMIT;
    END;
    Please let us know why it is not updating by using using CURRENT OF

  • Insert data into the column which is having null values.

    Hi,
    I have a column called "Classification_CD" .This column is having NULL values.I want to insert the data into this column.
    I tried to write the query as follows. But it is showing the mesg error "SQL Error: ORA-01400 cannot insert NULL into ("ABC"."A_CMP_W"."A_CMP_SEQ_NUM")"
    Can any one please help me out to write query to insert the dat afor this.
    Thanks.

    I think you are taking about updating the null value.So you can do this..
    SQL> select * from table_a;
            ID SCHEDULED MARK                       PRID
             5 07-NOV-10 T05                           7
             6 18-SEP-10 T06                           8
             4 31-JAN-11 T02                           2
             1 18-JAN-11 T01                           2
             2 18-JAN-11 T02
             3 18-JAN-11 T03                           1
    6 rows selected.See that prid is Null for id 2
    SQL> update table_a
      2  set prid =10
      3  where id =2;
    1 row updated.
    SQL> commit;
    Commit complete.
    SQL> select * from table_a;
            ID SCHEDULED MARK                       PRID
             5 07-NOV-10 T05                           7
             6 18-SEP-10 T06                           8
             4 31-JAN-11 T02                           2
             1 18-JAN-11 T01                           2
             2 18-JAN-11 T02                          10
             3 18-JAN-11 T03                           1
    6 rows selected.
    SQL> But remember while updating u should choose a primary key column in
    where condition so that only desired row or record is updated..
    Regards
    Umesh

  • Columns from different tables displayed in an ADF table format

    I have a many to one relationship between 2 tables (checklist -> subcontract). I would like to display columns from each table on a web page in an ADF Read Only Table format. I created a method that returns the data I need from the tables. When I drag that method over to my page JDev I'm only allowed to select columns from either the checklist table OR the subcontract table. I need one column from the subcontract table and several columns from the checklist table. I'm using JDev 10.1.3.2 and an Oracle 11g database. Thank you in advance.
    -Wade

    Thank you for your response. I ended up creating a new getter in the Checklist table which refers to the column in the Subcontract table. Then I was able to create a table column for that returned value.
    Thanks again!
    -W

  • Copying column from different table

    I want to copy the column of one table1 to table2 column with the condition to add the existing data not to replace
    Regards

    Sure ->
    satyaki>>
    satyaki>> CREATE TABLE emp_source
      2  AS
      3     SELECT * FROM emp;
    Table created.
    satyaki>>
    satyaki>> SELECT COUNT(*) FROM emp_source;
      COUNT(*)
            14
    satyaki>>
    satyaki>> CREATE TABLE emp_target
      2  AS
      3     SELECT * FROM emp WHERE ROWNUM <= 8;
    Table created.
    satyaki>>
    satyaki>> SELECT COUNT(*) FROM emp_target;
      COUNT(*)
             8
    satyaki>>
    satyaki>> BEGIN
      2     MERGE INTO emp_target et
      3        USING ( SELECT * FROM emp_source ) es
      4        ON    ( et.empno = es.empno )
      5     WHEN MATCHED THEN
      6     UPDATE
      7        SET et.ename  = es.ename
      8        ,   et.sal    = es.sal
      9        ,   et.mgr    = es.mgr
    10        ,   et.deptno = es.deptno
    11     WHEN NOT MATCHED THEN
    12     INSERT
    13        ( et.empno, et.ename, et.sal, et.mgr, et.deptno )
    14        VALUES
    15        ( es.empno, es.ename, es.sal, es.mgr, es.deptno );
    16 
    17     DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQL%ROWCOUNT) || ' rows merged.');
    18  END;
    19  /
    14 rows merged.
    PL/SQL procedure successfully completed.Hope this will help.
    Regards.
    Satyaki De.

  • How to insert data into Oracle db from MySQL db through PHP?

    Hi,
    I want to insert my MySQL data into Oracle database through PHP.
    I can access Mysql database using mysql_conect() & Oracle database using oci_connect() through PHP.
    Now How can I insert my data which is in MySQL into Oracle table. Both table structure are exactly same.
    So I can use
    insert into Oracle_Table(Oracle_columns....) values(Select * from MySQL_Table);
    But again problem is I can't open both connections at the same time.
    So has anyone done this before or anyone having any other idea..
    Plz guide me...

    You can do it if you setup a ODBC Gateway between MYSQL and Oracle DB. Then you can directly read from MySQL table using DB links and insert into Oracle table in one single SQL Statement.
    Otherwise you need to fetch the data from MySQL Into variables in your PHP Program and then insert into Oracle after binding the variables to the insert statement for Oracle. Hope that is clear enough.
    Regards
    Prakash

  • ADF Mobile - insert data into oracle db from oracle adf mobile application

    Hi,
    I have to insert data from mobile into oracle database 11g(not in local sqlite database), can somebody tell me how to do it?
    Thanks,
    Anshul

    Hi, Anshul, one common way is to expose DB data as ADF BC, and then add a service interface on top of ADF BC. This would allow the ADF Mobile app to access server DB through ADF BC. There may be other mechanisms to access server-side DB - I think APEX also expose some REST interface to Oracle DB as well. Another option is to use TopLink - TopLink can also expose some REST services.
    Thanks,
    Joe Huang

  • SSIS - Exporting Data into flat files from Oracle Table as batchwise process

    Hi All,
    Thanks in advance.
    I have a Large Table in Oracle Database with some 3 Lakhs record. I need to fetch the 10,000 records for every iteration and export it into the flat file. This process should occur recursively until the table becomes empty.
    Hence, For every iteration on flat file to be generated with 10,000 records.
    Please help how to proceed further in SSIS.
    Thanks
    Pyarajan.S

    Yes, it always helps if your question doesn't specify the actual requirements...
    Use the FOR loop container to control the iterations of the data flow. For each run you read 10,000 rows from the table and dump them in a flat file. Either move the flat file, or use an expression on the flat file connection manager to give them dynamic
    file names.
    30 million rows is also not a problem by the way, it just takes a bit longer.
    MCSE SQL Server 2012 - Please mark posts as answered where appropriate.

  • Inserting data into relational views of object tables

    I hope someone could help me to solve the following problem:
    I would like to design an object-relational database in O8i. Unfortunatly the client-software (GIS) can only understand relational data (except the object type 'geometry'). So I created relational views on my object tables. But now it is not possible to insert data in those views. Do I have to create triggers on those views for inserting data ???
    Nevertheless: Updating data in views makes no problems.
    Thank you for reading this and I look foreward to hearing from you soon.
    Christian Heil

    Hi Mohammed,
    I guess following is your requirement
    List1 tile is attached to "BOList1". On button press you wanted the data to be persisted to Business object "BOList1History"
    I assume that you have created "BOLIst1History" properly by associating a write Bdoc etc etc.
    Write a method in Business object "BOList" called ZUpdateHistory. This new method should contain code for creating an instance of "BOLIST1History" and fill the property values as per your requirement. Call this method from the Button press event as follows
    anchor.bo.ZUpdateHistory
    Hope this helps
    Regards
    Ganesh Datta

Maybe you are looking for