Entering content frame

Function documentation DML Statements Locate the document in its SAP Library structure

Use

The DML (Data Manipulation Language) statements Structure linkINSERT, Structure linkUPDATE and Structure linkDELETE are used to insert, update or delete data on the database respectively.

Activities

Inserting rows

The Structure linkINSERT statement is used to add rows to a table. Host variables may appear in the VALUES clause. A detailed description of the insert statement can be found in the Structure linkOpen SQL Grammar in the Reference Manual.

Example

String empl_name = "Miller";

int empl_id = 911;

 

#sql [ctx] { INSERT INTO employee (name, id)

            VALUES (:empl_name, :empl_id) };

Insert. One a new entry is inserted into the employee table.

Updating rows

The Structure linkUPDATE statement is used to modify rows in a table. Host variables can appear in the SET and Structure linkWHERE clause. A detailed description of the update statement can be found in the Structure linkOpen SQL Grammar in the Reference Manual.

Example

String empl_name = "Smith";

int empl_id = 911;

 

#sql [ctx] { UPDATE employee

            SET name = :empl_name

            WHERE id = :empl_id }

Update. The name of the employee with the id 911 is update to the new value 'Smith'.

Deleting rows

The Structure linkDELETE statement is used to remove rows from a table. Host variables can appear in the Structure linkWHERE clause. A detailed description of the delete statement can be found in the Structure linkOpen SQL Grammar in the Reference Manual.

Example

int empl_id = 1234;

 

#sql [ctx] { DELETE FROM employee

            WHERE id = :empl_id };

Delete. The entry for the employee with the id 1234 is deleted.

Update Count

In contrast to queries, DML statements do not produce a result set. Instead, DML statements return an update count – that is, the number of changed rows. In SQLJ, the update count of the last executed statement can be retrieved by the getUpdateCount() method of the Structure linkExecutionContext object this statement has been executed on.

Example

#sql [ctx] { DELETE FROM employee WHERE id > 500) };

 

int updateCount = ctx.getExecutionContext().getUpdateCount();

System.out.println("" + updateCount + " rows have been inserted.");

Update Count. A Structure linkDELETE statement is executed on the default execution context of the Structure linkconnection context ctx. The number of rows deleted is retrieved using the getUpdateCount() method of this Structure linkexecution context.

 

Leaving content frame