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

Hi there Monks!
I am trying to add this SQL statement to the code and I am getting an error:
"Can't call method "execute" on an undefined value at my_file.pl line 200."
It happens even if I try to explicitly put the value into the select. Has anyone done anything like this before?
Here is the part of the code.
... my $search = "joe"; # for testing my $sql = qq{ ;with data_count as ( select sum(case when FIRST = 'joe' then 1 else 0 end) as a_count, sum(case when MIDDLE = 'joe' then 1 else 0 end) as b_count, sum(case when LAST = 'joe' then 1 else 0 end) as c_count from my_table where FIRST like 'joe%' or MIDDLE like 'joe%' or LAST like 'joe%' ) select 'Search by: ' + 'joe' union all select 'Found ' + convert(varchar, a_count) + ' ' + 'joe' + ' for Firs +t' from data_count union all select 'Found ' + convert(varchar, b_count) + ' ' + 'joe' + ' for Midd +le' from data_count union all select 'Found ' + convert(varchar, c_count) + ' ' + 'joe' + ' for Last +' from data_count }; my $sth = $dbh->prepare($sql); $sth->execute() or die "SQL Error: $DBI::errstr\n"; my $data = $sth->fetchall_arrayref({}); warn Dumper $data; ...
I would like to use place holders as well, in this case any suggestion?
Thanks for looking!

Replies are listed 'Best First'.
Re: Undefined value from DBI
by McA (Priest) on Sep 10, 2014 at 16:09 UTC

    Hi,

    that's a hint that your line

    my $sth = $dbh->prepare($sql);

    results in an error assigning undef to $sth.

    Without handle attribute RaiseError you have to check EVERY DBI call for errors. So in this case it's probably a simple SQL error.

    Change the prepare line to

    my $sth = $dbh->prepare($sql) or die "Prepare Error: $DBI::errstr";

    Regards
    McA

      I know, the issue is how I use this kind on SQl statement with out causing any errors, if I use a simple
      "select * from my_table"
      it works, but because of the nature of the SQL statement DBI does not like it, and it gets worse by trying to add place holders to it.
        Which database are you using ? Your code works fine for me on MS SQL Server Express.
        poj

        Sorry, I misuderstood your question.

        In such cases I try these SQL statements in a database dependant client to find out what is wrong. Any hints from that?

        McA

Re: Undefined value from DBI
by erix (Prior) on Sep 10, 2014 at 20:04 UTC

    No problem at all for DBI. I ran your SQL without problem in the postgres dialect:

    #!/bin/env perl use strict; use warnings; use Data::Dumper; use DBI; # table my_table; # first | middle | last # -------+--------+------ # joe | john | sean # joe | john | sean # joe | john | sean # pet | joe | sean # pet | zoe | joe # ken | zoe | joe # (6 rows) # (picking up dsn from environment:) my $dbh = DBI->connect or die "oops - no db connection\n"; $dbh->{RaiseError} = 1; my $sql = " with data_count as ( select sum(case when FIRST = 'joe' then 1 else 0 end) as a_count , sum(case when MIDDLE = 'joe' then 1 else 0 end) as b_count , sum(case when LAST = 'joe' then 1 else 0 end) as c_count from my_table where FIRST like 'joe%' or MIDDLE like 'joe%' or LAST like 'joe%' ) select 'Search by: ' || 'joe' union all select 'Found ' || cast(a_count as integer) || ' ' || 'joe' +|| ' for First' from data_count union all select 'Found ' || cast(b_count as integer) || ' ' || 'joe' +|| ' for Middle' from data_count union all select 'Found ' || cast(c_count as integer) || ' ' || 'joe' +|| ' for Last' from data_count " ; # ?column? # ------------------------ # Search by: joe # Found 3 joe for First # Found 1 joe for Middle # Found 2 joe for Last # (4 rows) my $sth = $dbh->prepare($sql); $sth->execute() or die "SQL Error: $DBI::errstr\n"; my $data = $sth->fetchall_arrayref({}); warn Dumper $data;

    which results in the expected output (proving that neither DBI nor the general SQL is at fault):

    $VAR1 = [ { '?column?' => 'Search by: joe' }, { '?column?' => 'Found 3 joe for First' }, { '?column?' => 'Found 1 joe for Middle' }, { '?column?' => 'Found 2 joe for Last' } ];

    (A further variant with placeholders also gave no problem (in postgres). )

    What database are you using? What is the error you get? Is there anything in the database logfile?

Re: Undefined value from DBI
by dsheroh (Monsignor) on Sep 11, 2014 at 08:40 UTC
    Your error message indicates that $sth is undefined.

    If $sth is undefined, it is because $dbh->prepare($sql) failed and returned undef.

    If $dbh->prepare($sql) failed, it is because the database returned an error.

    If the database returned an error, it is probably because the query in $sql is not valid in its dialect of SQL, but it could be something else. Checking for a DBI error on the prepare is the only way to find out:

    my $sth = $dbh->prepare($sql) or die "SQL Error: $DBI::errstr\n"; $sth->execute() or die "SQL Error: $DBI::errstr\n";
    Turning on your $dbh's RaiseError or PrintError flag would also work, since those implicitly check for and report database errors after every DBI call. But, regardless, the only way to find out why your prepare fails is to check for database errors.