RE: [dbi] prepared insert syntax error on exec

From: Jeffrey Seger (Jeffrey.Seger_at_fairchildsemi.com)
Date: 10/15/03


To: "'dbi-users@perl.org'" <dbi-users@perl.org>
Date: Wed, 15 Oct 2003 09:55:56 -0400

And in case you have to iterate through a list of columns....

my $sth;
foreach my $col(@cols)
{
  $sth = $dbh->prepare("insert into mytable (col1, col2, $col) values
(?,?,?)");
  $sth->execute($val1,$val2,$val3);
}

Obviously this code is incomplete...just to illustrate that when you
change the column name, you need a new statement handle. The SQL parser
in the RDBMS can't successfully parse the statement without knowing that
the column names are all valid.

____________________________
Jeff Seger
Fairchild Semiconductor
jeffrey.seger@fairchildsemi.com
____________________________

"andy law (RI)" <andy.law@bbsrc.ac.uk>
10/15/2003 08:43 AM

 
        To: "'dbi-users@perl.org'" <dbi-users@perl.org>
        cc:
        Subject: RE: [dbi] prepared insert syntax error on exec

> I could be wrong but I didn't think you could use parameters
> to change the SQL
> which you appear to be doing by using ? where a column name
> is expected.

In which case, why is the prepare statement not failing?

Gary:

Will the following not work?

my ($fieldName) = 'nsmacc';

my ($stmt) = sprintf( "insert into names (nid, ncname, %s) values (?, ?,
?);", $fieldName);
my $cinh = $dbh->prepare( $stmt);
$cinh->execute( 1, 'test name', 'ACC01');

Later,

Andy

-------------
Yada, yada, yada...

The information contained in this e-mail (including any attachments) is
confidential and is intended for the use of the addressee only. The
opinions expressed within this e-mail (including any attachments) are the
opinions of the sender and do not necessarily constitute those of Roslin
Institute (Edinburgh) ("the Institute") unless specifically stated by a
sender who is duly authorised to do so on behalf of the Institute.

> -----Original Message-----
> From: Martin J. Evans [mailto:martin@easysoft.com]
> Sent: 15 October 2003 12:44
> To: dbi-users@perl.org
> Subject: RE: [dbi] prepared insert syntax error on exec
>
>
> I could be wrong but I didn't think you could use parameters
> to change the SQL
> which you appear to be doing by using ? where a column name
> is expected.
>
> Martin
> --
> Martin J. Evans
> Easysoft Ltd, UK
> Development
>
> On 15-Oct-2003 Gary Stainburn wrote:
> > Hi folks,
> >
> > can anyone see why this isn't working:
> >
> > my $cinh=$dbh->prepare("insert into names (nid, ncname, ?)
> values (?, ?,
> > ?);");
> > $cinh->execute('nsmacc',1,'test name','ACC01');
> >
> > produces the error message:
> >
> > ERROR: parser: parse error at or near "'"
> >
> > nid is an int4 primary key
> > ncname is a varchar(50) company name
> > nsmacc is a varchar(5) account number field
> >
> > --
> > Gary Stainburn
> >
> > This email does not contain private or confidential material as it
> > may be snooped on by interested government parties for unknown
> > and undisclosed purposes - Regulation of Investigatory
> Powers Act, 2000
>



Relevant Pages

  • Re: [dbi] prepared insert syntax error on exec
    ... >> to change the SQL ... > Yada, yada, yada... ... > sender who is duly authorised to do so on behalf of the Institute. ... >> Martin J. Evans ...
    (perl.dbi.users)
  • RE: [dbi] prepared insert syntax error on exec
    ... > to change the SQL ... Yada, yada, yada... ... > Martin J. Evans ... >> Gary Stainburn ...
    (perl.dbi.users)
  • Re: Incorrect syntax near nvarchar.
    ... SQL Management Studio Express has an activity monitor. ... <ItemTemplate> ... protected void GridView1_OnRowUpdating(Object sender, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Incorrect syntax near nvarchar.
    ... How do I do that on MS sql express 2005? ... <ItemTemplate> ... protected void GridView1_OnRowUpdating(Object sender, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Incorrect syntax near nvarchar.
    ... Run SQL Profiler to see the actual SQL syntax being sent ... <ItemTemplate> ... protected void GridView1_OnRowUpdating(Object sender, ...
    (microsoft.public.dotnet.framework.aspnet)