Solved

Drop a view if it exists

Posted on 2011-02-12
6
699 Views
Last Modified: 2012-08-14
Insanely easy points out there for you T-SQL jockeys...

I have a view which I am implementing, and the CREATE VIEW statement works exactly as expected.

Before running the CREATE VIEW statement, I want to check for existence of that same view, and if it exists, to drop the view.  So, the logic looks like this:


USE MyDB

GO

IF EXISTS (statement to find the view) DROP VIEW dbo.MyView

GO

CREATE VIEW dbo.MyView AS <SQL statement>

Open in new window



What I do not have is the "statement to find the view" bit :)

Thanks!
0
Comment
Question by:Patrick Matthews
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Accepted Solution

by:
sunezapa earned 500 total points
ID: 34879769
what about:

IF EXISTS  (Select * From dbo.sysobjects Where id =
Object_ID('dbo.MyView') and OBJECTPROPERTY(id, N'IsView') = 1) 

Open in new window

0
 
LVL 14

Expert Comment

by:leoahmad
ID: 34879772
if not exists (select * from sysobjects
  where name='view-name' and xtype='V')
    exec ('create view view-name as  ...')
0
 
LVL 16

Expert Comment

by:BurnieP
ID: 34879774

if exists (select * from sys.objects where [object_id] = object_id('dbo.MyView ) )
begin
 DROP VIEW dbo.MyView
end

CREATE VIEW dbo.MyView AS <SQL statement>

Open in new window

0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 31

Expert Comment

by:James Murrell
ID: 34879786
i use


USE MyDB
GO

IF EXISTS(SELECT * FROM sys.views
WHERE name = 'StudentGrades' AND schema_id = SCHEMA_ID('dbo'))
DROP VIEW dbo.MyView
GO

CREATE VIEW dbo.MyView AS <SQL statement>
GO

Open in new window

0
 
LVL 31

Expert Comment

by:James Murrell
ID: 34879802
in fact how about
USE [MyDB]
GO

IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[MyView]'))
DROP VIEW [dbo].[MyView]
GO

USE [MyDB]
GO


CREATE VIEW dbo.MyView AS <SQL statement>
GO

Open in new window

0
 
LVL 92

Author Closing Comment

by:Patrick Matthews
ID: 34880274
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question