Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Abort running script

Author  Topic 

carljohan_larsson
Starting Member

1 Post

Posted - 2010-09-27 : 02:33:43
Hi,

I have a script that creates a database and lots of tables. I would like to abort the entire script if the database exists.
The problem I have is that the script continues to run whatever I tried to do.
Here is the beginning of the script:

USE Database1
IF EXISTS (SELECT * FROM [dbo].[Table1])
BEGIN
PRINT 'Database1 exists'
RETURN
END
ELSE
BEGIN

CREATE DATABASE Database1

USE Database1
bla bla bla
I would like thre script to completely stop at line 5 (Return).

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-09-27 : 03:11:31
DECLARE @DBName AS VARCHAR(100)
SET @DBName = 'testDB'
IF NOT EXISTS( SELECT 1 FROM sys.databases WHERE Name = @DBName )
BEGIN
<Your Script>
....
....
END
ELSE
BEGIN
PRINT 'Database Exists'
END

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page
   

- Advertisement -