cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: Support INSERT inside an INSERT

Open JackHerring opened this issue 9 years ago • 4 comments

Support for something like the following would be nice

INSERT INTO orders VALUES (@OrderId, (INSERT INTO customers VALUES (@Id, @Name) RETURNING id))

JackHerring avatar Nov 24 '17 11:11 JackHerring

cc: @dianasaur323 or @awoods187

jseldess avatar Nov 27 '17 15:11 jseldess

@awoods187 would be the right person here

dianasaur323 avatar Nov 29 '17 22:11 dianasaur323

@JackHerring thanks for your request. We will be unable to address this request in the near time period, however, I've noted it for the long-term roadmap.

awoods187 avatar Dec 05 '17 14:12 awoods187

@JackHerring I believe this feature is supported already in CockroachDB 1.1 and 2.0.

Two syntaxes are possible:

  • CockroachDB-specific: insert into ab(a,b) values (1, (select * from [insert into a(a) values(2) returning a]));
  • Using standard SQL CTEs: with v as (insert into a(a) values(2) returning a) insert into ab(a,b) values (1, (select * from v));

Let us know if you have any further question or comment.

knz avatar Apr 28 '18 15:04 knz