如何在 SQL Server 2005 中向视图添加列
我没有使用 SQL Server 2005 的经验。我被分配了一项修改视图以向视图添加 4 列的任务。是否可以在视图引用的表中不反映列更改的情况下执行此操作。如果表中有列,那么我应该删除视图并创建一个新视图,还是有办法更改它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用ALTER VIEW来实现您正在寻找的结果。
这将只是删除现有视图并从新的 select 语句中添加新列。但是,这比删除现有视图并创建新视图更好,因为更改视图将保留授予用户的权限。
You can use ALTER VIEW to achieve the result you are looking for.
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.
如果这 4 列是根据现有数据计算的,那么您只需运行 ALTER VIEW... 并将它们添加到视图使用的查询定义中
您可以右键单击 Management Studio 中的视图定义,然后“Script View as -> Alter”查看现有定义。
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 viewYou can right click the View definition in Management Studio and "Script View as -> Alter" to see the existing definition.
更改视图TheViewName
作为
选择oldCol_A、oldCol_B、NEWCol_C
从某个表
去
alter view TheViewName
as
select oldCol_A, oldCol_B, NEWCol_C
from someTable
go