SQL - Data projection - Part 4 - Column alias of view in SQL
Ocak 6, 2012 by sql tutorial
|
|
Article Information
This SQL Tutorial contains and tries to cover following subjects:
- Explanation of Column Alias of view in sql.
- Example to Column Aliases of view in sql
Articles tries to provide answer to following questions
- How to give custom name to output column of view in sql server
- Creating custom column output name in sql
Article covers followings indirectly:
- alter view in sql
Articles pre-requisites following information:
- General knowledge of SQL Server Management Studio
SQL VIEW
SQL views are a way to build projection over a SQL table, with limiting visibility to protect particular data. View in sql are mainly used to increase data integrity and to pack the optimized-ready queries to use more than once. If we recall the views from earlier articles, view in sql provides column outputs like a SELECT query. View itself contains the SELECT query, and returns the result.
Column Aliases
The column names of SELECT statement which is contained in view, is possible to override with "column alias". Imagine that we return some columns with view in sql, but we need to give custom name to result columns. Iyn that case, column aliases can be used to override original column names returned by the view in sql.
Idea behind of column alias is to rename output column names of view.
Syntax for column alias in view
To rename output column names of table returned by view in sql, following syntax is used:
ALTER VIEW -view name goes here- (column aliases goes here)
AS
-SELECT query goes here-
Column Alias Example
Following sql example demonstrates how to give alias to column of view in sql.
To recall basic view in sql, lets first create a view and check the result set.
As above sql result set indicates, the output of select query returns original column names. To rename those column names, lets use column alias.
In above sql example screen, similar to earlier creating view, a view is created. However, this time we provided column alias as a list following the view name between parenthesis. Result sql query indicates that the column names have been renamed according to the aliases we provided to the ALTER VIEW. Instead "customerID", column name is now "customerCode".
Result columns become the name we supplied as alias.
Data Layers
Area: | programming \ languages \ tsql \ \ \ |
Ref: | |
Loc: | articles |
Tags: | tsql |
|