611
社区成员




//只使用一个数据窗口dw_1
//Select A.a,A.b,B.c,B.d From A,B Where A.a = B.a
int rc
dw_1.Modify("DataWindow.Table.UpdateTable = ~"A~" a_a.update = yes a_b.update = yes a_a.key = yes") //如果A表的主键不是a,刚此处的a_a.key应该修改为:a_实际主键列的名称.key = yes
rc = dw_1.Update(true,false)
if rc = 1 then //A表保存成功
dw_1.Modify("DataWindow.Table.UpdateTable = ~"B~" b_c.update = yes b_d.update = yes b_c.key = yes") //如果B表的主键不是c,刚此处的b_c.key应该修改为:b_实际主键列的名称.key = yes
dw_1.Update()
end if
只使用一个数据窗口dw_1
//Select A.a,A.b,B.c,B.d From A,B Where A.a = B.a
dw_1.Modify("DataWindow.Table.UpdateTable = ~"A~" a_a.update = yes a_b.update = yes a_a.key = yes")
dw_1.Update(true,false)
dw_1.Modify("DataWindow.Table.UpdateTable = ~"B~" b_c.update = yes b_d.update = yes b_c.key = yes")
dw_1.Update()
Updating more than one table
An important use of Modify is to make it possible to update more than one table from one DataWindow object.
The following script updates the table that was specified as updatable in the DataWindow painter;
then it uses Modify to make the other joined table updatable and to specify the key column and which columns to update.
This technique eliminates the need to create multiple DataWindow objects or to use embedded SQL statements
to update more than one table.
In this example, the DataWindow object joins two tables: department and employee.
First department is updated, with status flags not reset.
Then employee is made updatable and is updated. If all succeeds,
the Update commands resets the flags and COMMIT commits the changes.
Note that to make the script repeatable in the user's session, you must add code to make department the updatable table again:
integer rc
string err
/* The SELECT statement for the DataWindow is:
SELECT department.dept_id, department.dept_name,
employee.emp_id, employee.emp_fname,
employee.emp_lname FROM department, employee ;
*/
// Update department, as set up in the DW painter
rc = dw_1.Update(TRUE, FALSE)
IF rc = 1 THEN
//Turn off update for department 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 table updatable.
dw_1.Modify("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")
//Then update the employee table.
rc = dw_1.Update()
IF rc = 1 THEN
COMMIT USING SQLCA;
ELSE
ROLLBACK USING SQLCA;
MessageBox("Status","Update of employee table failed.Rolling back all changes.")
END IF
ELSE
ROLLBACK USING SQLCA;
MessageBox("Status","Update of department table failed.Rolling back changes to department.")
END IF