Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 What's the p for

Author  Topic 

JPAucamp
Starting Member

49 Posts

Posted - 2012-11-16 : 15:56:49
Sorry just trying to learn some sql. Can someone please explain the p to me after the From clause. Thanks

USE AdventureWorks

SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3,

[231] AS Emp4, [233] AS Emp5

FROM (SELECT

PurchaseOrderID, EmployeeID, VendorID

FROM Purchasing.PurchaseOrderHeader) p

PIVOT ( COUNT (PurchaseOrderID)

FOR EmployeeID IN ( [164], [198], [223], [231], [233] ) ) AS pvt

ORDER BY VendorID

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-11-16 : 16:27:02
The "p" is a table (or this case a derived-table) alias. It is better to use the AS operator to help indentify the alias. Here is some more information:
http://msdn.microsoft.com/en-us/library/ms187455(v=sql.90).aspx
Go to Top of Page

JPAucamp
Starting Member

49 Posts

Posted - 2012-11-16 : 16:38:14
Thanks. So the select statement is creating a table which has an alias of p? Also am i right in saying that in this example in doesn't serve much purpose as the alias in not used again where normally it would save you time not having to write the select again.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-11-16 : 16:50:57
Well in this case you HAVE to alias a derived table or the SQL will not parse.
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2012-11-16 : 18:29:09
quote:
Originally posted by Lamprey

The "p" is a table (or this case a derived-table) alias. It is better to use the AS operator to help indentify the alias. Here is some more information:
http://msdn.microsoft.com/en-us/library/ms187455(v=sql.90).aspx




I guess it's a matter of personal choice because I don't actually believe that using "AS" is better.
Go to Top of Page
   

- Advertisement -