Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

DBD: bind_column behviour with multiple "unnamed" columns

by olivierp (Hermit)
on Aug 06, 2004 at 12:00 UTC ( [id://380503]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow monks
I have the following snippet running on Win32, against a MSSQL2000 server:
my $sql =<<'EOFSQL'; SELECT LOWER(field_one), field_two, UPPER(field_three) FROM sample_table (NOLOCK) WHERE field_one = ? EOFSQL my $sth = $dbh->prepare($sql) or die; my ($field_one, $field_two, $field_three); for my $test_value (@test_values) { $sth->bind_param(1, $test_value); $sth->execute; $sth->bind_columns(\$field_one, \$field_two, \$field_three); while ($sth->fetch) { print join "-", ($field_one, $field_two, $field_three); } }
When this runs, $field_one gets clobbered by the contents of $field_three, as if $sth->fetchwere returning a hash,
but it's documented as being an alias of $sth->fetchrow_arrayref
Doing a print join "<->", @{$sth->{NAME}}; gives <->field_two<->, "confirming" my idea.
I have changed my query to explicitly name the columns, but am curious to understand what's going on.

Update:
gmax has spotted where the problem is.
The behaviour I see is using DBD::ADO with a sqloledb Provider
When using DBD::ODBC, "unnamed" colums retain their values and do not step over each other.
Now I wonder where I should file a bug report... :)
Update 2:
As per DBD:ADO maintainer, this is a bug in versions <2.87. I was using 2.84, and after an update to version 2.91, my "unnamed" columns work correctly.

--
Olivier

Replies are listed 'Best First'.
Re: DBD: bind_column behviour with multiple "unnamed" columns
by gmax (Abbot) on Aug 06, 2004 at 13:36 UTC

    I was not able to replicate the behavior you describe. I don't have MS-SQL server, but I tried your code on a MySQL ad a SQLite table, and it works as advertised, i.e. the data comes as I expect and the field names in $sth->{NAME} contain exactly what I put in my select statement, with or without alias.

    I can confirm you that fetch is actually fetching an arrayref, as you can see by yourself if you set a line with DBI->trace(4); before your loop. In my test, I get something like this:

    fetch for DBD::SQLite::st fetch= [ 'xx' 'yy' 'zz' ] row1 at test_bind.pl line 24

    The DBI docs give:

     "fetchrow_arrayref"
           $ary_ref = $sth->fetchrow_arrayref;
           $ary_ref = $sth->fetch;    # alias
    

    My guess is that your DBD driver is not playing according to DBI rules.

     _  _ _  _  
    (_|| | |(_|><
     _|   
    
Re: DBD: bind_column behviour with multiple "unnamed" columns
by Grygonos (Chaplain) on Aug 06, 2004 at 12:46 UTC

    edit: <unprofessional answer>after re-reading I see you fixed it... but anyhow.. here's my best guess as to what's happening. I'm betting fetch is seeing two fields named the same thing and only returning the last instance. For instance, fetch might say.. how many field names do we have? 2.. ok...and since field3 was named "un-named" last, we'll return its value for all fields named "un-named". SQL Server has no problem having two fields aliased to the same name, so I'm guessing its something in the behavior of $sth->fetchrow_arrayref() that's to blame. </unprofessional answer> gmax said exactly what I was trying to say, but aparently I couldn't make my brain function this morning. DBD driver not interacting properly. Check your version of DBD:ODBC maybe?

    Try this instead

    my $sql =<<'EOFSQL'; SELECT LOWER(field_one) AS field_one, field_two, UPPER(field_three) AS field_three FROM sample_table (NOLOCK) WHERE field_one = ? EOFSQL
    Just alias the column names to be unique. That should correct the problem. Not a Perl problem though, actually a SQL problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://380503]
Approved by JanneVee
Front-paged by JanneVee
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-24 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found