SML# Document Version 4.0.0
22 SQL Expressions and Commands

22.2 Extended ML Expressions for SQL Queries

As defined in Chapter 19, SML# extends the Standard ML expressions with the following constructs for SQL queries:

exp ::=
 | _sqlserver (appexp)? : ty SQL servers
 | _sql pat => sqlfn SQL execution function
 | _sql sql SQL query fragments
atexp ::=
 | _sql (sql) SQL query fragments

sql and sqlfn are given below:

sql ::= sqlexp SQL value expressions
 | sqlselect SELECT queries
 | sqlclause SQL query clauses
 | sqlcommand SQL commands
sqlfn ::= sqlselect SELECT queries
 | sqlclause SQL query clauses
 | sqlcommand SQL commands

sqlexp is defined in Section 22.4. sqlselect and sqcommand are defined in Section 22.5. sqlcommand is defined in Section 22.6.

In the expressions that begins with _sql, except for the position surrounded by ...(exp) or (...exp) (see Section 22.4, 22.5, and 22.6 for details), the following words are recognized as keywords (this list of keywords is reused from Section 17.2). In the sequel, the following words are refered to as SQL keywords.

asc all begin by commit cross default delete desc distinct fetch first from group inner insert into is join limit natural next not null offset only on or order rollback row rows select set update values where

The above syntax has the following limitations:

  • pat following _sql must not begin with a left parenthesize “(.”

  • The sql of _sql sql as expressions (top-level) must begin with an SQL keyword.

  • Any _sql sql as expressions (top-level) must occur only at the following positions:

    • the immediate right side of = in a val and fun declarations,

    • the position between in and end in a let expression,

    • the inside of a sequential execution expression (;),

    • immediately surrounded by parenthesizes that are not a part of any tuple, and

    • the immediate right side of => in a fn expression occurring at one of the above four positions.