如何使用 Alter 在 SQL-Server 2014 的现有视图中添加新列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39535278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:52:40  来源:igfitidea点击:

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

sqlsql-serverviewalter

提问by SPBeginer

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

我创建了一个基于另一个视图和表的视图。我想添加 varchar 类型的新列。我喜欢下面,但语法错误?我是 SQL 新手,所以,无法理解

ALTER VIEW [dbo].[MyView]
ADD New_Col varchar(10) null 
GO

回答by GuidoG

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_Colthan you write this :

现在你想添加一个名为的字段,而New_Col不是你写这个:

alter view myView as
  select field1,
         New_Col
  from table1

回答by BeardOfTriumph

You can't alter a view like a table. You have to script the view as Alter, and then alter the select statement that generates the view.

你不能像表格一样改变视图。您必须将视图编写为 Alter,然后更改生成视图的 select 语句。

回答by Mahmoud Mohamed Ramadan

You can try this query

你可以试试这个查询

ALTER VIEW view_name ADD column_name datatype