0

I'm coming from an Oracle background and came across code to create a SQL Server view which I've never seen before.

We have a view called viewA (for example)

Within the script to generate the view I see this code

select columnA, columnB, columnC
from tableA as vw_tableB

The question I have is, what is the view really called?

Is it viewA or vw_tableB.

The other question I have is what is the rationale behind this?

  • Oracle supports table aliases too? Just not with as. The as is optional in SQL Server. If that is the only code in the view the table alias looks pointless though. – Martin Smith Jun 1 '16 at 22:52
7

The view is called whatever is listed in the CREATE statement, which would look like this in the script.

CREATE VIEW viewA AS

The vw_tableB is an table alias https://technet.microsoft.com/en-us/library/ms187455.aspx

The readability of a SELECT statement can be improved by giving a table an alias, also known as a correlation name or range variable. A table alias can be assigned either with or without the AS keyword:

  • Thanks, I've just never seen the entire select statement aliased when that alias is not being used anywhere else. The link you provided is how I've normally done it (ie aliased the tables being joined). – vksf Jun 3 '16 at 3:17
  • Yeah, that's an odd way to use an alias. I wonder if the original author had started that query with more ideas on joins, but never finished. – tpet Jun 3 '16 at 15:19
  • agreed! when i first saw it i thought it was some weird sql server thing (since i'm coming from an oracle background). as you said it was probably something that the author started (or maybe copied from some other query). – vksf Jun 5 '16 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.