INSERT INTO SELECT Oracle PL/SQL faux pas

I’ve recently stumbled upon a bug that came down to the following piece of code:

INSERT INTO TABLE_A (
   SELECT ALPHA,
          GAMMA,
          BETA
   FROM TABLE_B
)
;

This is an INSERT query with respect to the column order of the table. In general the order of the columns in the table shouldn’t be important on our queries therefore the following more robust version of INSERT is guarding against ambiguity relying on the column order:

INSERT INTO TABLE_A (
   ALPHA,
   BETA,
   GAMMA
) VALUES
(  SELECT ALPHA,
          BETA,
          GAMMA
   FROM TABLE_B
)
;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s