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
  • 700,868
  • 160
  • 1,392
  • 1,356
william
  • 7,284
  • 19
  • 66
  • 106

3 Answers3

5
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
  • 78,642
  • 66
  • 377
  • 442
Skrealin
  • 1,114
  • 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,622
  • 4
  • 16
  • 27
0

You can also do using OBJECT_ID.

IF OBJECT_ID('table1') IS NOT NULL
print 'Exists' 
else
print 'Not Exists' 
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59