菜鸟问题,关于用视图进行多表更新

taoyiwan 2004-08-19 10:39:01
本人用sql server做后台,
建一个视图,希望通过该视图进行三个表同时更新。
在PB里建立一个DATAWINDOW连接该视图
可是每次提交时都显示 “视图或函数 'VIEW_main_input' 不可更新,因为修改会影响多个基表。”
而在代码中添加数据只添加到一个表中则没问题。
请教各位高手,是不是PB的DATAWINDOW是不是不能用视图这个方便的办法 进行多表更新啊?
如果可以用视图的话该如何设置呢?
...全文
241 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mittee 2004-08-20
  • 打赏
  • 举报
回复
如果你想在一个数据窗口更新多个表,就不要用视图用数据源。
数据窗口更新多个表没有问题,但是每次只能更新一个,而且更新属性需要写代码,有很多这方面的例程。
视图也可以更新,但是这个要求的条件高。
1TH 2004-08-20
  • 打赏
  • 举报
回复
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
workhand 2004-08-20
  • 打赏
  • 举报
回复
昨天刚看了sql的书,试图对表更新、插入的时候只能对一个表操作,这是sql的规定,dw的更新也只能对一个表更新。
taoyiwan 2004-08-20
  • 打赏
  • 举报
回复
本想建立一个视图,对多表进行一次同时更新,这样既保证了数据的同步也不用频繁书写代码设置DATAWINDOW的更新属性。
看来想偷懒是不行啦,呵呵。
不过把数据加到该视图中,如果只用部分数据仅加到视图中的一个表,那么提交就完全没问题了。
真是郁闷啊!
devotee2000 2004-08-20
  • 打赏
  • 举报
回复
牢记。
taoyiwan 2004-08-20
  • 打赏
  • 举报
回复
"昨天刚看了sql的书,试图对表更新、插入的时候只能对一个表操作,这是sql的规定,dw的更新也只能对一个表更新。"

可是我用SQL SERVER的企业管理器直接操作视图进行多表更新却是可以的,它把数据同时都写到三个表中了。
本人实在水平过低,不知这是不是SQL SERVER企业管理器单独提供的功能。而PB是无法调用此功能?
问题过傻,见笑啦,呵呵
兔子-顾问 2004-08-20
  • 打赏
  • 举报
回复
哦,学了一招。好帖子,好问答。
klbt 2004-08-20
  • 打赏
  • 举报
回复
一个数据窗口更新多个表,用query,不要用视图。

609

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder DataWindow
社区管理员
  • DataWindow社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧