Template for creating a view in sql server is provided below:
--------------------------------------------------------------------------
/******************************************************************
======== BEGIN MAINTENANCE WRAPPER ==========
******************************************************************/
-- Check if the view already exists
IF EXISTS ( SELECT 1
FROM sysobjects--------------------------------------------------------------------------
/******************************************************************
======== BEGIN MAINTENANCE WRAPPER ==========
******************************************************************/
-- Check if the view already exists
IF EXISTS ( SELECT 1
WHERE name = 'VW_VIEW_NAME'
AND uid = user_id('dbo')
AND type = 'V' )
-- P = Proc; V = View; U = Table; TR = Trigger
BEGIN
-- If it exists, drop to recreate the view
DROP VIEW dbo.VW_VIEW_NAME
PRINT 'VIEW VW_VIEW_NAME DROPPED SUCCESSFULLY'
END
ELSE
BEGIN
PRINT 'VIEW VW_VIEW_NAME DROPPED FAILED'
END
GO
/******************************************************************
======== END MAINTENANCE WRAPPER ==========
******************************************************************/
/********************************************************
** REVISION HISTORY : *
** ====================== *
** DATE VERSION NAME COMMENTS *
** ---------- ------- ----------------- -----------------*
** MM/DD/CCYY 1.0 Developer Name Initial Draft *
** *
*****************************************************************/
CREATE VIEW dbo.VW_VIEW_NAME(
)AS
/* view Body */
GO
/******************************************************************
======== BEGIN MAINTENANCE WRAPPER ==========
******************************************************************/
-- Check for errors in creating the view
IF EXISTS ( SELECT 1
FROM sysobjects
WHERE name = 'VW_VIEW_NAME'
AND uid = user_id('dbo')
AND type = 'V' )
-- P = Proc; V = View; U = Table; TR = Trigger
BEGIN
PRINT 'VIEW VW_VIEW_NAME CREATED SUCCESSFULLY'
END
ELSE
BEGIN
PRINT 'VIEW VW_VIEW_NAME CREATION FAILED'
END
GO
/******************************************************************
======== END MAINTENANCE WRAPPER ==========
******************************************************************/
-- SELECT * FROM dbo.VW_VIEW_NAME