# Execute command

The `Execute` command executes an SQL statement or a named prepared statement.

## Syntax

    EXECUTE { SQLString | PreparedStatementIdentifier }
    [ USING { String | Identifier } ]

The `Execute` command can be used in either of the following ways:

- To execute an SQL statement entered as an SQLString. The SQLString is passed to the connection without further examination or processing by `ij`.
  Note: Normally, you execute SQL statements directly, not with the `Execute` command.
- To execute a named statement identified by PreparedStatementIdentifier. This command must be previously prepared with `ij`'s `Prepare` command.

To execute either flavor of statement when that statement contains dynamic parameters, specify the values in the `Using` portion of the command. In this style, the SQLString or previously prepared PreparedStatementIdentifier is executed using the values supplied as String or Identifier. The Identifier in the `Using` clause must have a result set as its result. Each row of the result set is applied to the input parameters of the statement to be executed, so the number of columns in the `Using` clause's result set must match the number of input parameters in the `Execute` command's SQL statement. The results of each execution of the `Execute` command's SQL statement are displayed as they are made. If the `Using` clause's result set contains no rows, the `Execute` command's SQL statement is not executed.

When auto-commit mode is on, the `Using` clause's result set is closed upon the first execution of the `Execute` command's SQL statement. To ensure multiple-row execution of the `Execute` command, use the `Autocommit` command to turn auto-commit off.

## Examples

    ij> autocommit off;
    ij> prepare menuInsert as 'INSERT INTO menu VALUES (?, ?, ?)';
    ij> execute menuInsert using 'VALUES
        (''entree'', ''lamb chop'', 14),
        (''dessert'', ''creme brulee'', 6)';
    1 row inserted/updated/deleted
    1 row inserted/updated/deleted
    ij> commit;

    ij> connect 'jdbc:derby:firstdb;create=true';
    ij> create table firsttable (id int primary key,
        name varchar(12));
    0 rows inserted/updated/deleted
    ij> insert into firsttable values
        (10,'TEN'),(20,'TWENTY'),(30,'THIRTY');
    3 rows inserted/updated/deleted
    ij> select * from firsttable;
    ID         |NAME
    ------------------------
    10         |TEN
    20         |TWENTY
    30         |THIRTY

    3 rows selected
    ij> connect 'jdbc:derby:seconddb;create=true';
    ij(CONNECTION1)> create table newtable (newid int primary key,
        newname varchar(12));
    0 rows inserted/updated/deleted
    ij(CONNECTION1)> prepare src@connection0 as 'select * from firsttable';
    ij(CONNECTION1)> autocommit off;
    ij(CONNECTION1)> execute 'insert into newtable(newid, newname)
        values(?,?)' using src@connection0;
    1 row inserted/updated/deleted
    1 row inserted/updated/deleted
    1 row inserted/updated/deleted
    ij(CONNECTION1)> commit;
    ij(CONNECTION1)> select * from newtable;
    NEWID      |NEWNAME
    ------------------------
    10         |TEN
    20         |TWENTY
    30         |THIRTY

    3 rows selected
    ij(CONNECTION1)> show connections;
    CONNECTION0 -   jdbc:derby:firstdb
    CONNECTION1* -  jdbc:derby:seconddb
    ij(CONNECTION1)> disconnect connection0;
    ij>

Parent topic: ij commands and errors reference
Related concepts
ij commands
ij errors
Related reference
Absolute command
After Last command
Async command
Autocommit command
Before First command
Close command
Commit command
Connect command
Describe command
Disconnect command
Driver command
Elapsedtime command
Exit command
First command
Get Cursor command
Get Scroll Insensitive Cursor command
Help command
HoldForConnection command
Last command
LocalizedDisplay command
MaximumDisplayWidth command
Next command
NoHoldForConnection command
Prepare command
Previous command
Protocol command
Readonly command
Relative command
Remove command
Rollback command
Run command
Set Connection command
Show command
Wait For command
Syntax for comments in ij commands
Syntax for identifiers in ij commands
Syntax for strings in ij commands