Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:57:22+00:00 2026-06-12T06:57:22+00:00

I’m building a dynamic SQL statement, that will insert one or more sets of

  • 0

I’m building a dynamic SQL statement, that will insert one or more sets of VALUES via a prepared DBI statement, my question is this:

Since I have a dynamic number of VALUES sets, and I will need to add as many ( ?, ?, ?),( ?, ?, ?) etc as necessary to extend the statement INSERT INTO `tblname` ( $columnsString ) VALUES in order to submit only one query using placeholders and bind values- is this the preferred method(most efficient, etc., – reasoning behind efficiency would be helpful in your answer if possible) or should I just be building this as a query string with sprintf and dbh->quote()?

(As a little extra information: I’m actually using AnyEvent::DBI right now, which only exposes placeholders & bind values and not the quote() method so this wouldn’t be easy for me to accomplish without creating another straight DBI $dbh and using another db server connection just to use the quote() method, or without altering the AnyEvent::DBI module myself.)

Normally I would just execute the statements as necessary but in this heavy workload case I’m trying to batch inserts together for some DB efficiency.

Also, if anyone could answer if it is possible( and then how to ) insert an sql DEFAULT value using placeholders and bind values that’d be awesome. Typically if I ever needed to do that I’d append the DEFAULTs to the string directly and use sprintf and $dbh->quote() only for the non DEFAULT values.

UPDATE:

Worked out the misunderstanding in a quick chat. User ikegami suggested that instead of building the query string myself without placeholders, that I just intermingle VALUES and placeholders such as:

$queryString .= '(DEFAULT,?,?),(DEFAULT,DEFAULT,DEFAULT)';

Some of the reasoning behind my first asking of this question on SO was because I was somewhat against this intermingling due to my thought that it made the code less readable, though after being assured that sql ‘DEFAULT’ couldn’t be in a placeholder bind value, this was the method I had begun implementing.

Using placeholders where possible does seem to be the more accepted method of building queries, and if you want an SQL DEFAULT you just need to include it in the same query building as the placeholders. This does not apply to NULL values, as those CAN be inserted with placeholders and a bind value of undef.

Update 2:

The reasoning I asked about performance, the ‘acceptance’ of building your own query with quote() vs building with placeholders, and why I’ve gone with a solution that involves using all columns for the SQL INSERT INTO tblname (cols) is because I have roughly 2-4 million rows a day going into a terrible db server, and my code is running on an equally terrible server. With my requirements of needing DEFAULT sql values, and these terrible performance constraints, I’ve chosen a solution for now.

For future devs who stumble upon this – take a look at @emazep’s solution of using SQL::Abstract, or if for some reason you need to build your own, you might consider either using @Schwern’s subroutine solution or possibly incorporating some of @ikegami’s answer into it as these are all great answers as to the ‘Current state of affairs’ regarding the usage of DBI and building dynamic queries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:57:23+00:00Added an answer on June 12, 2026 at 6:57 am

    If you would use placeholders for “static” queries, you should use them for “dynamic” queries too. A query is a query.

    my $stmt = 'UPDATE Widget SET foo=?'
    my @params = $foo;
    
    if ($set_far) {
       $stmt .= ', far=?';
       push @params, $far;
    }
    
    {
       my @where;
    
       if ($check_boo) {
          push @where, 'boo=?';
          push @params, $boo;
       }
    
       if ($check_bar) {
          push @where, 'bar=?';
          push @params, $bar;
       }
    
       $stmt .= ' WHERE ' . join ' AND ', map "($_)", @where
          if @where;
    }
    
    $dbh->do($stmt, undef, @params);
    

    I used an UPDATE since it allowed me to demonstrate more, but everything applies to INSERT too.

    my @fields = ('foo');
    my @params = ($foo);
    
    if ($set_far) {
       push @fields, 'bar';
       push @params, $far;
    }
    
    $stmt = 'INSERT INTO Widget ('
          . join(',', @fields)
          . ') VALUES ('
          . join(',', ('?')x@fields)
          . ')';
    
    $dbh->do($stmt, undef, @params);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.