RE: How to store query results in an array?



Use a global temporary table to store the results and join the GTT in the select. No bind variables, no problem with quoting, very easy.

-----------------
Ron Reidy
Lead DBA
Array BioPharma, Inc.


-----Original Message-----
From: Job Miller [mailto:jobmiller@xxxxxxxxx]
Sent: Thursday, May 26, 2005 3:04 PM
To: CAMPBELL, BRIAN D (BRIAN); 'Ronald J Kimball'; 'Jared Still';
ricardd@xxxxxxxxxxxxxxx
Cc: DBI List
Subject: RE: How to store query results in an array?


while placeholders are better than literals, you will still end up with potentially hundreds of varieties of this sql depending on the varying number of placeholders used. If you always have the same number or approximately the same number of placeholders, than a series of placeholders with a bind of the array of values is a good choice. If it varies per query, you instead might consider this approach which ensures you only parse one unique query.

http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:210612357425

If your in clause has only a few values an the use of an index on the IN column is generally helpful for your query, ensure you read this if you are 9i or earlier to properly set the cardinality for the nested table so that the optimizer chooses the index (if that is helpful to you) dynamic sampling in 10g fixes this.

Read about how to use the cardinality hint here to solve this problem if you are 9i:

http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:3779680732446#15740265481549

Job


"CAMPBELL, BRIAN D (BRIAN)" <campbelb@xxxxxxxxxx> wrote:
I believe placeholders (?) could be a better alternative to quote().

Handling of the IN operator was addressed by a thread last October, and additional information like placeholders which allows for possible prepare statement optimization. You can jump in on my contribution if you like, and then work your way through the thread...

http://www.nntp.perl.org/group/perl.dbi.users/24638

Aren't archives wonderful?


-----Original Message-----
From: Ronald J Kimball [mailto:rjk-dbi@xxxxxxxxxxx]
Sent: Thursday, May 26, 2005 7:06 AM
To: 'Jared Still'; ricardd@xxxxxxxxxxxxxxx
Cc: DBI List
Subject: RE: How to store query results in an array?



Jared Still [mailto:jkstill@xxxxxxxxxx] wrote:

> Here's a fun and slightly obfuscated method to do that:
>
> my $usql=q{select username from dba_users};
> my $aryRef = $dbh->selectall_arrayref($usql);
> my @users = map { $aryRef->[$_][0] } 0..$#{$aryRef};
> my $newSql = q{select from users where username in ('}
> . join(q{','},@users) . q{')};
>
> print "$newSql\n";

Regardless of the method you use to construct the query, you should not
quote the values by hand. This approach will fail if a value contains a
single quote, and may make you vulnerable to SQL injection attacks.

Instead, either call $dbh->quote() or use placeholders. For example:

my @users = map $_->[0], @$aryRef;
my $newSql = 'SELECT FROM users WHERE username IN (' .
join(', ', map $dbh->quote($_), @users) . ')';

Ronald





---------------------------------
Do You Yahoo!?
Yahoo! Small Business - Try our new Resources site!

This electronic message transmission is a PRIVATE communication which contains
information which may be confidential or privileged. The information is intended
to be for the use of the individual or entity named above. If you are not the
intended recipient, please be aware that any disclosure, copying, distribution
or use of the contents of this information is prohibited. Please notify the
sender of the delivery error by replying to this message, or notify us by
telephone (877-633-2436, ext. 0), and then delete it from your system.

.



Relevant Pages

  • Re: Finding the nearest match without reusing results
    ... comparing an array with a value generates an array of Trues and ... Any diff with wrong State or with used Store is implicitly zeroed out. ... Each must be larger than the absolute value of the maximum Sales ... go.....it just needs to return the closest match. ...
    (microsoft.public.excel.programming)
  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: Challenge: reading ascii data
    ... to store all the data before producing any output. ... would be bad practice in terms of memory consumption to use a standard ... So I use hashes to create a two-level "sparse array", ... Well the original problem definition was: ...
    (comp.lang.fortran)
  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)
  • Re: Sparse arrays
    ... >access array element, set it, sit for a bit, and read it back ... ONLY operations are STORE and RETRIEVE with no structure to the ... If you can tolerate a bit of inefficiency ... They live in sorted lists. ...
    (comp.lang.fortran)