Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: if table exists (DBI)

by HelenCr (Monk)
on Apr 26, 2013 at 15:26 UTC ( #1030857=note: print w/replies, xml ) Need Help??

in reply to if table exists (DBI)

Although this thread is 11 years old, since this is a basic question, then for the benefit of PerlMonks users, here is the "right and efficient" answer (in my opinion):

for MS SQL Server:

(Assume the table name you are testing for existence is in variable $table_name):

my $query = "IF EXISTS(SELECT * FROM sys.tables WHERE type = 'U' AND +name = '$table_name') SELECT 1 ELSE SELECT 0"; my $sth = $dbh->prepare( $query ); $sth->execute; my $result = $sth->fetchrow_array;

$result should be 1 if $table_name exists in the database, and vice versa.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030857]
[Corion]: Discipulus: Yeah, I rediscovered it when I wanted to have a simple (cross-platform) wrapper for my OpenGL program so it has a nice window etc
[Corion]: ... and one thing leads to another, so ambrus provided the integration with AnyEvent so I can now conveniently make HTTP requests from that app too :)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2016-12-09 09:12 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.