How to tell if a database table exists?

S

Simon Wigzell

You'd think this would be the most basic sql query in the world but noooooo!
I've tried this:

on error resume next
strsql = "SELECT * FROM " & session("TablePrefix") & CurrentTable
SET rs = conn.execute(strsql)

tableExists = 0

if (Err.number = 0) then
tableExists = 1
end if

But it doesn't return an error if the table doesn't exist. I'm searching on
the internet and hitting these long complicatred solutions involving the
database "shema". Surely there is a simple way of telling with one line of
sql if a table exists or not??? Thanks!
 
S

Simon Wigzell

<snip>

I have found and implemented this solution from the internet:

<!-- #include file="adovbs.inc"-->
<%
Set Conn = Server.CreateObject ("ADODB.Connection")
Conn.Open "DSN=Library"
Set Rs = Conn.OpenSchema (adSchemaTables)

tableExists = 0

Do While Not Rs.EOF
if (rs("Table_Name") = CurrentTable) then
tableExists = 1
end if
Rs.MoveNext
Loop

Rs.Close
Set Rs = Nothing

Conn.Close
Set Conn = Nothing
%>
You must find on your system the include file adovbs.inc and copy it to the
directory where you ASP code is!

I'm just astonished and shaking my head in utter disbelief that there isn't
a simpler way of determining if a table exists! UGH!
 
Y

Yan Roosens

Hi Simon,

Simon said:
But it doesn't return an error if the table doesn't exist. I'm searching on
the internet and hitting these long complicatred solutions involving the
database "shema". Surely there is a simple way of telling with one line of
sql if a table exists or not??? Thanks!

With one line of SQL, I don't know, but with a few lines of vbscript....

set Cat = CreateObject("ADOX.Catalog")
Cat.activeConnection = conn
for each table in Cat.tables
if table.type="TABLE" then
if table.name = session("TablePrefix") & CurrentTable then
' do your thing....
end if
end if
next
set Cat = Nothing


HTH
Yan
 
B

Bob Barrows [MVP]

Simon said:
You'd think this would be the most basic sql query in the world but
noooooo! I've tried this:

on error resume next
strsql = "SELECT * FROM " & session("TablePrefix") & CurrentTable
SET rs = conn.execute(strsql)

tableExists = 0

if (Err.number = 0) then
tableExists = 1
end if

But it doesn't return an error if the table doesn't exist.

Yes it does. Your problem is that you've executed a statement that does not
raise an error between the statement that raises an error (the Execute
statement) and the statement that tests the Err object for the existence of
an error. When a statement executes with no error, the Err object is
cleared. Move the "tableExists = 0" line to before the Execute statement.
That will allow you to see the error.

Better yet, check the connection object's Errors collection, which will not
be affected by the execution of subsequent vbscript statements.

I'm
searching on the internet and hitting these long complicatred
solutions involving the database "shema". Surely there is a simple
way of telling with one line of sql if a table exists or not???
Thanks!

It's possible, but the implementation depends on the database you are using.
Jet databases have a MSysObjects table which can be queried for the database
schema. SQL Server has INFORMATION_SCHEMA views which can also be queried
for this information. I suspect Oracle databases have similar structures.
See:
http://www.aspfaq.com/show.asp?id=2112

HTH,
Bob Barrows
 
P

PW

Simon Wigzell said:
You'd think this would be the most basic sql query in the world but
noooooo!


If you're using Access, try this ...


myTableName = "BLAH"
myFileExists = FALSE
mySQL = "SELECT name FROM MSysObjects WHERE type in (1, 4)"
rs6.open mySQL,mydsn
Do While NOT rs6.EOF
if rs6("name") = myTableName then
myFileExists = TRUE
end if
rs6.MoveNext
Loop
 
B

Bob Barrows [MVP]

PW said:
If you're using Access, try this ...


myTableName = "BLAH"
myFileExists = FALSE
mySQL = "SELECT name FROM MSysObjects WHERE type in (1, 4)"
rs6.open mySQL,mydsn


This part is just silly:
*******************************
Do While NOT rs6.EOF
if rs6("name") = myTableName then
myFileExists = TRUE
end if
rs6.MoveNext
Loop
*******************************

Change your sql to (in practice, I would use a saved parameter query instead
of dynamic sql):
mySQL = "SELECT count(*) FROM MSysObjects " & _
"WHERE type in (1, 4) AND [name] = '" & myTableName & "'"

Now, open rs6 and simply check whether or not rs6(0) contains 0.

HTH,
Bob Barrows
 
P

PW

Bob Barrows said:
This part is just silly:


Done ...


myTableName = "BLAH"
mySQL = "SELECT count(*) FROM MSysObjects WHERE type in (1, 4) AND [name] =
'" & myTableName & "'"
rs6.open mySQL,mydsn
if rs6(0) = 0 then
myFileExists = FALSE
else
myFileExists = TRUE
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top