Skip to main content

DROP VIEW (SQL)

Deletes a view.

Synopsis

DROP VIEW [IF EXISTS] view-name [CASCADE | RESTRICT]

Description

The DROP VIEW command removes a view, but does not remove the underlying tables or data.

A drop view operation can also be invoked using the DropView()Opens in a new tab method call:

$SYSTEM.SQL.Schema.DropView(viewname,SQLCODE,%msg)

Privileges

The DROP VIEW command is a privileged operation. Prior to using DROP VIEW it is necessary for your process to have either %DROP_VIEW administrative privilege or a DELETE object privilege for the specified view. Failing to do so results in an SQLCODE -99 error (Privilege Violation). You can determine if the current user has DELETE privilege by invoking the %CHECKPRIV command. You can determine if a specified user has DELETE privilege by invoking the $SYSTEM.SQL.Security.CheckPrivilege()Opens in a new tab method. You can use the GRANT command to assign %DROP_VIEW privileges, if you hold appropriate granting privileges.

In embedded SQL, you can use the $SYSTEM.Security.Login()Opens in a new tab method to log in as a user with appropriate privileges:

   DO $SYSTEM.Security.Login("_SYSTEM","SYS")
   &sql(      )

You must have the %Service_Login:Use privilege to invoke the $SYSTEM.Security.Login method. For further information, see %SYSTEM.SecurityOpens in a new tab.

You can delete a view based on a table that is projected from a deployed persistent class.

Nonexistent View

To determine if a specified view exists in the current namespace, use the $SYSTEM.SQL.Schema.ViewExists()Opens in a new tab method.

By default, if you try to delete a nonexistent view, DROP VIEW issues an SQLCODE -30 error. To determine the current setting, call $SYSTEM.SQL.CurrentSettings()Opens in a new tab, which displays a Allow DDL DROP of non-existent table or view setting. The default is 0 (“No”). This is the recommended setting for this option. If set to 1 (“Yes”) issuing a DROP VIEW or DROP TABLE for nonexistent views and tables performs no operation and issues no error message.

From the Management Portal, System Administration, Configuration, SQL and Object Settings, SQL you can set this option (and other similar create, alter, and drop options) system-wide by selecting the Ignore redundant DDL statements check box.

The behavior of the predicate IF EXISTS takes priority over settings in the Management Portal and the configuration parameter file (CPF) which also govern DDL statements. These settings return SQLCODE 0 and suppress the error silently. When IF EXISTS is specified, the command returns SQLCODE 1 along with a message.

VIEW Referenced by Other Views

If you try to delete a view referenced by other views in their queries, DROP VIEW issues an SQLCODE -321 error by default. This is the RESTRICT keyword behavior.

By specifying the CASCADE keyword, an attempt to delete a view referenced by other views in their queries succeeds. The DROP VIEW also deletes these other views. If InterSystems IRIS cannot perform all cascade view deletions (for example, due to an SQLCODE -300 error) no views are deleted.

Associated Queries

Dropping a view automatically purges any related cached queries and purges query information generated by %SYS.PTools.StatsSQLOpens in a new tab. Dropping a view automatically purges any SQL runtime statistics (SQL Stats) information for any related query.

Arguments

IF EXISTS

An optional argument that suppresses the error if the command is executed on a nonexistent view. For further details, refer to the following section on nonexistent tables.

view-name

The name of the view to be deleted. A view name can be qualified (schema.viewname), or unqualified (viewname). An unqualified view name takes the default schema name.

CASCADE, RESTRICT

An optional argument. Specify the CASCADE keyword to drop any other view that references view-name. Specify RESTRICT to issue an SQLCODE -321 error if there is another view that references view-name. The default is RESTRICT.

Examples

The following example creates a view named "CityAddressBook" and later deletes the view. Because it is specified with the RESTRICT keyword (the default), an SQLCODE -321 error is issued if the view is referenced by other views:

CREATE VIEW CityAddressBook AS
     SELECT Name,Home_Street FROM Sample.Person 
     WHERE Home_City='Boston'
DROP VIEW CityAddressBook RESTRICT)

See Also

FeedbackOpens in a new tab