Skip to content

How to insert multiple rows in one query with some fields default for some rows? #628

@CJCombrink

Description

@CJCombrink

Hi,

I am trying to insert multiple rows at a time and some rows should use the default value for specific columns.

I am using insert_into with calls for values.add for each row to add but now handling the fields that should be default is not as easy.

        auto query = sqlpp::postgresql::insert_into(t_table)
                         .columns(t_table.id,
                                  t_table.name,
                                  t_table.description)
                         .returning(t_table.id);

        for (const auto& row: rows)
        {
            query.values.add(t_table.id = row.id? row.id: sqlpp::default_value, // << This line does not compile 
                             t_table.name       = row.name,
                             t_table.description= row.description);
        }

I am aware of dynamic_insert_into and use it in many places, but not sure if it supports adding multiple rows as above.

I am happy with any one of the following answers:

  1. Telling me it is possible in sqlpp11 and giving some guidance on achieving this
  2. Telling me it is not possible but it is possible in sqlpp23
  3. Telling me it is neither possible in sqlp11 nor sqlpp23 and putting it onto a "feature request" list for sqlpp23

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions