Re: [dbi] prepared insert syntax error on exec

From: Gary Stainburn (gary.stainburn_at_ringways.co.uk)
Date: 10/15/03


To: "andy law (RI)" <andy.law@bbsrc.ac.uk>, "'dbi-users@perl.org'" <dbi-users@perl.org>
Date: Wed, 15 Oct 2003 14:43:11 +0100

On Wednesday 15 Oct 2003 1:43 pm, andy law (RI) wrote:
> > 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

Hi Andy,

this would work, but would mean that I would have to prepare every time - or
at least every time the filename changed - which defeats the object of the
prepare/execute, i.e. improved efficiency by eliminating the parse/plan
stage.

I've got round this by having two seperate prepares, one for each of the two
possible fields, and then passing the appropriate statement handle as an
argument to the procedure executing it.

Not very pretty and not very scalable, but at least a workaround for now.

>
> -------------
> 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

-- 
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)