开发者

When to use with clause in sql

开发者_StackOverflow中文版

Can Anybody tell me when to use with clause.


The WITH keyword is used to create a temporary named result set. These are called Common Table Expressions.

A very basic, self-explanatory example:

WITH Administrators (Name, Surname)
AS
(
    SELECT Name, Surname FROM Users WHERE AccessRights = 'Admin'
)
SELECT * FROM Administrators 

For further reading and more examples, I suggest starting out with the following MSDN article:

  • Common Table Expressions by John Papa


In SQL Server you sometimes need the WITH clause to force a query to use an Index. This is often a necessity in spatial queries that can reduce query time from 1 minute to a few seconds.

select * from MyTable with(index(MySpatialIndex)) where...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜