check view exists sql server

How can I check if a View exists in a SQL Server Database?

Hello everyone, I am going to share the code sample for check a View is exists or not in SQL Server.

The different type of system objects are use to check SQL view is exists or not.

The Types are

1.       SYS.VIEWS
2.       SYS.OBJECTS
3.       SYS.SQL_MODULES
4.       SYS.SYSOBJECTS

Syntax for check SQL View exists using SYS.VIEWS

IF EXISTS(SELECT 1 FROM SYS.VIEWS WHERE Name = 'VP_VIEW_CEU')
    BEGIN
        PRINT 'View Is Exist!'
    END
ELSE PRINT 'View Not Exist!'


Syntax for check SQL View exists using SYS. OBJECTS

IF EXISTS (SELECT * FROM SYS.OBJECTS
            WHERE object_id = OBJECT_ID('dbo.VP_VIEW_CEU'))
    BEGIN
        PRINT 'View Is Exist!'
    END
ELSE PRINT 'View Is Not Exist!'

Syntax for check SQL View exists using SYS.SQL_MODULES

IF EXISTS (SELECT 1 FROM SYS.SQL_MODULES
            WHERE object_id =  OBJECT_ID('dbo.VP_VIEW_CEU'))
    BEGIN
       PRINT 'View Is Exist!'
    END
ELSE PRINT 'View Is Not Exist!'


Syntax for check SQL View exists using SYS.SYSOBJECTS

IF EXISTS(SELECT 1 FROM SYS.SYSOBJECTS 
     WHERE id = OBJECT_ID(N'dbo.VP_VIEW_CEU'))
    BEGIN
         PRINT 'View Is Exist!'
    END
ELSE PRINT 'View Is Not Exist!'



For more detail, click below links.
http://stackoverflow.com/questions/1306009/how-can-i-check-if-a-view-exists-in-a-database
http://stackoverflow.com/questions/18534919/how-to-make-create-or-replace-view-work-in-sql-server

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^