Welcome to Perl. May you find the enjoyment I have in working with it.

Please read Markup in the Monastery to understand how one is supposed to format postings for legibility. In particular, wrapping your code in <code> tags will cause it to display more clearly and make it easier for monks to download it for testing.

If I am reading your question correctly, you want to execute two SQL statements that exist as string constants in your code. Your issue is that your second assignment to the variable $sql overwrites the first assignment. There are a number of ways to address this, but I think the most logical would be assigning to an array rather than a scalar and then looping over those values. Something like:

use strict; use warnings; sub create_temp_tables($$) { # Create copies of sp047 and sp057 for updating my ($dbh, $dbms) = (@_); my @sqls; if ($dbms eq 'ORACLE') { push @sqls, q{create table sp047_postcodes_temp as select * fr +om sp047_postcodes}; push @sqls, q{create table sp057_regions_temp as select * from + sp057_regions}; } else # SQLServer { push @sqls, q{select * into sp047_postcodes_temp from sp047_po +stcodes}; push @sqls, q{select * into sp057_regions_temp from sp057_regi +ons}; } foreach my $sql (@sqls) { my $sth = $dbh->prepare($sql); if ($sth) { my $rc = $sth->execute(); if ($sth->err) { myexit($sth->errstr, $dbh); } else { # Need to commit to release locks in SQL Server - + we have done nothing else at this stage, # so no problem with this. $dbh->commit; } $sth->finish; } else { myexit("Unable to prepare SQL : " . $sql . "\n", $dbh); } } }

You can learn more about the data types in Perl from perldata and basic Perl control structures from perlsyn. There are a number of elements of your code that don't generally follow "best practice", like your prototyping (subroutine prototype in Perl), so you might take a few minutes to peruse the archives here to learn a bit about normal structures. Hope this helps, and please ask follow-up questions if this is unclear.


In reply to Re: New to Perl. How to loop! by kennethk
in thread New to Perl. How to loop! by midlandmonkey

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.