2

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.

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.

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?

SELECT        PortalID, [To], [From], Subject, Body, CreatedOnDate, CreatedByUserID
FROM            dbo.CoreMessaging_Messages
3
  • create view v (c1, c2, ...) as select ... Commented Jul 21, 2017 at 11:32
  • 1
    The same you do in a any Select: [To] AS newname Commented Jul 21, 2017 at 11:33
  • Thanks @dnoeth. That worked perfectly! Commented Jul 21, 2017 at 11:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.