One possibility for why the parser is failing is I think your posted SQL is invalid: unless I'm reading it wrong, you have 7 parameters to insert but 8 terms because you have a repeated comma. I also note no nested queries in the query provided, so it's also possible that I can't diagnose the issue because you haven't posted the correct target.

If this is an internal utility, you could accomplish your goal with something like

m/insert\s+into\s+(\S+)\s*\(\s*([^)]+?)\s*\)\s* (?:values\s*\(\s*([^)]+?)\s*\) | select\s*(.+?)\s+(?:where|order) ) /ix
but this has a number of potential issues: nested queries will parse incorrectly, it fails to consider quoted names and string literals, ... Which is why I usually suggest canned code.

Update: Or misparse by me; the regular expression should read

m/insert\s+into\s+(\S+)\s*\(\s*([^)]+?)\s*\)\s* (?:values\s*\(\s*([^)]+?)\s*\) | select\s*(.+?)\s+(?:from\b) ) /ix
Of course, your posted SQL doesn't actually correspond to the SQL posted in the original_string field in Re^2: Parse complex SQL statement.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re^3: Parse complex SQL statement by kennethk
in thread Parse complex SQL statement by perl_help26

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.