Explain Codes LogoExplain Codes Logo

Oracle insert from select into table with more columns

sql
best-practices
join
data-type-compatibilities
Alex KataevbyAlex Kataev·Jan 8, 2025
TLDR

Insert with specified columns:

INSERT INTO target (col1, col2, extra) SELECT col1, col2, 'default' FROM source;

This ensures only the required data gets inserted where source has fewer columns than target. The 'default' value is for the extra column absent in source.

Providing default values

When dealing with extra columns in the target table, here are some strategies for providing default values:

  1. Fixed value placement: Assign a constant value directly like a '0' or 'default' for those extra columns during the SELECT clause.
-- You know, '0' is seriously the most patient number, it has been at '0' for centuries and doesn't mind. 😆 INSERT INTO target (col1, col2, extra) SELECT col1, col2, '0' FROM source;
  1. Using COALESCE or NVL: Handle NULL values from source column by providing a default value. Keeps your data clean like a fresh haircut. 💇‍♂️
-- Because no one likes NULL coming uninvited to the party! 🥳 INSERT INTO target (col1, col2, extra) SELECT col1, col2, COALESCE(source.column, 'default') FROM source;
  1. Relying on table defaults: If your table has a default value set for a column, skip that column from your INSERT statement. Oracle will take care of the rest!
-- Letting Oracle do the heavy lifting 🏋️‍♂️ INSERT INTO target (col1, col2) SELECT col1, col2 FROM source;

Aligning target columns

To maintain peace with your DBA, ensure that your SELECT columns and target columns are on the same page:

  • Rename source columns with the AS keyword to match the target, if they differ.
  • Read up on data type compatibilities to avoid disappointments at runtime.

Checking privileges and syntax

Ensure you possess the necessary privileges to perform an INSERT on the target table. Failing to do so is like trying to withdraw money from someone else's bank account! 🏦

Also, keep Oracle syntax rules on your bookmarks. A misplaced keyword or identifier can lead to some severe head-scratch sessions.

Harnessing the power of INSERT...SELECT

Here is a practical scenario: You're tasked with inserting sales data into a report table. The report table has an extra column for 'bonus' calculations, which doesn't exist in the sales data. Here's your golden opportunity to shine:

-- "Extra value in the bag for me? Why, certainly!" 😎 INSERT INTO sales_report (sale_id, sale_date, amount, bonus) SELECT id, sale_date, total_amount, 0 FROM sales_data;

Explicit specifying '0' for bonus ensures clean column alignment and sets the stage for later bonus decimals.

Squashing bugs before they hatch

Here are some precautions to avoid that dreaded ORA-error message popping up:

  • Be watchful for ambiguous columns when joining tables in SELECT.
  • Always structure your queries with qualified column names when dealing with multiple tables.
  • Finally, always test before deploying. Your production data will thank you.