in reply to Extracting data from an Ms ACCESS query

Hmm... it looks to me that you should read up on database design and SQL. You can solve it like this in Perl, but I wouldn't. Oh and I would use DBD::ODBC instead, so your code would be a bit more portable.

I'd make several tables for business_unit, group, subgroup and application, referering to each other where appropriate. and a cross table between subgroup and application.

Then do something like:

my $result = $dbh->selectall_arrayref( "SELECT application.name FROM application, group, subgroup, app_sub +group WHERE application.id = app_subgroup.application AND app_subgroup.subgroup = subgroup.id AND subgroup.group = group.id AND group.name='Procurement'" ); foreach (@$result) { print @$_; }
With the current model it would look like:
my $result = $dbh->selectall_arrayref( "SELECT DISTINCT(application) FROM table_name WHERE group_name='Procurement'" ); foreach (@$result) { print @$_; }
Which is simpler, but will get you in trouble later on when your table starts getting bigger and bigger and people start spelling Internet Explorer as "Internt Explorer".

Replies are listed 'Best First'.
Re^2: Extracting data from an Ms ACCESS query
by blackadder (Hermit) on Jun 11, 2004 at 11:58 UTC
    Thanks, I see your point but to start off with I will carry on with ODBC.

    Anyway, this is what I tried;
    use Win32; use Win32::ODBC; my $dsn = "Hydra_FE"; my $qry = "dbo_vfunction_application"; my $dbh = new Win32::ODBC($dsn) || die "\nError=>\t$^E : $!\n"; print "\nError: $^E\n" if (! $dbh); my $result = $dbh->selectall_arrayref("SELECT DISTINCT(application) FR +OM dbo_vfunction_application WHERE group_name='Procurement'"); foreach (@$result) { print @$_; }
    and this was what I got;
    Can't locate auto/Win32/ODBC/selectall_a.al in @INC (@INC contains: C: +/Perl/lib C:/Perl/site/lib .) at U:\scripts\odbc1.pl line 14
    Any thoughts? Thanks

    Blackadder
      Yes, I tried this;
      my $result = $dbh->sql("SELECT * FROM $qry WHERE group_name='Procureme +nt'"); foreach (@$result) { print @$_; }
      and I got this;
      Can't use string ("1") as an ARRAY ref while "strict refs" in use at U +:\scripts\odbc1.pl line 15.
      Hmmmm. Holy ones please HELP!

      Blackadder

        Hi, blackadder

        have you read the documents Win32::ODBC?

        As has already been said, it's $dbh->Sql($statement) not $dbh->sql() and it does not return an arrayref... it returns undef upon success!

        $sql="select * from foo"; if($db->Sql($sql)) { warn "i failed: " . $db->Error(); }else{ print "read the documentation on FetchRow(), Data() and DataHash() +\n"; }

        HTH - Mark

        Thanks maa

        Yes I have read the Win32::ODBC but there is no examples it it!!!

        but so far you have given me enough stuff to work on...Cheers

        Blackadder
        Darn, I meant to include this in my post below. To sort of repeat myself, the Sql call in Win32::ODBC will return either false, for a successful query or an error number if the query fails. You are storing this number in $result and trying to use it as an array reference in your print statement. From your print error message, your sql call is returning the value "1" which I think is an error from the Win32::ODBC module itself. The ODBC statement is actually $dbh->Sql() not $dbh->sql().

        PJ
        We are drowning in information and starving for knowledge - Rutherford D. Rogers
        What good is knowledge if you have to pull teeth to get it - anonymous