How to check if the View exists in a SQL Server Database

To check if the View exists in a SQL Server Database, run the following code that checks the INFORMATION_SCHEMA.TABLES view:

IF EXISTS(
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'Schema_Name'
AND TABLE_NAME='View_Name'
AND TABLE_TYPE ='VIEW')
BEGIN
-- If the view exists, the code in this block will run.
PRINT 'The view exists!'
END

check_view_information_schema_tables

You will get the same result by running the following code that checks the INFORMATION_SCHEMA.VIEWS view:

IF EXISTS(
SELECT * FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'Schema_Name'
AND TABLE_NAME='View_Name')
BEGIN
-- If the view exists, the code in this block will run.
PRINT 'The view exists!'
END

check_view_information_schema_views

Read related articles:
How to check if the Table exists in a SQL Server Database
How to check if the Column exists in a SQL Server Table
How to List all Views in a SQL Server Database

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">