Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle insert from select into table with more columns

Tags:

sql

oracle

I want to insert to a table from a select statement, however, there are 3 columns returned from the select statement and the table has 4 columns, I would like to add 0 for all rows in the extra column. Can anyone give me a sample SQL query for that?

Thank you!

like image 548
Alan Han Avatar asked Jun 01 '12 00:06

Alan Han


People also ask

How do you select and insert in the same table?

The SQL INSERT INTO SELECT StatementThe 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.

How do I insert a row 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.


2 Answers

Just add in the '0' in your select.

INSERT INTO table_name (a,b,c,d)     SELECT        other_table.a AS a,        other_table.b AS b,        other_table.c AS c,        '0' AS d     FROM other_table 
like image 108
Matt Dodge Avatar answered Oct 01 '22 10:10

Matt Dodge


Put 0 as default in SQL or add 0 into your area of table

like image 42
Andrew Avatar answered Oct 01 '22 10:10

Andrew