如何使用与源表不同的列名创建视图

时间:2022-01-05 14:56:24

I have a table that stores email data. The challenge that I have is that some of the column names use MSSQL Reserved Words such as To and FROM.

我有一个存储电子邮件数据的表。我遇到的挑战是一些列名使用MSSQL保留字,如To和FROM。

I've place brackets around the column names and my Query works fine in Query Designer, but the challenge is I'll be calling it from ASP.Net. Bracket syntax in ASP.Net indicate it should inject data in place of the brackets.

我在列名称周围放置括号,我的查询在查询设计器中工作正常,但挑战是我将从ASP.Net调用它。 ASP.Net中的Bracket语法表明它应该代替括号注入数据。

To avoid all of this I want to create a View. How do I change the column names in the View to avoid Reserved Words?

为了避免所有这些,我想创建一个View。如何更改视图中的列名以避免保留字?

SELECT        PortalID, [To], [From], Subject, Body, CreatedOnDate, CreatedByUserID
FROM            dbo.CoreMessaging_Messages

1 个解决方案

#1


0  

In a view, you can do:

在视图中,您可以执行以下操作:

create view v_table as
    select PortalID, [To] as to_whatever, [From] as from_whatever,
           Subject, Body, CreatedOnDate, CreatedByUserID
    from dbo.CoreMessaging_Messages;

A simpler method might be just to use computed columns:

一个更简单的方法可能只是使用计算列:

alter table dbo.CoreMessaging_Messages
        add to_whatever as ([To]);

alter table dbo.CoreMessaging_Messages
        add from_whatever as ([From]);

Then the alternate names are available to whoever uses to the table.

然后,对于使用该表的任何人都可以使用替代名称。

#1


0  

In a view, you can do:

在视图中,您可以执行以下操作:

create view v_table as
    select PortalID, [To] as to_whatever, [From] as from_whatever,
           Subject, Body, CreatedOnDate, CreatedByUserID
    from dbo.CoreMessaging_Messages;

A simpler method might be just to use computed columns:

一个更简单的方法可能只是使用计算列:

alter table dbo.CoreMessaging_Messages
        add to_whatever as ([To]);

alter table dbo.CoreMessaging_Messages
        add from_whatever as ([From]);

Then the alternate names are available to whoever uses to the table.

然后,对于使用该表的任何人都可以使用替代名称。