in reply to New to Perl. How to loop!
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: New to Perl. How to loop!
by midlandmonkey (Initiate) on Nov 25, 2009 at 15:44 UTC |