3

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

BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
william
  • 6,666
  • 18
  • 63
  • 105

3 Answers3

4
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' 
Stefan Steiger
  • 73,615
  • 63
  • 359
  • 429
Skrealin
  • 1,054
  • 6
  • 16
  • 32
2

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.

Martin
  • 1,612
  • 3
  • 14
  • 27
0

You can also do using OBJECT_ID.

IF OBJECT_ID('table1') IS NOT NULL
print 'Exists' 
else
print 'Not Exists' 
Pankaj Agarwal
  • 10,965
  • 12
  • 40
  • 58