如何在 SQL Server 2005 中向视图添加列

发布于 2024-10-16 14:51:17 字数 120 浏览 2 评论 0原文

我没有使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱你一生 2024-10-23 14:51:17

您可以使用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.

野侃 2024-10-23 14:51:17

如果这 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

您可以右键单击 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 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.

药祭#氼 2024-10-23 14:51:17

更改视图TheViewName
作为
选择oldCol_A、oldCol_B、NEWCol_C
从某个表

alter view TheViewName
as
select oldCol_A, oldCol_B, NEWCol_C
from someTable

go

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文