Solved

Creating a SQL Script (batch) file with multiple create view statements

Posted on 2009-07-16
1
850 Views
Last Modified: 2012-05-07
I want to create a Microsoft SQL script file that creates multiple views in a database, and can be run multiple times without an error.

Is this possible. The code below will work, but it won't run a second time without an error. If I add an existence check I get an error that the create view statement isn't the first in the batch, is there a way around this problem?
create view v1
as 
select top 5 * from Name
go
 
create view v2
as
select top 6 * from Name
go

Open in new window

0
Comment
Question by:purplesoup
1 Comment
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 24869839
put this in front of each call:

if object_id('v1') is not null
drop view v1
go
create view v1
as
select top 5 * from Name
go
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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