Languages
[Edit]
EN

MS SQL Server - Drop view if exists

0 points
Created by:
Dirask JS Member
3460

In this article, we would like to show you how to DROP VIEW IF EXISTS in MS SQL Server.

Quick solution:

DROP VIEW IF EXISTS [view_name];

Practical example

To show how the DROP VIEW IF EXISTS statement works, we will use the following table:

MS SQL Server - example data used with DROP VIEW IF EXISTS statement
MS SQL Server - example data used with DROP VIEW IF EXISTS statement

Note:

At the end of this article you can find database preparation SQL queries.

Example 

In this example, we will delete the Spain Users view which we created in this article.

Query:

DROP VIEW IF EXISTS [Spain Users];

Output:

MS SQL Server - DROP VIEW IF EXISTS result in HeidiSQL (before)
MS SQL Server - DROP VIEW IF EXISTS result in HeidiSQL (before)
MS SQL Server - DROP VIEW IF EXISTS result in HeidiSQL (after)
MS SQL Server - DROP VIEW IF EXISTS result in HeidiSQL (after)

Database preparation

create_tables.sql file:

CREATE TABLE [users] (
	[id] INT IDENTITY(1,1),
	[name] VARCHAR(100) NOT NULL,
	[email] VARCHAR(100) NOT NULL,
	[country] VARCHAR(15) NOT NULL,
	PRIMARY KEY ([id])
);

insert_data.sql file:

INSERT INTO [users]
	([name], [email], [country])
VALUES
	('Tom', 'tom01@email.com', 'Poland'),
	('Chris','chris123@email.com', 'Spain'),
	('Jack','jack32@email.com', 'Spain'),
    ('Kim','kim103@email.com', 'Vietnam'),
    ('Marco','marco942@email.com', 'Italy'),
	('Kate','kate227@email.com', 'Spain'),
	('Nam','nam393@email.com', 'Vietnam');

MS SQL Server - problems

MS SQL Server - Drop view if exists
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join