Search code examples
sqloracle-databaseinsert-select

Oracle SQL: INSERT with SELECT


i have a query like this:

insert into book ('book_id', 'category_id', 'book_name', 'buy_price', 'sell_price') values ('B-001, 'BOM-001', 'Tarzan', 200, 300);

is it possible to get the category_id from another table which is category table using select and condition where category_name = 'adventure' so i get the BOM-001? can anyone give me a sample of that query ?

thank you


Solution

  • This would look like:

    insert into book (book_id, category_id, book_name, buy_price, sell_price)
        select ?, c.category_id, ?, ?, ?
        from category c
        where c.category_name = ?;
    

    The ? are placeholders for your constant values. Note that there are no single quotes around the column names for the insert.