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.
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
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
AS is simply telling SQL that you want to name or type the item before as the name or statment after for example.
SUM(A + B) AS MyTotal
CREATE View MyView Column1, Column2 AS SELECT * From TABLE
SELECT MyColumn(s) FROM table_name AS MyAlias
So basically, AS just casts the item before as the item after, being an alias.
See http://www.w3schools.com/SQl/sql_alias.asp for a better explanation than I could give.
and many more examples.
Regards RE
The AS keyword basically means that you want to 'alias' someting to something else.
The most comman being table anmes and field names...
SELECT
[p].fn AS FirstName
FROM
people AS [p]
Line 2 aliases the field [fn] to the new name [FirstName].
Line 4 aliases the table [people] to the new name [p].
In your view example, and for Stored Procedures, etc, the AS is saying treat this AS . So when you use the name, the database engine treats it AS the code yuou just wrote...
SQL was once referred to as "Structured English Query Language". It's all trying to make the programatic syntax as similar as possible to English syntax.
It's part of the statement in that case defining what to declare the View 'as'.
In other cases, where you can use the AS statement in a SELECT statement on either a field or table, it's option.
The AS keyword is primarily used to create aliases (view, table, column, etc.).
View
Your example works fine. The View name you specified is an alias for the query that follows.
Table
select * from Table1 as t
inner join Table2 as t2
on t.id = t2.tid
Column
--will return a column called sum with a single row of 3
select 1+2 as sum
AS is used all over the place:
CAST('1' AS INT)
SELECT CAST('1' AS INT) AS Col1
WITH ThisCTE AS (SELECT CAST('1' AS INT) AS Col1)
SELECT Col1 FROM ThisCTE AS a
CREATE VIEW ThisView AS
WITH ThisCTE AS (SELECT CAST('1' AS INT) AS Col1)
SELECT Col1 FROM ThisCTE AS a
etc ad nauseum.
Bite the bullet, open up BOL, and start reading!
When you use the CREATE TABLE or VIEW statement, the AS keyword pulls in the records from the statement following, to fill the Table or View with initially.
So your example View will start as an exact copy of the People table.
This allows you choose the fields that you want to load into your Table or View.
Related
I am a newbie in Oracle SQL, though I have experience in SQL Server.
In SQL Server, to select the rows from a table with a particular column in front:
select columnName,* from tableName
In Oracle:
select columnName,* from tableName
gives error ORA-00936: missing expression, as below:
Please guide.
I can't view images, but here's what I think you need:
select t.column_name, t.*
from table_name t
i.e. you should prefix that particular column name with a table alias ("t"), and then use the same alias with the asterisk ("t.*") to retrieve all table columns.
In Oracle, if you need to view a column but also all columns, you need to define an alias for the table.
Select columnName, A.*
from tableName A;
few things we need to keep it in mind
Alias name in sql - used to derive the individual column name via select query
When you are going to use *[select all] you don't have to worry about the alias name
But when you try to pull all the columns and some specific fields you want to filter then you should go for "Alias"
Alias its object key to refer the inter column
select stu.studentName,stu.* from student stu;
I ran a SELECT-Statement like the following in Oracle successfully:
SELECT a.*, b.* FROM table_a a, table_b b;
Here I give table table_a the alias 'a' and table_b the alias 'b'.
Then I use the two table aliases in the select list ('a.*, b.*')
with an asterisk for each table alias to select all columns from both
tables.
Sure, I could just use '*' as select list to get all columns
from both tables, but it's about what is valid for the select list
(like here 'a.*, b.*').
I can execute a statement like the one above, but when I look at the syntax diagram for the select list in Oracle's documentation, it seems as it would be not valid to use multiple table aliases in the select list.
The question is, whether it is possible to derive something like ('a.*, b.*') with the syntax diagram for the select list. In the syntax diagram it seems, as if I can just have one table alias in the select, because I can't 'travel back' in the diagram to add a comma and an other table alias.
Syntax diagram: http://docs.oracle.com/database/121/SQLRF/img/select_list.gif
General description for SELECT: http://docs.oracle.com/database/121/SQLRF/statements_10002.htm#SQLRF01702
Yes you can. If you have more values then you just need to traverse back to comma as shown below.
I have the following query:
SELECT * FROM MailingList
There are about 20+ columns in the MailingList table, one which is called Address. This column has some fields which contain commas, which I need to take out. So I updated my query to:
SELECT REPLACE(Address, ',', '') AS Address, * FROM MailingList
But now I have two Address columns. Is there a way to only display one Address column while still using the wildcard (*) for all the other columns?
There is not a way to do this, though listing the columns you want explicitly is a good idea anyway.
You can trick as following query:
Get the data into a temp table
Drop the cloumns that are not needed
Get results and drop temp table
SELECT *, REPLACE(Address, ',', '') AS Address2
INTO #TempTable
FROM MailingList
ALTER TABLE #TempTable
DROP COLUMN [Address]
SELECT * FROM #TempTable
DROP TABLE #TempTable
I agree with Shadow - avoid using the * wild card if you can...
I know listing out ALL of the columns in select statement for big tables is a pain so here is a quick short cut you may not be aware of: In SQL Server Management Studio, browse through the object explorer and find the table you want to select from (MailingList). Right-click it to view the context menu and choose "Script Table as" then "SELECT TO" then "New Query Editor Window". This will create a new select statement with each column spelled out. In the future, use this method to create select statements, queries, procedures, etc. rather then the * wildcard. Performance is better and it just looks nicer :-)
Then you can solve your alias issue with the replace function.
hi in my database i am store more than 50 field with primarykey (Auto increment) i am not sure about the fields name but i wants to select the entire data in that table , i am using
SELECT * FROM tablename
i want to select all the fields except that ID but this query populate the entire table so is there is possible to unselect the particular field in the select query. Can anyone have an idea please guide me. Thanks in Advance
The * indicates that you want to select ALL fields from a given table. If you want to select only a few fields, or all but one, then you will need to specify the ones you want manually:
select field1,field2,field3 from tablename
The SQL standard does not offer an "except" notation. It would be neat if we could
select t.* -t.ID
from some_table t
/
but it is not supported.
On the other hand, SELECT * is a dangerous construct. It is always better to explicitly list the columns we want in any given situation.
how to convert result of an select sql query into a new table in msaccess ?
You can use sub queries
SELECT a,b,c INTO NewTable
FROM (SELECT a,b,c
FROM TheTable
WHERE a Is Null)
Like so:
SELECT *
INTO NewTable
FROM OldTable
First, create a table with the required keys, constraints, domain checking, references, etc. Then use an INSERT INTO..SELECT construct to populate it.
Do not be tempted by SELECT..INTO..FROM constructs. The resulting table will have no keys, therefore will not actually be a table at all. Better to start with a proper table then add the data e.g. it will be easier to trap bad data.
For an example of how things can go wrong with an SELECT..INTO clause: it can result in a column that includes the NULL value and while after the event you can change the column to NOT NULL the engine will not replace the NULLs, therefore you will end up with a NOT NULL column containing NULLs!
Also consider creating a 'viewed' table e.g. using CREATE VIEW SQL DDL rather than a base table.
If you want to do it through the user interface, you can also:
A) Create and test the select query. Save it.
B) Create a make table query. When asked what tables to show, select the query tab and your saved query.
C) Tell it the name of the table you want to create.
D) Go make coffee (depending on taste and size of table)
Select *
Into newtable
From somequery