Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Doing Updates Against Multiple Database Tables from the Same

Datawindow
This Script Demonstrates How A Datawindow Based Upon A Join Of Multiple Tables Can Have Update Capability Against
All Of Those Tables, Eliminating The Need To Create Multiple Datawindows Or Write Embedded SQL Scripts To
Propagate Changes Across Tables .

int rc
string err

The datawindow is built upon a join of two tables, department and employee. The select that it is based upon appears
as follows:

SELECT department.dept_id, department.dept_name,


employee.emp_id, employee.emp_fname,
employee.emp_lname FROM department, employee

The datawindow was initially set up with update capability against the department table. So when the datawindow is
initially updated, it will apply the SQL statements to the department table and the columns in the department table that
are set to be updateable. Set the second Update argument (resetflag) to false so that the update status's of the
datawindow are not cleared if the update is successful. This will allow us to change tables and issue a second update
on the datawindow this time applying the SQL statements to the second table. (We will assume that SetTransObject
was used, so that our database changes will not be automatically committed for us.)

rc = dw_1.Update(true,false)  // Don't clear the update flags if


successful.

// If the update against department table was successful, we want to modify


the update
// characteristics of the datawindow to point to the next table ...

IF rc = 1 THEN

  // First, turn off update for departments columns ...


  dw_1.Modify("department_dept_name.Update=No")
  dw_1.Modify("department_dept_id.Update=No")
  dw_1.Modify("department_dept_id.Key=No")

  // Make employee the new updateable table ...


  dw_1.dwModify("DataWindow.Table.UpdateTable='employee' ")

  // Turn on update for desired employee columns ...


  dw_1.Modify("employee_emp_id.Update=Yes")
  dw_1.Modify("employee_emp_fname.Update=Yes")
  dw_1.Modify("employee_emp_lname.Update=Yes")
  dw_1.Modify("employee_emp_id.Key=Yes")

  // Update the employee table ...


  rc = dw_1.Update(true, true)
  IF rc = 1 THEN
     COMMIT USING SQLCA;
  ELSE
     MessageBox(Update of employee table failed, Rolling back changes to &
                department and employee)
     ROLLBACK USING SQLCA;
  END IF
  // Reset the update properties of the datawindow back to the original
department table.
  dw_1.Modify("department_dept_name.Update=Yes")
  dw_1.Modify("department_dept_id.Update=Yes")
  dw_1.Modify("department_dept_id.Key=Yes")
  dw_1.Modify("DataWindow.Table.UpdateTable = 'department' ")

  dw_1.Modify("employee_emp_id.Update=No")
  dw_1.Modify("employee_emp_fname.Update=No")
  dw_1.Modify("employee_emp_lname.Update=No")
  dw_1.Modify("employee_emp_id.Key=No")

ELSE
  MessageBox(Update of department table failed, Rolling back changes to &
              department)
  ROLLBACK USING SQLCA;
END IF

The above example uses Modify to change the update properties of the datawindow. Dot notation can also be used to
make these changes. The following is an example of dot notation to make these changes:

dw_1.Object.department_dept_name.Update = 'Yes'
dw_1.Object.department_dept_id.Update = 'Yes'
dw_1.Object.department_dept_id.Key = 'Yes'
dw_1.Object.DataWindow.Table.UpdateTable = 'department'

You might also like