|
Example: Implementing a Master/Detail Update |
|
|
Suppose that you need to insert records to a table that depends on another record in another table. For example the "ordered products" records and the "order" record. To store the complete order in database, typically you need to insert a record to orders table and insert several records to orderdetails table and you should link the orderdetails records to the order record. orders (The Master Table)
ID CUSTOMER_ID SALESPERSON_ID ORDER_DATE -------- ----------- --------------- ---------- 1 10 3 2003-10-10
orderdetails (The Details Table)
ID ORDER_ID PRODUCT_ID QUANTITY UNIT_PRICE -------- ----------- ---------- --------- ---------- 1 1 12 2 20.00 2 1 20 5 30.00 3 1 23 3 15.00
There is an "one-to-many" relation between the orders.ID field and the orderdetails.ORDER_ID field. Lets see now how we can insert all these records into these tables in one go. We need two process blocks each containing a RelDBUpdateProcess Element. Process Definitions
In the SQL property of the first process with INSERT statement a new record is inserted into the orders table and with selecT @@IDENTITY AS v:ORDER_ID statement the identity of the inserted record is transferred to the pool variable ORDER_ID. See Retrieving Information from database The SQL statement is executed for each product of the order after resolving MScript functions existing in it the SQL statement are resolved. The execution will be as follows. Insert_to_orders_Table Process
The pool variable ORDER_ID will contain 1.
Insert_to_orderdetails_Table Process
The Oracle Database Oracle doesn't have an AutoNumber or Identity column and instead uses sequences. A sequence must first be created for a table before it can be accessed. If we arrange the process queries for Oracle they would be as follows:
|