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

Ron Tuffin picture Ron Tuffin · Sep 13, 2010 · Viewed 14.7k times · Source

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?

Answer

Martin Smith picture Martin Smith · Sep 13, 2010
RAISERROR ('column already exists',0,1)  with nowait

or

print 'column already exists'