2.5. DML Commands 2.5.1. DML Commands JBoss Data Virtualization supports SQL for issuing queries and defining view transformations; see also Section 2.9.1, "Procedural Language" and Section 2.10.1, "Virtual Procedures" for how SQL is used in virtual procedures and update procedures. Nearly all these features follow standard SQL syntax and functionality, so any SQL reference can be used for more information. There are 4 basic commands for manipulating data in SQL, corresponding to the standard create, read, update and delete (CRUD) operations: INSERT, SELECT, UPDATE, and DELETE. A MERGE statement acts as a combination of INSERT and UPDATE. In addition, procedures can be executed using the EXECUTE command or through a procedural relational command. See Section 2.5.8, "Procedural Relational Command" . 2.5.2. SELECT Command The SELECT command is used to retrieve records for any number of relations. A SELECT command consists of several clauses: [WITH ...] SELECT ... [FROM ...] [WHERE ...] [GROUP BY ...] [HAVING ...] [ORDER BY ...] [(LIMIT ...) | ([OFFSET ...] [FETCH ...])] [OPTION ...] See Section 2.6.1, "DML Clauses" for more information about all of these clauses. All of these clauses other than OPTION are defined by the SQL specification. The specification also specifies the order that these clauses will be logically processed. Below is the processing order where each stage passes a set of rows to the following stage. Note that this processing model is logical and does not represent the way any actual database engine performs the processing, although it is a useful model for understanding questions about SQL. WITH stage - gathers all rows from all WITH items in the order listed. Subsequent WITH items and the main query can reference a WITH item as if it is a table. FROM stage - gathers all rows from all tables involved in the query and logically joins them with a Cartesian product, producing a single large table with all columns from all tables. Joins and join criteria are then applied to filter rows that do not match the join structure. WHERE stage - applies a criteria to every output row from the FROM stage, further reducing the number of rows. GROUP BY stage - groups sets of rows with matching values in the GROUP BY columns. HAVING stage - applies criteria to each group of rows. Criteria can only be applied to columns that will have constant values within a group (those in the grouping columns or aggregate functions applied across the group). SELECT stage - specifies the column expressions that should be returned from the query. Expressions are evaluated, including aggregate functions based on the groups of rows, which will no longer exist after this point. The output columns are named using either column aliases or an implicit name determined by the engine. If SELECT DISTINCT is specified, duplicate removal will be performed on the rows being returned from the SELECT stage. ORDER BY stage - sorts the rows returned from the SELECT stage as desired. Supports sorting on multiple columns in specified order, ascending or descending. The output columns will be identical to those columns returned from the SELECT stage and will have the same name. LIMIT stage - returns only the specified rows (with skip and limit values). This model helps to understand how SQL works. For example, columns aliased in the SELECT clause can only be referenced by alias in the ORDER BY clause. Without knowledge of the processing model, this can be somewhat confusing. Seen in light of the model, it is clear that the ORDER BY stage is the only stage occurring after the SELECT stage, which is where the columns are named. Because the WHERE clause is processed before the SELECT, the columns have not yet been named and the aliases are not yet known. Note The explicit table syntax TABLE x may be used as a shortcut for SELECT * FROM x . 2.5.3. INSERT Command The INSERT command is used to add a record to a table. Example Syntax INSERT INTO table (column,...) VALUES (value,...) INSERT INTO table (column,...) query 2.5.4. UPDATE Command The UPDATE command is used to modify records in a table. The operation will result in 1 or more records being updated, or in no records being updated if none match the criteria. Example Syntax UPDATE table SET (column=value,...) [WHERE criteria] 2.5.5. DELETE Command The DELETE command is used to remove records from a table. The operation will result in 1 or more records being deleted, or in no records being deleted if none match the criteria. Example Syntax DELETE FROM table [WHERE criteria] 2.5.6. MERGE Command The MERGE command, also known as UPSERT, is used to add and/or update records. The JBoss Data Virtualization (non-ANSI) MERGE is simply a modified INSERT statement that requires the target table to have a primary key and for the target columns to cover the primary key. The MERGE operation will then check the existence of each row prior to INSERT and instead perform an UPDATE if the row already exists. Example Syntax Note The MERGE statement is not currently pushed to sources, but rather will be broken down into the respective insert/update operations. 2.5.7. EXECUTE Command The EXECUTE command is used to execute a procedure, such as a virtual procedure or a stored procedure. Procedures may have zero or more scalar input parameters. The return value from a procedure is a result set or the set of inout/out/return scalars. Note that EXEC or CALL can be used as a short form of this command. Example Syntax EXECUTE proc() CALL proc(value, ...) EXECUTE proc(name1=>value1,name4=>param4, ...) - named parameter syntax Syntax Rules: The default order of parameter specification is the same as how they are defined in the procedure definition. You can specify the parameters in any order by name. Parameters that have default values and/or are nullable in the metadata, can be omitted from the named parameter call and will have the appropriate value passed at runtime. Positional parameters that are have default values and/or are nullable in the metadata, can be omitted from the end of the parameter list and will have the appropriate value passed at runtime. If the procedure does not return a result set, the values from the RETURN, OUT, and IN_OUT parameters will be returned as a single row when used as an inline view query. A VARIADIC parameter may be repeated 0 or more times as the last positional argument. 2.5.8. Procedural Relational Command Procedural relational commands use the syntax of a SELECT to emulate an EXEC. In a procedural relational command a procedure group name is used in a FROM clause in place of a table. That procedure will be executed in place of normal table access if all of the necessary input values can be found in criteria against the procedure. Each combination of input values found in the criteria results in an execution of the procedure. Example Syntax SELECT * FROM proc SELECT output_param1, output_param2 FROM proc WHERE input_param1 = 'x' SELECT output_param1, output_param2 FROM proc, table WHERE input_param1 = table.col1 AND input_param2 = table.col2 Syntax Rules: The procedure as a table projects the same columns as an exec with the addition of the input parameters. For procedures that do not return a result set, IN_OUT columns will be projected as two columns, one that represents the output value and one named {column name}_IN that represents the input of the parameter. Input values are passed via criteria. Values can be passed by '=','is null', or 'in' predicates. Disjuncts are not allowed. It is also not possible to pass the value of a non-comparable column through an equality predicate. The procedure view automatically has an access pattern on its IN and IN_OUT parameters which allows it to be planned correctly as a dependent join when necessary or fail when sufficient criteria cannot be found. Procedures containing duplicate names between the parameters (IN, IN_OUT, OUT, RETURN) and result set columns cannot be used in a procedural relational command. Default values for IN, IN_OUT parameters are not used if there is no criteria present for a given input. Default values are only valid for named procedure syntax. See Section 2.5.7, "EXECUTE Command" . Note The usage of 'in' or join criteria can result in the procedure being executed multiple times. Note None of the issues listed in the syntax rules above exist if a nested table reference is used. See Section 2.6.5, "FROM Clause" . 2.5.9. Set Operations JBoss Data Virtualization supports the UNION, UNION ALL, INTERSECT, EXCEPT set operations as ways of combining the results of query expressions. Usage: queryExpression (UNION|INTERSECT|EXCEPT) [ALL] queryExpression [ORDER BY...] Syntax Rules: The output columns will be named by the output columns of the first set operation branch. Each SELECT must have the same number of output columns and compatible data types for each relative column. Data type conversion will be performed if data types are inconsistent and implicit conversions exist. If UNION, INTERSECT, or EXCEPT is specified without all, then the output columns must be comparable types. INTERSECT ALL, and EXCEPT ALL are currently not supported. 2.5.10. Subqueries A subquery is an SQL query embedded within another SQL query. The query containing the subquery is the outer query. Supported subquery types: Scalar subquery - a subquery that returns only a single column with a single value. Scalar subqueries are a type of expression and can be used where single valued expressions are expected. Correlated subquery - a subquery that contains a column reference to form the outer query. Uncorrelated subquery - a subquery that contains no references to the outer subquery. 2.5.11. Inline Views Subqueries in the FROM clause of the outer query (also known as "inline views") can return any number of rows and columns. This type of subquery must always be given an alias. An inline view is nearly identical to a traditional view. SELECT a FROM (SELECT Y.b, Y.c FROM Y WHERE Y.d = 3) AS X WHERE a = X.c AND b = X.b See Also: Section 2.6.2, "WITH Clause" 2.5.12. Alternative Subquery Usage Subqueries are supported in quantified criteria, the EXISTS predicate, the IN predicate, and as Scalar Subqueries (see Section 2.3.9, "Scalar Subqueries" ). Example 2.5. Example Subquery in WHERE Using EXISTS Example 2.6. Example Quantified Comparison Subqueries SELECT a FROM X WHERE a >= ANY (SELECT b FROM Y WHERE c=3) SELECT a FROM X WHERE a < SOME (SELECT b FROM Y WHERE c=4) SELECT a FROM X WHERE a = ALL (SELECT b FROM Y WHERE c=2) Example 2.7. Example IN Subquery SELECT a FROM X WHERE a IN (SELECT b FROM Y WHERE c=3) See also Subquery Optimization .

Commands:
MERGE INTO table (column,...) VALUES (value,...)
MERGE INTO table (column,...) query
SELECT a FROM (SELECT Y.b, Y.c FROM Y WHERE Y.d = 3) AS X WHERE a = X.c AND b = X.b
SELECT a FROM X WHERE EXISTS (SELECT 1 FROM Y WHERE c=X.a)
SELECT a FROM X WHERE a >= ANY (SELECT b FROM Y WHERE c=3) SELECT a FROM X WHERE a < SOME (SELECT b FROM Y WHERE c=4) SELECT a FROM X WHERE a = ALL (SELECT b FROM Y WHERE c=2)
SELECT a FROM X WHERE a IN (SELECT b FROM Y WHERE c=3)