Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select from one table, insert into another table oracle sql query

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 
like image 812
user3003451 Avatar asked Nov 28 '13 08:11

user3003451


People also ask

How can I insert data from one table to another in Oracle?

The Oracle INSERT INTO SELECT statement requires the data type of the source and target tables match. If you want to copy all rows from the source table to the target table, you remove the WHERE clause. Otherwise, you can specify which rows from the source table should be copied to the target table.

How do I pass values from one table to another in SQL?

The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.

Can we use insert and select together?

You can use a select-statement within an INSERT statement to insert zero, one, or more rows into a table from the result table of the select-statement. The select-statement embedded in the INSERT statement is no different from the select-statement you use to retrieve data.


1 Answers

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!

like image 69
Keerthivasan Avatar answered Sep 22 '22 05:09

Keerthivasan