SQL statements can include programming elements just like most other programming languages. (You may want to see my section on Programming.)

  • Developer Comments. Started by two dashes -- or enclosed by slash-asterisk /* and asterisk-slash */. MySQL uses -- (note the following space), /**/ and # (the latter is just like -- ).
  • Statement Layout. Some specific to SQL-Transact.
    • Expressions. In SQL, an expression evaluates a single data value for each row in the result set. EG: SELECT ID, 1+2, price*2 FROM TableA. In most programming languages, expressions are symbols and operators that evaluate to a single data value. Expressions can include constants, functions that return one value, column references, or variables.
  • Data Types. Each column, local variable, expression, and parameter has a related data type.
  • Identifiers. Labels for database objects.
  • Operators. Here is the precedence list in SQL Server:
    • Unary operators: + (Positive), - (Negative), ~ (Bitwise NOT)
    • Math and string operators:
      • * (Multiply), / (Division), % (Modulo)
      • + (Add), (+ Concatenate), - (Subtract)
    • Comparison operators: =, >, <, >=, <=, <>, !=, !>, !<
    • Bitwise operators: ^ (Bitwise XOR), & (Bitwise AND), | (Bitwise OR)
    • Logical operators:
      • NOT
      • AND
    • Assignment operator: =
  • Flow Control
  • Functions
  • Input/Output
  • Program Halts
  • Reserved Keywords. Certain keywords are reserved by SQL Server because they have particular use in T-SQL syntax.

There are four ways that multiple statements can be executed:

  • Batches are non-compiled multiple statements sent to the server. (The end of a batch is signaled in SQL Server Query Analyzer, osql, and isql by use of the GO command.)
  • Scripts are statements stored as files. Scripts usually consist of batches.
  • Stored Procedures are pre-compiled statements on the server that can be called upon.
  • Triggers are stored procedures that are not called but execute when certain events occur.

GeorgeHernandez.comSome rights reserved