passing array as subquery to a SQL query

From: Kamran (mkazeem_at_isb.paknet.com.pk)
Date: 09/27/04


To: dbi-users@perl.org
Date: Tue, 28 Sep 2004 00:20:28 +0600

Hello all,
I am new to the list.

I am trying to achieve a goal in PERL. That is:
I have a table say students with fields (id int, name char(10) ) .
I have data like:-

ID NAME
== ==========
1 Kamran
2 Imran
3 Amir
4 Abid

I have an array in perl as :-

my @ids = (1,2,3) ;

I want to select those records from the table students , which do not
have ID in the array @ids. (This should give me 4 and 'Abid' ) .

I am trying to do it as the follwoing code:-

########################## code snip start #########################
my @ids = (1,2,3) ;

my $SQL = "select * from students where id in (@ids)";

# I am using the syntax of subquery in the above line .

my $sth = $slavedbh->prepare($SQL) || die "Cannot prepare SQL
statement\n";

my $howmany = $sth->execute || die "Cannot select from table\n";

######################### code snip end ############################

I am using Postgresql 7.3 on Redhat Linux 9. I have perl 5. Rather
everything is what comes bundled with Redhat 9. nothing extra.

I don't know if this problem has been addressed in past. If so please
point me in the right direction.

Thanks in advance for your time and help.

Kamran