Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write a query to create a view if not exist already and if exist then need to alter view (not dropping and recreate) in MSSQL. Can someone help me?

What I have tried:

I searched in many websites. Most of times i found only dropping and recreate the view.
Posted
Updated 29-Aug-16 18:42pm

Try with the documentation first : ALTER VIEW (Transact-SQL)[^]
 
Share this answer
 
SQL
IF NOT EXISTS(select * FROM sys.views where name = 'data_VVVV ')
    BEGIN
        CREATE VIEW data_VVVV AS 
        SELECT VCV.xxxx, VCV.yyyy AS yyyy, VCV.zzzz AS zzzz FROM TABLE_A VCV
    END
ELSE
    BEGIN
        ALTER VIEW data_VVVV AS 
        SELECT VCV.xxxx, VCV.yyyy AS yyyy, VCV.zzzz AS zzzz FROM TABLE_A VCV
    END
 
Share this answer
 
Comments
e10Engi 21-Sep-16 5:22am    
@Daniel It is giving an error. Because of Create View or Alter view must be the only statements in the batch.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900