Solved

Drop a view if it exists

Posted on 2011-02-12
6
680 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now