Click here to Skip to main content
16,004,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can one give me simple example of with clause in sql ?

And Where should we use this ?
Posted
Comments
[no name] 9-Aug-13 9:24am    
Was the documentation unclear?

SQL
WITH Sales_CTE (SalesPersonID, NumberOfOrders)
AS
(
    SELECT SalesPersonID, COUNT(*)
    FROM Sales.SalesOrderHeader
    WHERE SalesPersonID IS NOT NULL
    GROUP BY SalesPersonID
)
SELECT AVG(NumberOfOrders) AS "Average Sales Per Person"
FROM Sales_CTE;
GO


Look further at MSDN:

http://technet.microsoft.com/en-us/library/ms175972.aspx[^]
 
Share this answer
 
You can use it to allow dirty reads in a select for example;

SQL
select * from my_table with(nolock) where condition='something'


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Manas Bhardwaj 9-Aug-13 9:29am    
Yup +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900