sql table exists code example

Example 1: t-sql test if table exists

IF (EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'TheSchema' 
                 AND  TABLE_NAME = 'TheTable'))
BEGIN
    --Do Stuff
END

Example 2: sql server check table exists

IF EXISTS 
  (SELECT object_id FROM sys.tables
  WHERE name = 'Artists'
  AND SCHEMA_NAME(schema_id) = 'dbo')
  PRINT 'The table exists'
ELSE 
  PRINT 'The table does not exist';

Example 3: sql exists

Checks for the existence of any record within the subquery, returning true if
one or more records are returned.
Example: Lists any dealerships with a deal finance percentage less than 10.
SELECT dealership_name
FROM dealerships
WHERE EXISTS (SELECT deal_name FROM deals WHERE
dealership_id = deals.dealership_id AND finance_
percentage < 10);

Tags:

Sql Example