Ron Tuffin
Ron Tuffin

Reputation: 54628

How can I display something during the execution of a SQL script on SQLServer?

For example. I have a database upgrade script for adding a column to a database table. It looks a little like this:

IF NOT Exists(SELECT * FROM SysColumns sc, SysObjects so 
              WHERE sc.Name = 'dealer_number'  
              AND so.Name = 'collector'
              AND so.Type= 'U'
              AND so.id = sc.id)
BEGIN
 -- SQL for creating column
END
ELSE
BEGIN
 -- notify user that column already exists
END

How do I notify the user that the column already exists?

Upvotes: 8

Views: 22312

Answers (4)

Joel Shemtov
Joel Shemtov

Reputation: 3078

Use PRINT - it works from most SQL client applications. SELECT also works e.g

PRINT 'column already exists or something'

or

SELECT 'column already exists or something'

Upvotes: 3

StuartLC
StuartLC

Reputation: 107277

RAISERROR seems appropriate here. See here.

Upvotes: 2

Martin Smith
Martin Smith

Reputation: 453328

RAISERROR ('column already exists',0,1)  with nowait

or

print 'column already exists'

Upvotes: 11

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55489

you can use PRINT statement in SQL

Upvotes: 3

Related Questions