使用:EXISTS (Transact-SQL)

指定一个子查询,测试行是否存在。

主题链接图标 Transact-SQL 语法约定

语法

EXISTS subquery

参数

subquery

受限制的 SELECT 语句。不允许使用 COMPUTE 子句和 INTO 关键字。有关详细信息,请参阅 SELECT (Transact-SQL) 中有关子查询的信息。

结果类型

Boolean

结果值

如果子查询包含任何行,则返回 TRUE。

示例

A. 在子查询中使用 NULL 仍然返回结果集

以下示例返回在子查询中指定了 NULL 的结果集,并且通过使用 EXISTS 仍求值为 TRUE。

复制代码
USE AdventureWorks ;
GO
SELECT DepartmentID, Name 
FROM HumanResources.Department 
WHERE EXISTS (SELECT NULL)
ORDER BY Name ASC ;

B. 比较使用 EXISTS 和 IN 的查询

以下示例比较了两个语义等同的查询。第一个查询使用 EXISTS,第二个查询使用 IN

复制代码
USE AdventureWorks ;
GO
SELECT DISTINCT c.FirstName, c.LastName, e.DepartmentID
FROM Person.Contact c JOIN HumanResources.Employee e
ON e.ContactID = c.ContactID WHERE EXISTS
(SELECT *
FROM HumanResources.Department d
WHERE e.DepartmentID = d.DepartmentID
AND d.Name = 'Purchasing') ;
GO

下面的查询使用 IN

复制代码
USE AdventureWorks ;
GO
SELECT DISTINCT c.FirstName, c.LastName, e.DepartmentID
FROM Person.Contact c JOIN HumanResources.Employee e
ON e.ContactID = c.ContactID WHERE DepartmentID IN
(SELECT DepartmentID 
FROM HumanResources.Department d
WHERE d.Name = 'Purchasing') ;
GO

以下是其中任一查询的结果集。

复制代码
FirstName                 LastName                  DepartmentID
------------------------- ------------------------- ------------
Annette                   Hill                      5           
Arvind                    Rao                       5           
Ben                       Miller                    5           
Eric                      Kurjan                    5           
Erin                      Hagins                    5           
Frank                     Pellow                    5           
Fukiko                    Ogisu                     5           
Gordon                    Hee                       5           
Linda                     Meisner                   5           
Mikael                    Sandberg                  5           
Reinout                   Hillmann                  5           
Sheela                    Word                      5           (12 row(s) affected)

C. 比较使用 EXISTS 和 = ANY 的查询

以下示例显示两个查找其名称与供应商名称相同的商店的查询。第一个查询使用 EXISTS,第二个查询使用 = ANY

复制代码
USE AdventureWorks ;
GO
SELECT DISTINCT s.Name
FROM Sales.Store s 
WHERE EXISTS
(SELECT *
FROM Purchasing.Vendor v
WHERE s.Name = v.Name) ;
GO

下面的查询使用 = ANY

复制代码
USE AdventureWorks ;
GO
SELECT DISTINCT s.Name
FROM Sales.Store s 
WHERE s.Name = ANY
(SELECT v.Name
FROM Purchasing.Vendor v ) ;
GO

D. 比较使用 EXISTS 和 IN 的查询

以下示例显示查找以 P 开头的部门员工的查询。

复制代码
USE AdventureWorks;
GO
SELECT c.FirstName, c.LastName, e.Title
FROM Person.Contact c JOIN HumanResources.Employee e
ON e.ContactID = c.ContactID WHERE EXISTS
(SELECT *
FROM HumanResources.Department d
WHERE e.DepartmentID = d.DepartmentID
AND d.Name LIKE 'P%');
GO

下面的查询使用 IN

复制代码
USE AdventureWorks;
GO
SELECT c.FirstName, c.LastName, e.Title
FROM Person.Contact c JOIN HumanResources.Employee e
ON e.ContactID = c.ContactID WHERE DepartmentID IN
(SELECT DepartmentID
FROM HumanResources.Department
WHERE Name LIKE 'P%');
GO

E. 使用 NOT EXISTS

NOT EXISTS 的作用与 EXISTS 正相反。如果子查询没有返回行,则满足 NOT EXISTS 中的 WHERE 子句。以下示例查找不在部门中且姓名以 P 开头的员工。

复制代码
USE AdventureWorks;
GO
SELECT c.FirstName, c.LastName, e.Title
FROM Person.Contact c JOIN HumanResources.Employee e
ON e.ContactID = c.ContactID WHERE NOT EXISTS
(SELECT *
FROM HumanResources.Department d
WHERE e.DepartmentID = d.DepartmentID
AND d.Name LIKE 'P%')
ORDER BY LastName, FirstName
GO

下面是结果集: 

复制代码
FirstName                      LastName                       Title                         
------------------------------ ------------------------------ ------------
Syed                           Abbas                          Pacific Sales Manager         
Hazem                          Abolrous                       Quality Assurance Manager     
Humberto                       Acevedo                        Application Specialist        
Pilar                          Ackerman                       Shipping & Receiving Superviso
François                       Ajenstat                       Database Administrator        
Amy                            Alberts                        European Sales Manager        
Sean                           Alexander                      Quality Assurance Technician  
Pamela                         Ansman-Wolfe                   Sales Representative          
Zainal                         Arifin                         Document Control Manager      
David                          Barber                         Assistant to CFO              
Paula                          Barreto de Mattos              Human Resources Manager       
Shai                           Bassli                         Facilities Manager            
Wanida                         Benshoof                       Marketing Assistant           
Karen                          Berg                           Application Specialist        
Karen                          Berge                          Document Control Assistant    
Andreas                        Berglund                       Quality Assurance Technician  
Matthias                       Berndt                         Shipping & Receiving Clerk    
Jo                             Berry                          Janitor                       
Jimmy                          Bischoff                       Stocker                       
Michael                        Blythe                         Sales Representative          
David                          Bradley                        Marketing Manager             
Kevin                          Brown                          Marketing Assistant           
David                          Campbell                       Sales Representative          
Jason                          Carlson                        Information Services Manager  
Fernando                       Caro                           Sales Representative          
Sean                           Chai                           Document Control Assistant    
Sootha                         Charncherngkha                 Quality Assurance Technician  
Hao                            Chen                           HR Administrative Assistant   
Kevin                          Chrisulis                      Network Administrator         
Pat                            Coleman                        Janitor                       
Stephanie                      Conroy                         Network Manager               
Debra                          Core                           Application Specialist        
Ovidiu                         Crãcium                        Sr. Tool Designer             
Grant                          Culbertson                     HR Administrative Assistant   
Mary                           Dempsey                        Marketing Assistant
Thierry                        D'Hers                         Tool Designer                 
Terri                          Duffy                          VP Engineering                
Susan                          Eaton                          Stocker                       
Terry                          Eminhizer                      Marketing Specialist          
Gail                           Erickson                       Design Engineer               
Janice                         Galvin                         Tool Designer                 
Mary                           Gibson                         Marketing Specialist          
Jossef                         Goldberg                       Design Engineer               
Sariya                         Harnpadoungsataya              Marketing Specialist          
Mark                           Harrington                     Quality Assurance Technician  
Magnus                         Hedlund                        Facilities Assistant          
Shu                            Ito                            Sales Representative          
Stephen                        Jiang                          North American Sales Manager  
Willis                         Johnson                        Recruiter                     
Brannon                        Jones                          Finance Manager               
Tengiz                         Kharatishvili                  Control Specialist            
Christian                      Kleinerman                     Maintenance Supervisor        
Vamsi                          Kuppa                          Shipping & Receiving Clerk    
David                          Liu                            Accounts Manager              
Vidur                          Luthra                         Recruiter                     
Stuart                         Macrae                         Janitor                       
Diane                          Margheim                       Research & Development Enginee
Mindy                          Martin                         Benefits Specialist           
Gigi                           Matthew                        Research & Development Enginee
Tete                           Mensa-Annan                    Sales Representative          
Ramesh                         Meyyappan                      Application Specialist        
Dylan                          Miller                         Research & Development Manager
Linda                          Mitchell                       Sales Representative          
Barbara                        Moreland                       Accountant                    
Laura                          Norman                         Chief Financial Officer       
Chris                          Norred                         Control Specialist            
Jae                            Pak                            Sales Representative          
Wanda                          Parks                          Janitor                       
Deborah                        Poe                            Accounts Receivable Specialist
Kim                            Ralls                          Stocker                       
Tsvi                           Reiter                         Sales Representative          
Sharon                         Salavaria                      Design Engineer               
Ken                            Sanchez                        Chief Executive Officer       
Jos?                          Saraiva                        Sales Representative          
Mike                           Seamans                        Accountant                    
Ashvini                        Sharma                         Network Administrator         
Janet                          Sheperdigian                   Accounts Payable Specialist   
Candy                          Spoon                          Accounts Receivable Specialist
Michael                        Sullivan                       Sr. Design Engineer           
Dragan                         Tomic                          Accounts Payable Specialist   
Lynn                           Tsoflias                       Sales Representative          
Rachel                         Valdez                         Sales Representative          
Garrett                        Vargar                         Sales Representative          
Ranjit                         Varkey Chudukatil              Sales Representative          
Bryan                          Walton                         Accounts Receivable Specialist
Jian Shuo                      Wang                           Engineering Manager           
Brian                          Welcker                        VP Sales                      
Jill                           Williams                       Marketing Specialist          
Dan                            Wilson                         Database Administrator        
John                           Wood                           Marketing Specialist          
Peng                           Wu                             Quality Assurance Supervisor  
(91 row(s) affected)
posted @ 2006-11-30 10:20  疯一样的自由  阅读(1443)  评论(0编辑  收藏  举报