Update, insert, and the ? character in DBEXECUTE
?
character.Suppose you have an empty SQL table called Items, with two columns: ID and Name. ID is the primary key.
You have this formula in an unprotected cell:
=DBEXECUTE("configdb", "SELECT items.id,items.name FROM items WHERE ID=1","INSERT into items (ID,Name) VALUES (1,?)")
In View mode, you specify 1 in the cell and press Enter.
Because the table is empty and does not contain an ID with the value 1, the Insert statement is executed. The Insert statement creates this row in the table:
Row number | ID | Name |
---|---|---|
1 | 1 | 1 |
By entering 1, you have specified the ID. The ?
character causes this value to be entered in the Name column.
You now specify 2 in the cell. Because the ID 1 is present, the Insert statement is not executed. Instead, the row that contains ID 1 is updated as shown in this table:
Row number | ID | Name |
---|---|---|
1 | 2 | 1 |
Because the Insert statement is not executed, the Name column is not updated.
You now specify 3 in the cell. Because the ID 1 is not present, the Insert statement is executed and a new row is added:
Row number | ID | Name |
---|---|---|
1 | 1 | 3 |
2 | 2 | 1 |
The values 1 and 3 result from this part of the formula: VALUES (1,?)
.
If you now specify 4, the ID 1 is again present and the row containing ID 1 is updated.
Row number | ID | Name |
---|---|---|
1 | 2 | 1 |
2 | 4 | 3 |
If you specify 5 a further row is added:
Row number | ID | Name |
---|---|---|
1 | 1 | 5 |
2 | 2 | 1 |
3 | 4 | 3 |