Search code examples
couchbaseinsert-intosql++insert-select

N1QL - Insert-Select


I'm trying to execute an insert-select statement in N1QL (inserting documents that their key/value are the result of a select statement) and I'm failing to understand the syntax.

I tried executing:

insert into tempbucket (KEY payload.id,VALUE select * from default where payload.fooId in [100,101 ] )  RETURNING * ;

in some variations but nothing worked.

Edit: The SELECT statement is

select * from default where payload.fooId in [100,101 ]

The KEY of the documents I want to create is the value of the field key and the VALUE is the entire JSON of the SELECT statement mentioned above.


Solution

  • The KEY and VALUE must reference expressions from your query.

    INSERT INTO tempbucket (KEY d.`key`, VALUE d)
    SELECT d
    FROM default d
    WHERE payload.fooId IN [100,101 ]
    ;