withExpression method

MssqlInsertSelect withExpression(
  1. MssqlCte cte
)

Adds a common table expression to the statement.

Declared here rather than on query because T-SQL puts WITH at the start of the statement, before INSERT — not between INSERT and SELECT, which is where a query that carried its own would end up.

Implementation

MssqlInsertSelect withExpression(MssqlCte cte) {
  for (final existing in ctes) {
    if (existing.name.toLowerCase() == cte.name.toLowerCase()) {
      throw ArgumentError.value(
        cte.name,
        'cte',
        'The WITH clause already names an expression "${existing.name}". '
            'Two expressions of one name cannot both be referred to.',
      );
    }
  }
  return MssqlInsertSelect._(source, columns, query, <MssqlCte>[
    ...ctes,
    cte,
  ], output);
}