Solved

Drop a view if it exists

Posted on 2011-02-12
6
696 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Industry Leaders: 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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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
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 setup several different housekeeping processes for a SQL Server.

735 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