Solved

Drop a view if it exists

Posted on 2011-02-12
6
688 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

776 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