Monday, February 13, 2017

How to find out whether a table exists?

In this article we will see how to check if a table exists in the schema.

Oracle

USER_TABLES has information about the tables that you own.

SELECT 1 FROM USER_TABLES WHERE LOWER(TABLE_NAME) = 'emp';

SQLSERVER

SYS.OBJECTS contains all the schema-scoped object that is created within a database

OBJECT_ID - Object identification number.

SELECT 1 FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'emp') AND TYPE IN (N'U');

HSQL

SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EMP'

MySQL

SELECT 1 FROM INFORMATION_SCHEMA.TABLES where LOWER(TABLE_SCHEMA) = '{SCHEMA_NAME}' AND LOWER(TABLE_NAME) = 'emp'

POSTGRES

To get the current schema:
SELECT current_schema

SELECT 1 FROM INFORMATION_SCHEMA.TABLES where LOWER(TABLE_SCHEMA) = '{SCHEMA_NAME}' AND LOWER(TABLE_NAME) = 'emp'

No comments:

Post a Comment