Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new column in existing View in SQL-Server 2014 using Alter

I have created a view that is based on another view and a table. I want to add new column of type varchar. I did like below, But getting syntax error? I am new to SQL, So,could not understand

ALTER VIEW [dbo].[MyView]
ADD New_Col varchar(10) null 
GO
like image 759
SPBeginer Avatar asked Sep 16 '16 15:09

SPBeginer


People also ask

How do I add a column to an existing SQL view?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.

Can we alter column in view?

The altered view replaces the existing view, so you cannot modify specific columns in a view. A view is a virtual table based on the result set of a SELECT query or a UNION of such queries.

Can ALTER TABLE add new column?

Using the ALTER TABLE statement to add columns to a table automatically adds those columns to the end of the table. If you want the columns in a specific order in the table, you must use SQL Server Management Studio.


1 Answers

you have to write the entire view again and just add or omit what you want to change

for example your view is now :

create view myView as
  select field1
  from table1

and now you want to add a field called New_Col than you write this :

alter view myView as
  select field1,
         New_Col
  from table1
like image 100
GuidoG Avatar answered Oct 20 '22 21:10

GuidoG