Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:29:05+00:00 2026-05-26T15:29:05+00:00

I use following query to insert a new row: insert into table1 (c1, c2,

  • 0

I use following query to insert a new row:

insert into table1 (c1, c2, c3) (select c1, c2, c3 from table2 where some_condition)

This works finely if there is a row in table2 that satisfies some_condition. But if there are no rows, nothing is inserted.

Are there any way to specify default values to insert if select returns empty result set? I want to do that in one sql query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:29:05+00:00Added an answer on May 26, 2026 at 3:29 pm

    This isn’t very pretty, but it does what you want, You’d need to test with your environment to see if it performs well enough

    SQL> drop table so_tgt;
    
    Table dropped.
    
    SQL> 
    SQL> create table so_src (
      2   c1 varchar2(6)
      3  ,c2 varchar2(6)
      4  ,c3 varchar2(6)
      5  );
    
    Table created.
    
    SQL> 
    SQL> insert into so_src values ( 'foo','bar','moo' );
    
    1 row created.
    
    SQL> 
    SQL> create table so_tgt as select * from so_src where 1 = 0;
    
    Table created.
    
    SQL> 
    SQL> /* Test for existing row insert */
    SQL> insert into so_tgt
      2  with x as ( select s.*, 1 as r
      3          from so_src s
      4          where c1='foo'
      5          union
      6          select 'x','y','z',0 as r /* DEFAULT VALUES */
      7          from dual )
      8  select c1,c2,c3
      9  from x
     10  where r = ( select max(r) from x ) ;
    
    1 row created.
    
    SQL> 
    SQL> select * from so_tgt;
    
    C1     C2     C3
    ------ ------ ------
    foo    bar    moo
    
    SQL> truncate table so_tgt;
    
    Table truncated.
    
    SQL> 
    SQL> /* Test for default row insert */
    SQL> insert into so_tgt
      2  with x as ( select s.*, 1 as r
      3          from so_src s
      4          where c1='far'
      5          union
      6          select 'x','y','z',0 as r /* DEFAULT VALUES */
      7          from dual )
      8  select c1,c2,c3
      9  from x
     10  where r = ( select max(r) from x ) ;
    
    1 row created.
    
    SQL> 
    SQL> select * from so_tgt;
    
    C1     C2     C3
    ------ ------ ------
    x      y      z
    
    SQL> truncate table so_tgt ;
    
    Table truncated.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example i use following command to find a record SELECT `users`.`mail` FROM `users`
I'm trying to perform an INSERT INTO statement like the following: insert into table1
I use the following XPATH Query to list the object under a site. ListObject[@Title='SomeValue']
I use the following mysql query, DELIMITER $$ DROP PROCEDURE IF EXISTS `allied`.`aboutus_delete`$$ CREATE
I would like to use following sql to avoid constructing sql dynamically: SELECT CommentID,
I use the following statement prepared and bound in ODBC: SELECT (CASE profile WHEN
I have the following code: public function createNewGuide($userID,$guideName) { $sql = INSERT INTO myTable(name,
I need help with the following query. create table #table1 (id int not null
Currently looking to extract data from 2 tables and insert into another. Tables as
Im running the following query from my PhpMyadmin sql tab CREATE TRIGGER testTrigger AFTER

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.