Hi Monks!
In my application I want to have one sub to use just to retrieve all the information I need from my db table. The issue is
that I am using fetchall_arrayref. In the first call to this sub I am retrieving all the records so I am passing all the column names I need
to the sub, it works fine, but on my second call I don’t need all the columns. My question is, can I pass only the rows I need or if there is a way so fetchall_arrayref
will take whatever will be passed by the sql query.
Here is the sub with all the rows been queried from the db table.
# Retrieve all the data to process from the database
sub _all_data {
my $self = shift;
my $sql = shift;
my $sth = $mssql_dbh->prepare($sql);
$sth->execute() || die $sth->errstr;
# array indices for each returned row
my $rs = $sth->fetchall_arrayref(
{
id => 1,
account_number => 1,
box => 1,
number => 1,
address => 1,
type => 1,
quantity => 1,
count => 1,
new_line => 1,
date => 1,
}
);
return $rs;
}
This first call as example, if I am retrieving all the records by calling all the rows from the table, this works fine.
...
my $sql = "select id, account_number, box,number,address, type, quant
+ity, count, new_line, date
from my_table where id <>''
order by date desc";
my $results = $self->_all_data($sql);
...
This call, I would only need to retrieve 3 rows, but I would like to still use the same sub _all_data(), thats the issue, cause the
_all_data() is expecting all the columns and I only need 3 this time.
...
my $sql = "select id, account_number, date
from my_table where id <>''
order by date desc";
my $results = $self->_all_data($sql);
...
Thanks for helping!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.