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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With