Scalar operators are used for operations with scalar values. Transact-SQL supports numeric and Boolean operators as well as concatenation.
There are unary and binary arithmetic operators. Unary operators are + and – (as signs). Binary arithmetic operators are +, –, *, /, and %. (The first four binary operators have their respective mathematical meanings, whereas % is the modulo operator.)
Boolean operators have two different notations depending on whether they are applied to bit strings or to other data types. The operators NOT, AND, and OR are applied to all data types (except BIT). They are described in detail in coming chapters.
The bitwise operators for manipulating bit strings are listed here, and Example 4.6 shows how they are used:
- ∼ Complement (i.e., NOT)
- & Conjunction of bit strings (i.e., AND)
- | Disjunction of bit strings (i.e., OR)
- ∧ Exclusive disjunction (i.e., XOR or Exclusive OR)
~(1001001) = (0110110) (11001001) | (10101101) = (11101101) (11001001) & (10101101) = (10001001) (11001001) ^ (10101101) = (01100100)
The concatenation operator + can be used to concatenate two character strings or bit strings.
Global variables are special system variables that can be used as if they were scalar constants. Transact-SQL supports many global variables, which have to be preceded by the prefix @@. The following table describes several global variables. (For the complete list of all global variables, see Books Online.)
|@@CONNECTIONS||Returns the number of login attempts since starting the system.|
|@@CPU_BUSY||Returns the total CPU time (in units of milliseconds) used since starting the system.|
|@@ERROR||Returns the information about the return value of the last executed Transact-SQL statement.|
|@@IDENTITY||Returns the last inserted value for the column with the IDENTITY property.|
|@@LANGID||Returns the identifier of the language that is currently used by the database system.|
|@@LANGUAGE||Returns the name of the language that is currently used by the database system.|
|@@MAX_CONNECTIONS||Returns the maximum number of actual connections to the system.|
|@@PROCID||Returns the identifier for the stored procedure currently being executed.|
|@@ROWCOUNT||Returns the number of rows that have been affected by the last Transact-SQL statement executed by the system.|
|@@SERVERNAME||Retrieves information about the local database server. This information contains, among other things, the name of the server and the name of the instance.|
|@@SPID||Returns the identifier of the server process.|
|@@VERSION||Returns the current version of the database system software.|