How do I determine if a particular object is a view and not a table?

SQL Server Helper – Frequently Asked Questions – SQL Server Views

To determine if a particular object is a view, you have to use the OBJECTPROPERTY metadata function.

OBJECTPROPERTY ( id, property )

The OBJECTPROPERTY metadata function, which returns information about objects in the current database, accepts two parameters, namely the id, which is the ID of the object in the current database, and the property, which is the information to be returned for the object specified by id. To determine if an object is a view, you will use the IsView property.

IF OBJECTPROPERTY( OBJECT_ID( ‘[dbo].[Customers]’ ), ‘IsView’ ) = 1
PRINT ‘Object is a view’

In this script, the OBJECTPROPERY metadata function will return a value of 1 if the object is a view. Otherwise, it will return a value of 0. If the object passed to the function is not a valid object in the current database, the function will return a value of NULL.

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

What is 4 + 8 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)