Lets suppose I've to insert into a table with many fk, just to explain here below the wrong statement:
insert into mytable
values
(
somevalue
,somevalue
,select id from othertable1 where ...condition
,select id from othertable2 where ...condition
,select id from othertable3 where ...condition
)
so basically values to insert comes from different subqueries, is it possible to achieve such a behavior ?
Multiple row subquery returns one or more rows to the outer SQL statement. You may use the IN, ANY, or ALL operator in outer query to handle a subquery that returns multiple rows. Contents: Using IN operator with a Multiple Row Subquery.
Subqueries also can be used with INSERT statements. The INSERT statement uses the data returned from the subquery to insert into another table. The selected data in the subquery can be modified with any of the character, date or number functions.
You can specify up to 16 subqueries within a single SQL statement, and you can specify subqueries within a subquery. Subqueries run from last to first within the main SQL statement in which they appear.
Answer: A.A single-row subquery can return a maximum of one value. Multiple-column subqueries return more than one column to the outer query.
insert into mytable (columns)
select somevalue, somevalue, a.id, b.id, c.id
from
othertable1 a
cross join othertable2 b
cross join othertable3 c
where
a ... condition
b ... condition
c ... condition
Can you use a select statement to do the insert?
INSERT INTO MYTABLE
SELECT (SOMEVALUE,
SOMEVALUE,
T1.ID,
T2.ID
)
FROM ANOTHERTABLE T1
JOIN YETANOTHERTABLE T2
ON T1.BLAH = T2.BLAH
WHERE condition1...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With