database 如何在 SQL Server 2005 中向视图添加列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4946212/
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
How to add columns to a view in SQL Server 2005
提问by gizgok
I have no experience with SQL Server 2005. I've been assigned a task to modify views for adding 4 columns to the view. Is it possible to do this without the column change reflected in the table the view is referring. If I have the columns in the Table, then should I just drop the view and create a new one or is there a way to alter it.
我没有使用 SQL Server 2005 的经验。我被分配了一个任务来修改视图以向视图添加 4 列。是否可以在没有视图所引用的表中反映的列更改的情况下执行此操作。如果我在表中有列,那么我应该删除视图并创建一个新视图还是有办法改变它。
回答by Sachin Shanbhag
You can use ALTER VIEWto achieve the result you are looking for.
您可以使用ALTER VIEW来实现您正在寻找的结果。
This will just act as dropping the existing view and adding new columns from your new select statement. However, this is better than dropping your existing view and creating a new view because the Alter view will retain the permissions granted to users.
这将只是删除现有视图并从新的 select 语句中添加新列。但是,这比删除现有视图并创建新视图要好,因为 Alter 视图将保留授予用户的权限。
回答by Martin Smith
If these 4 columns are calculated based on existing data then you just need to run ALTER VIEW...
and add them into the query definition used by the view
如果这 4 列是基于现有数据计算的,那么您只需要运行ALTER VIEW...
并将它们添加到视图使用的查询定义中
ALTER VIEW dbo.foo
AS
SELECT originalcolumnlist, A+B AS col1, C+D as col2, E+F as col3, G+H as col4
FROM yourtable
You can right click the View definition in Management Studio and "Script View as -> Alter" to see the existing definition.
您可以右键单击 Management Studio 中的视图定义和“脚本视图为 -> 更改”以查看现有定义。
回答by Lord Tydus
alter view TheViewName
as
select oldCol_A, oldCol_B, NEWCol_C
from someTable
将视图名称更改
为
从someTable中选择 oldCol_A、oldCol_B、
NEWCol_C
go
走