Yii2: Add INSERT INTO SELECT support

Created on 6 May 2015  路  7Comments  路  Source: yiisoft/yii2

Description of syntax can be found here.

Example (taken from here):

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;

Can't find it in core. QueryBuilder's insert method returns concatenated string with VALUES:

return 'INSERT INTO ' . $schema->quoteTableName($table)
    . ' (' . implode(', ', $names) . ') VALUES ('
    . implode(', ', $placeholders) . ')';
db enhancement

Most helpful comment

How if this feature added to batchInsert?

     * @param string $table the table that new rows will be inserted into.
     * @param array $columns the column names
     * @param array|Query $rows the rows to be batch inserted into the table
     * @return $this the command object itself
     */
    public function batchInsert($table, $columns, $rows)
    {

All 7 comments

Well, w3schools is not a good resource to refer to for a specific syntax.

Need to check the manuals of all Yii supported DBMS, here are some:

How if this feature added to batchInsert?

     * @param string $table the table that new rows will be inserted into.
     * @param array $columns the column names
     * @param array|Query $rows the rows to be batch inserted into the table
     * @return $this the command object itself
     */
    public function batchInsert($table, $columns, $rows)
    {
if (is_array($info)) {..........}
if (is_array($result) && isset($result[0])) {........}


if (isset($info[0])) {..........}
if (isset($result[0])) {........}

what do you want to say?

there is an issue regarding this added function.

I tried it like insert into ... select ... from ... where ...

An error says [Microsoft][ODBC Driver 11 for SQL Server]COUNT field incorrect or syntax error

I tried removing where condition and it works

I did it like this:

Yii::$app->db->createCommand()->insert(Class1::tableName(), Class2::find()
->select([ ... ])
->where([ 'id' => 1 ])
)->execute()

Please create a separate issue and describe everything in detail: how to reproduce the issue, what SQL is generate, what SQL is expected. Thanks.

Please see #13807

Was this page helpful?
0 / 5 - 0 ratings