INSERT Execution
When an INSERT statement is executed, the provider executes the POST method of the schema, where you can build the HTTP insert request.
Execute Inserts to JSON
In the POST method, the _input item, one of the Items in API Script, contains the columns to insert.
For example, consider the following statement:
INSERT INTO Persons (Name, Id) VALUES ('Maria Anders','7')
You can use the _input item's attributes to set these column values in the HTTP insert request. In the OData API, this is an HTTP POST. The POST data to make the preceding insert in the OData API is below:
{
"@odata.type": "#ODataDemo.Person",
"Name": "Maria Anders",
"ID": 7
}
In the example schema's corresponding POST method, the required columns are first validated and then used to define the POST data. You can check that a column was provided and alert the user otherwise with the api:validate keyword.
After validating that the needed attributes exist, you can set the column values in the POST data. Set the data attribute to the POST data -- the body of the api:set keyword is convenient for setting long or multiline values like this.
The api:call keyword invokes the operation that will make the HTTP request. Before invoking the operation, use api:set to set the method attribute to the HTTP method you want within the scope of the api:script keyword.
<api:script method="POST">
<api:set attr="method" value="POST"/>
<api:validate attr="_input.Name" desc="Name and Id are required to insert." />
<api:validate attr="_input.Id" desc="Name and Id are required to insert." />
<api:set attr="data">
{
"@odata.type": "#ODataDemo.Person",
"Name": "[_input.Name]",
"ID": [_input.Id]
}
</api:set>
<api:call op="jsonproviderGet"/>
</api:script>
Note that an INSERT statement can sometimes return data, for example, the generated Id of the new record. If you want to access values returned from an insert, use the api:push keyword instead of the api:call keyword.
Access Components of INSERT Statements
In the POST method, the _query item contains the following attributes:
query | The SQL statement, as in the following statement:
INSERT INTO Account (account_name, account_type) VALUES ('Contoso','Company') |
table | The table in the SQL statement. For example, Account in the preceding query. |
Keyword Reference
See the API Script Reference for more information on the keywords used in this section: