|
Cry How to...Create a view unless it already existsTo create a view in SQL Server, unless it has already been created. Conceptually this is simply a case of checking whether the view exists and if it doesn't then creating it. Unfortunately, it is a little more complicated that this because SQL server insists that "create view" statement must be the first statement in a query batch. To get around this the creation of the view is executed within a separate Transact-SQL batch:
where view-name is the name of the table. The 'xtype' field holds the type of object, 'V' denoting a view. For example:
An alternative approach would be to drop the view and recreate it, but this may cause problems in some circumstances. These notes have been tested against SQL Server 2000. |