Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know where AS keyword should be used?

There is a AS keyword in TSQL but in what situations I should use it ?

For example : Create View StatisticalData AS Select * from People

We used AS keyword in this statement but when creating tables we don't use it , I mean I am kinda confused.

Could you please tell me, in what kinda position I should use AS. I mean it is used to assign value to a variable?

Thanks in advance.

like image 735
Tarik Avatar asked Oct 27 '09 20:10

Tarik


2 Answers

Main uses:

  • Aliases for tables and columns
  • Between CREATE and definition
  • CAST AS newtype

Examples

SELECT
   foo AS tom,
   foo + bar AS dick,
   CAST(bar AS varchar(50)) AS harry
FROM
   fizz AS f


CREATE VIEW/PROC/FUNCTION etc
AS
... proc or view of udf etc definition
GO
like image 82
gbn Avatar answered Sep 17 '22 21:09

gbn


The as keyword is used when you create or alter views, procedures, functions and triggers.

Example:

create procedure Test
as
select 1

The keyword is also used with a different meaning to create aliases for tables and fields in a query.

Example:

select u.usnm as UserName, u.pwd as UserPassword
from applicationusertable as u
like image 45
Guffa Avatar answered Sep 17 '22 21:09

Guffa