midlandmonkey has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks! I'm very new to perl. In fact the only coding i have done is a basic VB using Mapbasic as I'm in GIS. I'm trying to edit a script which someone left behind and I am really stuck here. I realised after a few stupid minutes that my edit in which I've added 2 SQL updates cant be done and it can only prepare 1 statement which i believe is the last one, so how can I loop the code below to run both sql updates? Many many thanks!

Gareth

I have posted my sub routine below:

sub create_temp_tables($$) { # Create copies of sp047 and sp057 for updating my ($dbh, $dbms) = (@_); my $sql; if ($dbms eq 'ORACLE') { $sql = q{create table sp047_postcodes_temp as select * +from sp047_postcodes}; $sql = q{create table sp057_regions_temp as select * fr +om sp057_regions}; } else # SQLServer { $sql = q{select * into sp047_postcodes_temp from sp047_ +postcodes}; $sql = q{select * into sp057_regions_temp from sp057_re +gions}; } $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); } }

Replies are listed 'Best First'.
Re: New to Perl. How to loop!
by kennethk (Abbot) on Nov 25, 2009 at 15:29 UTC
    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.

      wow - That worked a treat! Thanks very much kennethk! I do believe this place will be my second home over the next few weeks :)
PLZ use <code> tags! :)
by LanX (Saint) on Nov 25, 2009 at 15:26 UTC
    Hi Gareth

    could you please put <code> ...</code> around your code?

    It's unreadable and therefor (unfortunately) quite unlikely that you'll get much help! 8)

    HTH

    Cheers Rolf

Re: New to Perl. How to loop!
by jethro (Monsignor) on Nov 25, 2009 at 15:27 UTC
    Please edit your post to use <code> tags around your code so that we can read it. And (if you haven't done so) you might indicate which parts you added and (if that isn't already obvious) how it fails