Use query within the value of another query

1

Is it possible - with some syntax adaptation - to fulfill the purpose of the following query?

insert into X (a, b) values(16, select c from Y where d = e)
    
asked by anonymous 19.05.2015 / 21:27

3 answers

1

You can use quotation marks for such a requirement, thus creating a Subquery .

A Subquery is basically one query within another. When working with subquerys, we should beware of certain points, such as the case that a subquery can return more than one value, causing your sql query to generate an error (my experience is based only on databases mssql and oracle , so I do not know the behavior of other databases.)

Example:

insert into X (a, b) values(16, (select c from Y where d = e))
    
19.05.2015 / 21:29
1
insert into X (a, b) 
select 16, c from Y where d = e

You can do that too, without the word values .

    
19.05.2015 / 21:34
1

You do not even have to put the word Values, just that the values are inside the select. So:

insert into X (a, b) (select "16", c from Y where d = e)
    
19.05.2015 / 21:34