Skip to content

Conversation

@egawata
Copy link

@egawata egawata commented Feb 8, 2023

Since MySQL 8.0.20, use of VALUES() function in ON DUPLICATE KEY UPDATE clause has been deprecated, and throw warnings:

INSERT INTO users ( age, id, name ) VALUES ( ?, ?, ? ), ( ?, ?, ? ) ON DUPLICATE KEY UPDATE age = VALUES( age ), id = VALUES( id ), name = VALUES( name )
Warning : 1287 : 'VALUES function' is deprecated and will be removed in a future release. Please use an alias (INSERT INTO ... VALUES (...) AS alias) and replace VALUES(col) in the ON DUPLICATE KEY UPDATE clause with alias.col instead

Instead, an alias can be used for that usage since MySQL 8.0.19.

INSERT INTO users ( age, id, name ) VALUES ( ?, ?, ? ), ( ?, ?, ? ) AS new ON DUPLICATE KEY UPDATE age = new.age, id = new.id, name = new.name

See also: MySQL :: MySQL 8.0 Reference Manual :: 13.2.7.2 INSERT ... ON DUPLICATE KEY UPDATE Statement

This PR adds a 'use_alias' option to use alias instead of 'VALUES()` function.

use DBI;
use SQL::Abstract;
use SQL::Abstract::Plugin::InsertMulti;

my $dbh = DBI->connect(...);

my $sql = SQL::Abstract->new();
my ($stmt, @bind) = $sql->update_multi('users', [
    +{ id => 1, name => 'Alice', age => 18 },
    +{ id => 2, name => 'Bob', age => 22 },
], { use_alias => 1 });

print "stmt = |$stmt|\n";

$dbh->do($stmt, undef, @bind);

# if warnings are detected, print them.
if ($dbh->{mysql_warning_count}) {
     my $warnings = $dbh->selectall_arrayref('SHOW WARNINGS');
     for my $w (@$warnings) {
        say (join ' : ', @$w);
     }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant