select from one table, insert into another table oracle sql query
Asked Answered
T

5

33

I am trying to select data from one table
and insert the data into another table

    SELECT ticker FROM tickerdb;

Using OracleSql I am trying to
get the ticker symbol "GOOG" from the tickerdb table,
and insert the t.ticker into the stockdb table.

select from tickerdb table --> insert into quotedb table

    INSERT INTO quotedb
    (t.ticker, q.prevclose, q.opn, q.rnge,
    q.volume, q.marketcap, q.dividend, q.scrapedate)
    VALUES (?,?,?,?,?,?,?,?,SYSDATE)
    tickerdb t inner JOIN quotedb q
    ON t.ticker = q.ticker
Triangulate answered 28/11, 2013 at 8:44 Comment(0)
L
64

From the oracle documentation, the below query explains it better

INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

You can read this link

Your query would be as follows

//just the concept    
    INSERT INTO quotedb
    (COLUMN_NAMES) //seperated by comma
    SELECT COLUMN_NAMES FROM tickerdb,quotedb WHERE quotedb.ticker = tickerdb.ticker

Note: Make sure the columns in insert and select are in right position as per your requirement

Hope this helps!

Lining answered 28/11, 2013 at 8:52 Comment(1)
Can you update the link please ? it's best to quote into you answer if possible to avoid this.Longerich
G
18

You can use

insert into <table_name> select <fieldlist> from <tables>
Gull answered 28/11, 2013 at 8:48 Comment(1)
INSERT INTO table1 SELECT * FROM table2 works for me in old Oracle10g2 where table1 and table2 have the same table schemaPlatinotype
A
8

try this query below:

Insert into tab1 (tab1.column1,tab1.column2) 
select tab2.column1, 'hard coded  value' 
from tab2 
where tab2.column='value';
Angiosperm answered 28/10, 2016 at 10:25 Comment(1)
Good example which also shows how to create hard coded values in destination table.Tillage
A
4

You will get useful information from here.

SELECT ticker
INTO quotedb
FROM tickerdb;
Ansate answered 28/11, 2013 at 8:50 Comment(0)
L
0

I was getting this error and realized I had left out the Connection Open command. It worked perfectly after that.

Levin answered 15/8, 2023 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.