# 運算式 (Transact-SQL)Expressions (Transact-SQL)

## 語法Syntax

``````-- Syntax for SQL Server and Azure SQL Database

{ constant | scalar_function | [ table_name. ] column | variable
| ( expression ) | ( scalar_subquery )
| { unary_operator } expression
| expression { binary_operator } expression
| ranking_windowed_function | aggregate_windowed_function
}
``````
``````-- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse

-- Expression in a SELECT statement
<expression> ::=
{
constant
| scalar_function
| column
| variable
| ( expression  )
| { unary_operator } expression
| expression { binary_operator } expression
}
[ COLLATE Windows_collation_name ]

-- Scalar Expression in a DECLARE, SET, IF...ELSE, or WHILE statement
<scalar_expression> ::=
{
constant
| scalar_function
| variable
| ( expression  )
| (scalar_subquery )
| { unary_operator } expression
| expression { binary_operator } expression
}
[ COLLATE { Windows_collation_name ]

``````

## 引數Arguments

constantconstant 這是代表單一特定資料值的符號。Is a symbol that represents a single, specific data value. 如需詳細資訊，請參閱常數 (Transact-SQL)For more information, see Constants (Transact-SQL).
scalar_functionscalar_function 這是 Transact-SQLTransact-SQL 語法的單位，可提供特定服務並傳回單一值。Is a unit of Transact-SQLTransact-SQL syntax that provides a specific service and returns a single value. scalar_function 可以是內建的純量函數 (例如 SUM、GETDATE 或 CAST 函數) 或純量使用者定義函數。scalar_function can be built-in scalar functions, such as the SUM, GETDATE, or CAST functions, or scalar user-defined functions.
[ table_name .[ table_name. ]] 這是資料表的名稱或別名。Is the name or alias of a table.
columncolumn 這是資料行的名稱。Is the name of a column. 運算式中只能使用資料行的名稱。Only the name of the column is allowed in an expression.
variablevariable 這是變數或參數的名稱。Is the name of a variable, or parameter. 如需詳細資訊，請參閱 DECLARE @local_variable (Transact-SQL)For more information, see DECLARE @local_variable (Transact-SQL).
( expression )( expression ) 這是符合這個主題所定義的任何有效運算式。Is any valid expression as defined in this topic. 括號是分組運算子，可確保會先評估運算式在括號內的所有運算子之後，才組合各個產生的運算式。The parentheses are grouping operators that make sure that all the operators in the expression within the parentheses are evaluated before the resulting expression is combined with another.
( scalar_subquery )( scalar_subquery ) 這是傳回單一值的子查詢。Is a subquery that returns one value. 例如：For example:

`SELECT MAX(UnitPrice)`

`FROM Products`
{ unary_operator }{ unary_operator } 一元運算子只適用於會評估得出數值資料類型類別目錄之任何資料類型的運算式。Unary operators can be applied only to expressions that evaluate to any one of the data types of the numeric data type category. 這是只有單一數值運算元的運算子：Is an operator that has only one numeric operand:

+ 表示正數。+ indicates a positive number.

- 表示負數。- indicates a negative number.

~ 表示運算元的補充運算子。~ indicates the one's complement operator.
{ binary_operator }{ binary_operator } 這是定義組合兩個運算式來產生單一結果之方式的運算子。Is an operator that defines the way two expressions are combined to yield a single result. binary_operator 可以是算術運算子、指派運算子 (=)、位元運算子、比較運算子、邏輯運算子、字串串連運算子 (+) 或一元運算子。binary_operator can be an arithmetic operator, the assignment operator (=), a bitwise operator, a comparison operator, a logical operator, the string concatenation operator (+), or a unary operator. 如需運算子的詳細資訊，請參閱運算子 (Transact-SQL)For more information about operators, see Operators (Transact-SQL).
ranking_windowed_functionranking_windowed_function 這是任何 Transact-SQLTransact-SQL 次序函數。Is any Transact-SQLTransact-SQL ranking function. 如需詳細資訊，請參閱次序函數 (Transact-SQL)For more information, see Ranking Functions (Transact-SQL).
aggregate_windowed_functionaggregate_windowed_function 這是含有 OVER 子句的任何 Transact-SQLTransact-SQL 彙總函式。Is any Transact-SQLTransact-SQL aggregate function with the OVER clause. 如需詳細資訊，請參閱 OVER 子句 (Transact-SQL)For more information, see OVER Clause (Transact-SQL).

## RemarksRemarks

• 運算式有相同的資料類型。The expressions have the same data type.

• 優先順序較低的資料類型可以隱含地轉換成優先順序較高的資料類型。The data type with the lower precedence can be implicitly converted to the data type with the higher data type precedence.

``````USE AdventureWorks2012;
GO
SELECT ProductID, 1+2
FROM Production.Product;
GO
``````

• Azure SQL 資料倉儲會配置固定的記憶體數量上限給每個執行緒，這樣就不會有執行緒用盡所有記憶體。Azure SQL Data Warehouse allocates a fixed maximum amount of memory to each thread so no thread can use up all the memory. 此記憶體有一部份會用來儲存查詢的運算式。Some of this memory is used for storing queries’ expressions. 若查詢有太多運算式，而且其所需記憶體超過內部限制，引擎將不會執行它。If a query has too many expressions and its required memory exceeds the internal limit, the engine will not execute it. 為避免發生這個問題，使用者可以將查詢變更為多個查詢，以減少每個查詢中的運算式數。To avoid this problem, users can change the query into multiple queries with smaller number of expressions in each. 例如，您查詢的 WHERE 子句中有一長串運算式：For example, you have a query with a long list of expressions in the WHERE clause:
``````DELETE FROM dbo.MyTable
WHERE
(c1 = '0000001' AND c2 = 'A000001') or
(c1 = '0000002' AND c2 = 'A000002') or
(c1 = '0000003' AND c2 = 'A000003') or
...

``````

``````DELETE FROM dbo.MyTable WHERE (c1 = '0000001' AND c2 = 'A000001');
DELETE FROM dbo.MyTable WHERE (c1 = '0000002' AND c2 = 'A000002');
DELETE FROM dbo.MyTable WHERE (c1 = '0000003' AND c2 = 'A000003');
...
``````