Groups | Blog | Home
all groups > asp.net > july 2007 >

asp.net : how to check existance of a table in sql server?



Mr. Arnold
7/7/2007 5:41:29 PM

[quoted text, click to view]

You make a stored procedure and ask the question.

If exist(tablename)

The stored procedure returns a Return code of zero if it's there or non-zero
if it's not there, which you'll check in code the return code that you have
set and returned, taking take the appropriate action.

Use Google where you can ask *How to check if a SQL Table Exist* or
something along those lines. Also look up *How to get a output parm or
return code from a Stored Procedure using ADO.NET* or something along those
lines.
Peter Bromberg [C# MVP]
7/7/2007 6:06:02 PM
If exists(Tablename) does not work in this case. That has to be a legitimate
query, not a "sysobject". Mark's solution would be the preferred one to me.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com



[quoted text, click to view]
Mark Rae [MVP]
7/7/2007 10:36:36 PM
[quoted text, click to view]

Since you don't mention what back-end RDBMS you're using, I'll assume it's
SQL Server...

Whatever method you're using to connect to the RDBMS, use the ADO.NET
ExecuteScaler method on the following SQL:

SELECT COUNT(*) FROM sys.tables WHERE [name] = 'dan' AND [type] = 'U'

If ExecuteScalar returns 1, the table exists - if it returns 0, it
doesn't...

There must be at least half a dozen other ways of doing this...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Mr. Arnold
7/7/2007 10:49:19 PM

"Peter Bromberg [C# MVP]" <pbromberg@yahoo.yabbadabbadoo.com> wrote in
message news:367F8241-5810-469C-9B65-B3F75A11C7F9@microsoft.com...
[quoted text, click to view]

What? Do you think I am going to rattle this stuff off the top of my head?
The OP has got the point, and I am sure the OP will find it, the solution,
after being given a little push. That's all it was and nothing else. I am
not going to worry about something as trivial as this.
Mark Rae [MVP]
7/7/2007 10:55:29 PM
[quoted text, click to view]

D'oh - apologies - I didn't read the title closely enough...!


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Dan
7/7/2007 11:25:03 PM
Hello,

we have an intranet application using Windows Integrated Authentification.
When an user starts the application, he gets a form for inputting data. The
first time he does that, the application creates in a specific database a
table with the name of his account (read with
Request.ServerVariables("remote_user") and creates in that table the records
he enters. From the second time the user starts the application, still the
same form appears but the table may not be recreated.
How can i check in code-behind (VB) whether that table (e.g. table 'dan'
exists)?

Thanks
Dan

(O)enone
7/8/2007 12:00:00 AM
[quoted text, click to view]

A better one IMO is to use the INFORMATION_SCHEMA views.

\\\
select *
from INFORMATION_SCHEMA.Tables
where TABLE_NAME = 'dan'
///

This is an ANSI standard (http://en.wikipedia.org/wiki/Information_Schema).
No directly accessing system tables, no "magic" codes (why does 'type' need
to be set to 'U'?), won't break on future versions of SQL Server and also
works on other RDBMSs.

There are lots of other INFORMATION_SCHEMA views that give access to
columns, views, constraints, stored procedures, etc. To see them all, take a
look at the views that are defined against the master database on your
server.

HTH,

--

(O)enone

Dan
7/8/2007 12:25:16 AM
Thanks to you two

"Mr. Arnold" <MR. Arnold@Arnold.com> schreef in bericht
news:ON7dZ%23NwHHA.3400@TK2MSFTNGP03.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button