WHERE (Transact-SQL)WHERE (Transact-SQL)

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

指定查詢所傳回之資料列的搜尋條件。Specifies the search condition for the rows returned by the query.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

[ WHERE <search_condition> ]  

引數Arguments

< search_condition > 會定義所傳回資料列要符合的條件。< search_condition > Defines the condition to be met for the rows to be returned. 搜尋條件中所能包括的述詞數目沒有限制。There is no limit to the number of predicates that can be included in a search condition. 如需有關搜尋條件和述詞的詳細資訊,請參閱搜尋條件 (Transact-SQL)For more information about search conditions and predicates, see Search Condition (Transact-SQL).

範例Examples

下列範例示範如何在 WHERE 子句中使用某些常見的搜尋條件。The following examples show how to use some common search conditions in the WHERE clause.

A.A. 使用簡單的等式尋找資料列Finding a row by using a simple equality

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE LastName = 'Smith' ;  

B.B. 尋找所含值為字串之一部分的資料列Finding rows that contain a value as part of a string

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE LastName LIKE ('%Smi%');  

C.C. 使用比較運算子尋找資料列Finding rows by using a comparison operator

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey  <= 500;  

D.D. 尋找符合三個條件中任何一個條件的資料列Finding rows that meet any of three conditions

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey = 1 OR EmployeeKey = 8 OR EmployeeKey = 12;  

E.E. 尋找必須符合數個條件的資料列Finding rows that must meet several conditions

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey <= 500 AND LastName LIKE '%Smi%' AND FirstName LIKE '%A%';  

F.F. 尋找位於值清單中的資料列Finding rows that are in a list of values

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE LastName IN ('Smith', 'Godfrey', 'Johnson');  

G.G. 尋找兩個值之間有一個值的資料列Finding rows that have a value between two values

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey Between 100 AND 200;  

另請參閱See Also

DELETE (Transact-SQL) DELETE (Transact-SQL)
述詞 (Transact-SQL) Predicates (Transact-SQL)
搜尋條件 (Transact-SQL) Search Condition (Transact-SQL)
SELECT (Transact-SQL) SELECT (Transact-SQL)
UPDATE (Transact-SQL) UPDATE (Transact-SQL)
MERGE (Transact-SQL)MERGE (Transact-SQL)