SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 What's the p for
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

JPAucamp
Starting Member

49 Posts

Posted - 11/16/2012 :  15:56:49  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 11/16/2012 :  16:27:02  Show Profile  Reply with Quote
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

Edited by - Lamprey on 11/16/2012 16:47:42
Go to Top of Page

JPAucamp
Starting Member

49 Posts

Posted - 11/16/2012 :  16:38:14  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 11/16/2012 :  16:50:57  Show Profile  Reply with Quote
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

USA
649 Posts

Posted - 11/16/2012 :  18:29:09  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000