in reply to INSERT with RETURNING INTO clause

Your 'insert into' specifies 2 values, but your 'create table' lists three columns. I think this should throw a mismatch error. But you are not checking for errors, except in the connect, and even there you would be displaying the wrong error message if there were a problem ($! is not a DBI error message, $DBI::errstr is)...try setting RaiseError to 1, and specify column names in your insert statement.

Replies are listed 'Best First'.
Re^2: INSERT with RETURNING INTO clause
by Akoya (Scribe) on Jul 06, 2007 at 17:50 UTC

    Thank you for your reply. It was much more useful than the flaming going on in the chatterbox. The solution is as follows:

    1. Change the sql, so that there are 3 placeholders in the values clause:

    my $sql = qq { INSERT INTO peck_test VALUES ( ?,?,? ) RETURNING my_id INTO ? };

    2. Change the binding as follows:

    $sth->bind_param (1, $id, SQL_NUMERIC); $sth->bind_param (2, $name, SQL_VARCHAR); $sth->bind_param (3, $desc, SQL_VARCHAR); $sth->bind_param_inout(4, \$id, SQL_NUMERIC);
    Thanks again, Akoya

      I would also name the fields in the insert statement. It helps self-document and would simplify the command since you could omit the field populated by the trigger. And, it will keep you from having to revise this code should another field be added to the table. Also, if one of the fields you use is renamed or removed, the error messages might be more explicit.

    A reply falls below the community's threshold of quality. You may see it by logging in.