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

Hi,
I'm new to using Win32::ODBC (and I can't seem to get DBD or DBI to work regardless of what I do), but I'm having problems joining tables... (here is a sample of the tables)

Table: DlrLogins (columns apear within ())
(UserID) autodev (P1) APS (P2) TEMP
Table: APSVehicles
(Tag) 1989 (Dealer) APS

I get an environment variable for logged in user and place it in $AuthUser... I've tried a lot of things - the one actually worked in the SQL Analyzer but not from Perl...

Here is the code:

select APSVehicles.*, DlrLogins.* FROM APSVehicles INNER JOIN DlrLogins ON APSVehicles.Dealer = DlrLogins.P1 or APSVehicles.Dealer = DlrLogins.P2 WHERE DlrLogins.UserID = '$AuthUser' ORDER by DLRID";

Any help would be appreciated! I've spent a lot of time banging my head on the wall :(.

Thanks - Zack

Code tags added by davido.

Replies are listed 'Best First'.
Re: Question about joins?
by legato (Monk) on Jan 14, 2005 at 16:49 UTC

    With respect, posting your SQL code isn't nearly as much help without posting your Perl code as well. I strongly suspect that DBI will be a better match for you, and I know it works -- I use DBI with DBD::ODBC to connect to SQL Server databases all the time.

    Why don't you post some code that you tried with DBI, and we'll try to help you fix that up? Or, failing that, at least post the releveant Perl for this problem.

    Anima Legato
    .oO all things connect through the motion of the mind

      excellent :) thanks here is the code.
      $username="admin"; $pw="temppwd"; # connect to the named DSN, returns a database handle $dbh=new Win32::ODBC("DSN=APS;UID=$username;PWD=$pw;"); die "Unable to connect to DSN PerlSysAdm:" . Win32::ODBC::Error( ) . +"\n" unless (defined $dbh); $selst = "select APSVehicles.*, DlrLogins.* FROM APSVehicles INNER JOI +N DlrLogins ON APSVehicles.Dealer = DlrLogins.P1 or APSVehicles.Deale +r = DlrLogins.P2 WHERE DlrLogins.UserID = '$AuthUser' ORDER by DLRID" +; print "<BR>$selst<BR>\n"; $rc = $dbh->Sql($selst); die "Error: " . Win32::ODBC::Error( ) . "\n"; $results = $dbh->RowCount( ); $a = 1; $temp = 0; while ($dbh->FetchRow( )) { push(@AutoActive,$dbh->Data("AutoActive")); push(@Tag,$dbh->Data("Tag")); push(@Dealer,$dbh->Data("Dealer")); push(@YrMade,$dbh->Data("YrMade")); push(@Make,$dbh->Data("Make")); push(@Model,$dbh->Data("Model")); push(@ExtCol,$dbh->Data("ExtCol")); push(@Price,$dbh->Data("Price")); push(@DateAdded,$dbh->Data("DateAdded")); push(@FinanceAvail,$dbh->Data("FinanceAvail")); $a = ++$a; } $dbh->DropCursor( ); $dbh->Close( );
      Thanks - Zack
        I would reccomend an on the fly connect, in order to reduce dependence on the DSN being setup.
        my $dbh = DBI->connect('dbi:ODBC:driver=SQL Server;server=192.168.x.x; +database=My Database;app=my database application',$user,$pass);
Re: Question about joins?
by jZed (Prior) on Jan 14, 2005 at 16:44 UTC
    Do you have warnings turned on? If not, do so (put use warnings; at the top of your script). If so, does it tell you that you have an uninitialized value? That would mean that $AuthUser is not being captured.

    Please, don't give up on DBI, if you are just starting with perl and SQL, it will save you much grief in the long run. If you're having problems installing it, post the problems here, we can help.

      Thanks for the heads up :) I had printed out the $sqlst and it printed out with the $AuthUser variable included, but it's bizarre... I installed DBI and the DBD modules and whenever I try and run a script it always comes back with the "incomplete http headers" so then I try something different and nothing prints out, no error messages, etc.
      I'm on a 2003 server box and if someone could post the proper DBI connect statements with variables (even an example) to MS SQL and a basic select statement I could take it from there.
      I appreciate everyones help! :) Thanks - Zack
        Incomplete headers is a CGI problem, not a DBI problem. Try adding CGI::Carp as I show below so errors will print to the browser. Assuming you have created a *system* DSN for your database called MyDB, here's basic usage:
        #!perl -w use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; my $dbh = DBI->connect( 'dbi:ODBC:MyDB' , $user , $pass , {RaiseError=>1} ); my $sth = $dbh->prepare("SELECT * FROM foo"); $sth->execute; print CGI::header(); while (my $row = $sth->fetch){ my @r = map { defined $_ ? $_ : '' } @$row; print "@r<br>\n"; } $dbh->disconnect;
Re: Question about joins?
by Mutant (Priest) on Jan 14, 2005 at 16:39 UTC
    Try printing out the value of $AuthUser, or removing the where clause altogether (to make sure that's not what's causing the problem.)
      I tried removing the $AuthUser and it didn't work :( For some reason it isn't giving me an error. I used error trapping
      $rc = $dbh->Sql($selst);
      die "Error: " . Win32::ODBC::Error( ) . "\n";

      and it just prints out Error: (with no error message). Every other time it prints out an error.