william
william

Reputation: 7664

how can i check whether a table exists in the database (ACCESS or SQL) in C#

I found a lot of questions regarding with this question.

But is there any simple statements to accomplish this task?

for both SQL and ACCESS

Upvotes: 3

Views: 6477

Answers (3)

Skrealin
Skrealin

Reputation: 1114

IF (EXISTS (SELECT 1 FROM sys.tables WHERE name = 'table_name'))
BEGIN
    -- do stuff
END

sys.tables can also give you some information about the table object, e.g. the is_replicated column tells you if the table was created by replication or the has_replication_filter column tells you if the table has a replication filter set up

NB: this is for SQL Server

Edit: For Access:

SELECT COUNT(*) as Exists from MsysObjects 
WHERE type = 1
AND name = 'MY_TABLE_NAME' 

Upvotes: 5

Martin
Martin

Reputation: 1622

Note that there is no standardized way to do this in SQL, you will have to write plattform-specific code.

To my knowledge, all DBMS have this functionality in one way or another, but it differs greatly, eg in Oracle you can query the sys.all_tables view.

Upvotes: 2

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11311

You can also do using OBJECT_ID.

IF OBJECT_ID('table1') IS NOT NULL
print 'Exists' 
else
print 'Not Exists' 

Upvotes: 0

Related Questions