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!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.