user20907
user20907

Reputation: 83

How do I add columns to a view?

Is there a way to add new columns and update them in the view?

The columns are not in any table; I want to select few columns from a table then add few new columns and update them.

I tried ALTER VIEW to add columns but it gave me an error:

Cannot alter 'viewName' because it is not a table.

Upvotes: 2

Views: 10419

Answers (1)

See the SQL Server ALTER VIEW documentation.

You don't add columns to a new view, you just include the entire query to your view.

For exmaple, if I had:

CREATE VIEW MyView 
AS
SELECT UserID, UserName
FROM Users

And then wanted to add DateCreated to my select list, I would write:

ALTER VIEW MyView 
AS
SELECT UserID, UserName, DateCreated
FROM Users

Alternately, you can just DROP the view and CREATE it with the new columns as well.

Upvotes: 6

Related Questions