An important part of your USoft Rules Engine will be written in SQL.
In USoft, key objects such as Constraints, Logical Views and Batch SQL Tasks are all expressed in SQL.
The strength of the USoft concept is that you can achieve maximum expressivity and functionality without using complex SQL syntax. All you need is a good command of the standard ANSI-92 SQL grammar.
The dialect of core SQL constructs supported by USoft is the subset of ANSI SQL listed in this topic.
You can use this SQL dialect in all SQL contexts offered by USoft tools, such as SQL Command, Authorizer Conditions, BenchMark, and SQL statements in GUI tools, except if limitations are stated.
This help topic provides only a brief overview of the main constructs.
Use SELECT statements to retrieve data, to define Logical Views, and (while defining Constraints) to define data that is the subject of restrictive rules.
Syntax
SELECT expression
, ...
FROM table alias
, ... alias
RELATE alias "role" alias
, ...
WHERE condition
{ AND | OR } ...
GROUP BY ...
HAVING ...
ORDER BY ...
|
Example
This SELECT statement retrieves country information of all Latin-American countries:
SELECT c.country
, b.max_no_of_assets
, COUNT(*) no_of_customers
FROM country c
, customer cs
, boundary b
RELATE cs "RESIDES IN" c
WHERE b.country = c.country
AND b.name = 'max_no_of_assets'
AND c.area = 'LATIN AMERICA'
GROUP BY c.country
, b.max_no_of_assets
ORDER BY c.country
|
The required SELECT clause must contain a list (called output list) of 1 or more expressions. Expressions must be separated by commas.
The FROM clause must identify 1 or more tables. Multiple table names must be separated by commas. In USoft, you can omit the FROM clause if you don't need to retrieve stored data from the RDBMS, as when you are only interested in the outcome of a SQL function:
If the FROM clause lists multiple tables, the statement is called a join between those tables. The result of a join contains a record for each combination of a record of the first table and a record of the second table: if the first table has 1000 records and the second table 20, the result will contain 20,000 records. You can limit such a large result set by adding restrictive conditions in one or more RELATE clauses and/or one or more WHERE clauses.
Using RELATE, a USoft-specific extension to SQL, your join result will be restricted to combinations of records of two tables that "belong together" as defined by a Relationship between the tables. You can add multiple RELATE clauses using commas as a separator. Any RELATE clause or clauses must immediately follow the FROM clause.
Using WHERE, AND and OR, you can further restrict your query result by applying standard first-order logic to your result set. This part of SQL is explained in all SQL textbooks.
GROUP BY, HAVING and ORDER BY are also standard SQL clauses explained in any tutorial. Using GROUP BY, you can group retrieved data by category and apply group operations to each category, such as counting customers by country of residence:
Using HAVING, you can apply restrictions to the groups defined by GROUP BY that you want to see in your result.
Using ORDER BY, always as the last clause of any SELECT statement, you can sort the result set in a specific way.
|
A subquery is a SELECT statement nested in an embedding SQL statement, which may be a higher-level SELECT, but also an INSERT, UPDATE or DELETE statement. The subquery is placed between parentheses:
higher-level-statement
(
SELECT ...
)
|
Use subqueries to retrieve additional data needed for the execution of your higher-level statement. You can nest subqueries any level deep.
An independent subquery will return a result that is then passed to the embedding statement. The embedding statement is executed as an independent next step. The subquery does not reference elements in the higher-level query. In the example, the count(*) function will return an integer that is passed to the higher-level SELECT. Then, this higher-level SELECT will execute. In the example, if the subquery returns 10, the overall result set will be restricted to customers who have more than 10 assets. The same boundary value applies to all customers:
SELECT c.name, COUNT(*)
FROM customer c
, asset a
RELATE c "HAS" a
GROUP BY c.name
HAVING COUNT(*) >
(
SELECT b.value
FROM boundary b
WHERE b.name = 'max_no_of_assets'
)
|
A joined subquery refers to a table instance in the higher-level SQL statement, joining each record of the subquery to each corresponding record in the higher-level query, much in the same way as joining takes place when you name more than 1 table in the FROM clause. In the example, each customer who has more assets than allowed in her country of residence is included in the result set. The boundary value is different for customers in different countries:
SELECT c.name, COUNT(*)
FROM customer c
, asset a
RELATE c "HAS" a
GROUP BY c.name
HAVING COUNT(*) >
(
SELECT b.value
FROM boundary b
WHERE b.name = 'max_no_of_assets'
AND b.country = c.country_of_residence
)
|
|
Use INSERT statements to add new records to a table. Provide the values for columns of a specific new record by enumerating them in a VALUES clause:
INSERT INTO table
(
column
, ...
)
VALUES (
expression
, ...
)
|
Or write a subquery that defines the new data to be inserted; this way, you can potentially add many new records in 1 go:
INSERT INTO table
(
column
, ...
)
SELECT expression
, ...
|
|
Use UPDATE statements to modify column values in a set of existing records in a table. The SET clause specifies the new column value. If there are multiple new column values, separate instructions by a comma:
UPDATE table
SET column = expression
, ...
WHERE ...
|
If you omit the optional WHERE clause, ALL columns of table are updated. Add a WHERE clause to restrict the operation to one record or a smaller number of records.
An alternative syntax is first to enumerate the columns to be changed, and then to supply the list of values that they must be changed to. Note the use of parentheses and commas in both lists. The first-mentioned expression is applied to the first-mentioned column, and so on:
UPDATE ...
SET
(
column
, ...
) =
(
expression
, ...
)
|
Either way, instead of enumerating the new value(s) that the column(s) must be set to, you can write a subquery that will retrieve the target value(s):
UPDATE ...
SET
(
column
, ...
) =
(
SELECT expression
, ...
)
WHERE ...
|
In this example, each customer who has more assets than allowed in her country will get the status "Overrun":
UPDATE customer c
SET c.status = 'Overrun'
WHERE EXISTS
(
SELECT ''
FROM asset a
RELATE c "HAS" a
HAVING COUNT(*) >
(
SELECT b.value
FROM boundary b
WHERE b.name = 'max_no_of_assets'
AND b.country = c.country_of_residence
)
)
|
|
Use DELETE statements to remove existing records from a table:
DELETE { FROM | } table
WHERE ...
|
If you omit the optional WHERE clause, ALL the records in the table are removed. Use one or more WHERE clauses to restrict the DELETE action to a smaller number of records, or to 1 specific record.
|
The SQL set operator UNION ALL is supported by USoft.
The SQL set operators UNION, INTERSECT, and MINUS may be used if the underlying RDBMS supports it, in the following places:
•In the SQL Command Dialog. •In USoft Batch, except when used in an external set that has a child set. •In decisions. •In the SqlScript() method. UNION, INTERSECT, and MINUS may be used in Logical View SQL and in Constraint SQL in subselects, that is, in contexts OTHER than the topmost SELECT statement, on condition that the subselect does NOT query a Logical Views that contains UNION ALL.
Furthermore, in the USoft Remote Rules Service API, the UNION operator is supported.
|
USoft Developer supports the use of Oracle (8i or earlier) outer join syntax, using the (+) notation. Outer joins can be nested and non-nested.
On all other database servers, this outer join syntax using (+) is translated to ANSI 92/ODBC outer join syntax.
USoft versus ANSI 92/ODBC Outer Join syntax
USoft Syntax
|
ANSI 92/ODBC Syntax
|
SELECT *
FROM t1, t2
WHERE t1.col1 = t2.col1(+)
|
SELECT *
FROM t1 LEFT OUTER JOIN t2
ON t1.col1 = t2.col1
|
SELECT *
FROM t1, t2, t3
WHERE t1.col1 = t2.col1(+)
AND t2.col1 = t3.col1(+)
|
SELECT *
FROM t1 LEFT OUTER JOIN t2
ON t1.col1 = t2.col1 LEFT OUTER JOIN t3
ON t2.col1 = t3.col1
|
Outer joins can NOT be used in combination with the RELATE keyword, and cannot be used on component tables.
|
The following construct can be used in USoft Developer in most places:
SELECT ...
FROM ...
CONNECT BY ...
START WITH ...
|
but not:
•In constraints. •In the SQL statement of a logical view. •When selecting from component tables. On Oracle, the START WITH clause can precede the CONNECT BY clause.
|
See also
SQL Functions
Host variables in SQL
|