Possible to do INSERT with some columns from SELECT query and some columns with specific values?

All we need is an easy explanation of the problem, so here it is.

Is it possible to do an one INSERT statement where some values are from a SELECT query and some from specific values?

Trying to do something like this where cust_id, rep_id are from a select query and sku_ordered is a specified value:

    INSERT into ORDERS (cust_id, rep_id, sku_ordered='ABC123') 
     SELECT id, 
            sales_rep 
     from CUSTOMERS 
     where cust_name = 'ABC Corp';

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Literal must be a column in output list simply:

INSERT into ORDERS (cust_id,   rep_id , sku_ordered) 
SELECT                id   , sales_rep,   'ABC123'
from CUSTOMERS 
where cust_name = 'ABC Corp';

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply