Skip to content

Commit 3bbf0e2

Browse files
authored
Merge pull request #10496 from basdebakker/oql-insert-attributes
Add documentation on OQL INSERT statement
2 parents 5479800 + b95cdd6 commit 3bbf0e2

1 file changed

Lines changed: 73 additions & 12 deletions

File tree

content/en/docs/refguide/modeling/domain-model/oql/oql-statements.md

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ From Mendix version 11.3, you can also update object attributes in bulk using OQ
2323

2424
From Mendix version 11.4, you can update object associations as well as attributes in bulk using OQL `UPDATE` statements.
2525

26+
From Mendix version 11.6, you can insert new objects with attributes in bulk using OQL `INSERT` statements.
27+
2628
{{% /alert %}}
2729

2830
## Java API for OQL updates
@@ -45,13 +47,18 @@ The `execute()` method returns the number of objects that were affected by the s
4547

4648
## `DELETE` Statement {#oql-delete}
4749

50+
{{% alert color="info" %}}
51+
Available from Mendix version 11.1.0
52+
{{% /alert %}}
53+
4854
The syntax of `DELETE` statements is:
4955

5056
```sql
5157
DELETE FROM <entity> WHERE <condition>
5258
```
5359

54-
`condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where).
60+
* `entity` is the entity whose objects are being deleted.
61+
* `condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where).
5562

5663
### OQL `DELETE` Limitations
5764

@@ -61,21 +68,39 @@ DELETE FROM <entity> WHERE <condition>
6168

6269
## `UPDATE` Statement {#oql-update}
6370

71+
{{% alert color="info" %}}
72+
Available from Mendix version 11.3.0
73+
{{% /alert %}}
74+
6475
The syntax of `UPDATE` statements is:
6576

6677
```sql
6778
UPDATE <entity>
68-
SET { { <attribute> | <association> } = <expression> } [ ,...n ]
79+
SET { { <attribute> | <association> } = <expression> } [ ,n ]
6980
WHERE <condition>
7081
```
7182

72-
`entity` is the entity whose objects are being updated.
83+
* `entity` is the entity whose objects are being updated.
84+
85+
* `attribute` is an attribute of the entity that is being updated.
86+
87+
An attribute of type `autonumber` can not be updated. The `ID` attribute of an entity cannot be updated.
88+
89+
* `association` is an association that is being updated. Associations can be updated in Mendix version 11.4.0 and above.
90+
91+
Multiple attributes and associations can be updated in the same statement.
7392

74-
`attribute` is an attribute of the entity that is being updated. `association` is an association that is being updated. Multiple attributes and associations can be updated in the same statement. An attribute of type `autonumber` can not be updated. The `ID` field of an entity cannot be updated.
93+
* `expression` is a new value of an attribute or association. Any [OQL expression](/refguide/oql-expressions/) is allowed.
7594

76-
`expression` is a new value of an attribute or association. Any [OQL expression](/refguide/oql-expressions/) is allowed. When updating attributes, the value type of the expression should match the attribute type according to [type coercion precedence](/refguide/oql-expression-syntax/#type-coercion). When updating an enumeration attribute using a literal, the literal must be a valid value for the enumeration. When updating an enumeration attribute using another enumeration, the expression enumeration must be a subset of the attribute enumeration. When updating a string attribute using a string literal, the literal length must be equal to or less than the length of the attribute. In the case of associations, association and entity expressions must match the target association type. Values of type LONG can also be used as association values, but they must be valid ids of associations which are of the target association type.
95+
* When updating attributes, the value type of the expression should match the attribute type according to [type coercion precedence](/refguide/oql-expression-syntax/#type-coercion).
96+
* When updating an enumeration attribute using a literal, the literal must be a valid value for the enumeration.
97+
* When updating an enumeration attribute using another enumeration, the expression enumeration must be a subset of the attribute enumeration.
98+
* When updating a string attribute using a string literal, the literal length must be equal to or less than the length of the attribute.
99+
* In the case of associations, association and entity expressions must match the target association type.
100+
101+
Values of type LONG can also be used as association values, but they must be valid ids of associations which are of the target association type.
77102

78-
`condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where).
103+
* `condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where).
79104

80105
Example:
81106

@@ -139,7 +164,48 @@ SET
139164
SpecializationAttribute = 1
140165
```
141166

142-
## Joins
167+
## `INSERT` Statement {#oql-insert}
168+
169+
{{% alert color="info" %}}
170+
Available from Mendix version 11.6.0
171+
{{% /alert %}}
172+
173+
The syntax of `INSERT` statements is:
174+
175+
```sql
176+
INSERT INTO <entity> ( <attribute> [ , …n ] ) <oql-query>
177+
```
178+
179+
* `entity` is the entity for which new objects will be created.
180+
181+
* `attribute` is an attribute of the entity that will be inserted.
182+
183+
* `oql-query` is any OQL query that returns same number of columns as the number of attributes that will be inserted.
184+
This query can select data from persistable entities and/or [view entities](/refguide/view-entities/).
185+
186+
Example:
187+
188+
```sql
189+
INSERT INTO Module.Order ( OrderNumber, CustomerNumber )
190+
SELECT NewOrderNumber, Loader.TemporaryData_Customer/Loader.Customer/Number FROM Loader.TemporaryData
191+
```
192+
193+
### OQL `INSERT` Limitations
194+
195+
* It is not yet possible to insert associations. As a workaround, they can be added after the `INSERT` using an OQL `UPDATE` statement.
196+
* Attributes of type "Date and time" with a default value of `'[%CurrentDateTime%]'` will not have their default values set when the `INSERT` statement does not specify them.
197+
As a workaround, explicitly insert the attribute with a value of `'[%CurrentDatetime%]'` in the `SELECT` part.
198+
* When using Oracle, due to database limitations, inserting attributes of type unlimited string or binary is not supported.
199+
* The general limitations for OQL statements also apply. See [General Limitations for OQL Statements](#oql-limitations), below.
200+
201+
## General Limitations for OQL Statements {#oql-limitations}
202+
203+
* OQL statements can be used only with persistable entities.
204+
* Entity access rules are not applied to any OQL statements.
205+
* No event handlers will be executed.
206+
* Runtime and client state will not be updated with the changes.
207+
208+
### Joins
143209

144210
You cannot directly join other entities in the `FROM` clause of OQL `DELETE` or in the `UPDATE` clause of OQL `UPDATE`. However, you can achieve the same result using long paths or subqueries. For example:
145211

@@ -160,9 +226,4 @@ WHERE ID IN (
160226
WHERE Module.Customer/Name = 'Mary' )
161227
```
162228

163-
## General Limitations for OQL Statements {#oql-limitations}
164229

165-
* OQL statements can be used only with persistable entities.
166-
* Entity access rules are not applied to any OQL statements.
167-
* No event handlers will be executed.
168-
* Runtime and client state will not be updated with the changes.

0 commit comments

Comments
 (0)