Use of uninitialized value in concatenation (.) or string

hI

i am running a perl script which is big enough to post here,but i will post the part which throws error.

I am trying to retrieve rows through an sql query.

#--------------script--------------------

#4.1.Finding job sets id
print("
Finding job sets id from table jobs…
");
$sql =<<“EOF”;
select job_id from moose.jobs
where parent_id = ‘$job_app’
EOF

 debug("SQL&gt;$sql", 2);
 $s = $db-&gt;prepare($sql);
 $s-&gt;execute();
 while (@row = $s-&gt;fetchrow_array){
     	print join(",",@row),"\

“;
$jobs = join(”,",@row);
print(“jobs = $jobs”);
}

Now when I run this script,it gives the following error :

Finding job sets id from table jobs…
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in array element at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in concatenation (.) or string at ./removeMooseBuild_dev_nidhi.pl line 340.
Use of uninitialized value in array dereference at ./removeMooseBuild_dev_nidhi.pl line 340.
SQL> select job_id from moose.jobs
where parent_id = 1385279
EOF

 debug("SQL&gt;        select job_id from moose.jobs
    where schedule_id = '1385277'

", 2);
DBI::st=HASH(0x51f588) = DBI::db=HASH(0x35cd58)->prepare( select job_id from moose.jobs
where schedule_id = ‘1385277’
);
DBI::st=HASH(0x51f588)->execute();
…and so on…

Ideally the query returns three rows with a single column integer value in each row such as :
80
84
92

Also,$job_app is not null…can you please help me out with the possible mistake i am making…?

You left out one crucial piece of information: Which line is line 340?

I’m going to guess that it’s the line print("jobs = $jobs");, because the warning would show up on two different lines if it was the join causing it.

If my guess is right, then the warning means that $jobs is undefined, which in turn means that @row contains a single undef value. And that would mean that you’ve retrieved a database row in which the job_id field is NULL. Have you run the SQL query manually and verified that every returned record has a value in job_id?

Incidentally, since you’re only selecting a single field from the database, there’s no need for the join. join combines multiple values from an array into a single string, but your array will never contain multiple values.

As a broader point, you should make a habit of never inserting variables directly into your SQL statements. It’s much, much safer to use SQL placeholders instead. Here’s a quick revision of your posted code which demonstrates doing so, as well as testing for whether NULLs have been returned:


print("\
Finding job sets id from table jobs..\
");
my $sql =<<"EOF";
select job_id from moose.jobs
where parent_id = ?
EOF

debug("SQL>$sql", 2);
my $s = $db->prepare($sql);
$s->execute($job_app);
while (my @row = $s->fetchrow_array){
  my $job = $row[0];
  unless (defined $job) {
    warn "Found NULL job_id with parent_id $job_app\
";
    next;
  }
  print("job = $job");
}

Everything dsheroh said plus is this right?


SQL> select job_id from moose.jobs
where parent_id = 1385279
EOF

debug("SQL> select job_id from moose.jobs
where schedule_id = '1385277'
", 2);


What’s the difference between schedule_id and parent_id or are these separate queries?