Michael Peppler
Sybase Consulting
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
Sybase on Linux FAQ
Sybperl FAQ
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: "Robert Bresner" <rbresner at olf dot com>
Subject: How do I get a value with a select constant ?
Date: Aug 30 1999 7:14PM

Hi there... 

You know how you get something in your head
and you just KNOW youre doing the right thing, 
but it doesn't work anyway; and since you KNOW
you're doing the right thing no other solution
comes popping along ? Well, I've got one of those.

I want to see if a table exists in a database, 
and if it does, I want to select data from that table. 
So, I connect to the database and check sysobjects 
for the table, and if it exists I select 1, if 
it doesn't exist, I select 0. Then I check the 
result of that bit of code. But, when I do a 
dbnextrow(), I'm getting an undefined. 

I have a 
set up, and I'm getting no errors during my

Here's my Perl:

   $dbh = SybConnect($Password, $Server, $User );
   $dbh->dbuse($DB) or return 0;
   my $Cmd = "if exists( select * from sysobjects where name = 
      'version_number' and type = 'U' ) select 1 else select 0";
   my $Result = SybCmd( $dbh, $Cmd );
   my $RetVal = $dbh->dbnextrow();   # Is this the right thing? 
   if( not defined $RetVal )  {
      print "NO RETVAL ($Result)\n";
      return 0;
   else  {
      print "RETVAL: $RetVal\n";
      return $RetVal;

   sub SybCmd
      my ($dbh, $Cmd) = @_;
      chomp $Cmd;
      print STDOUT "SybCmd: $Cmd\n" if $verbose;
      return $dbh->dbresults;

SybCmd() is returning 1 (one). But dbnextrow() is 
getting me nothing. So, I'm wondering what I'm doing
wrong. It looks so right to me, ya know?

(The reason I'm doing a select 0/1 in the if exists
is because Sybase is parsing the whole command string
before it runs it, and if the table doesn't exist, I 
get an error before the block of code runs. Make sense?)

Thanks in advance for another perspective ...

Robert Bresner
Open Link Financial    516-227-6600 x216    fax: 516-227-1799
Opinions expressed are explicitly my own
"No more talking!  Cerebus has a SWORD!"