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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book