`
zjut_xiongfeng
  • 浏览: 280171 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

datawindow stored procedure update 的使用介绍

阅读更多

This document explains how to implement the new feature of the DataWindow which uses stored procedures to insert, update, and delete data from a database

Overview

In previous versions of PowerBuilder, it was possible to use a stored procedure as a datasource
for a DataWindow, however to use a stored procedure for update, insert or delete, the SQLPreview
event had to be used to substitute the generated SQL statement for a stored procedure call.

PowerBuilder 7.0 adds direct support for stored procedures for update, insert and delete actions.
This makes it possible to design a DataWindow entirely on stored procedures within the painter.

Specifying Update Properties

When a DataWindow is based on a stored procedure, the DataWindow is by default not updateable. When stored procedures are used for all three updating actions, it is not necessary to change the properties the Specify Update Properties dialog, but the taborder of the columns to be updated must be set to a non-zero, positive, number.

In the following example a set of simple ASA stored procedures are given for updating the EAS Demo DB department table. The stored procedure sp_select_departments can be used as a datasource for the DataWindow as usual, however the other three procedures must be associated using the Stored Procedure Update menu item.

//Note : The tilde character "~" represents the terminator character.
drop procedure sp_select_departments ~
drop procedure sp_insert_department ~
drop procedure sp_update_department ~
drop procedure sp_delete_department ~

create procedure sp_select_departments()
result (dept_id int, dept_name char(40), dept_head_id int)
begin
select dept_id, dept_name , dept_head_id
from department
end~

create procedure sp_insert_department(in id int, in name char(40), in head_id int)
begin
insert into department
values (id, name, head_id)
end~

create procedure sp_update_department(in old_id int, in new_id int, in name char(40), in head_id int)
begin
update department
set dept_id = new_id,
dept_name = name ,
dept_head_id= head_id
where dept_id = old_id
end~

create procedure sp_delete_department(in id int)
begin
delete from department where dept_id = id
end~


The stored procedure arguments must match the columns of the DataWindow. For each argument, you can specify to use the original value or the current value. The original value is typically used in the where clause. For example in the procedure sp_update_department you would check Use Original for the old_id argument and uncheck it for the new_id, name and head_id arguments.


datawindow


Working with different databases

The Stored Procedure Update functionality is not dependent on the particular database used, although the database itself can pose certain restrictions. Similar procedure sets can be defined for other databases like Sybase ASE or Oracle.

// ASE syntax
drop procedure sp_select_departments ~
drop procedure sp_insert_department ~
drop procedure sp_update_department ~
drop procedure sp_delete_department ~

create procedure sp_select_departments
as
begin
select dept_id, dept_name , dept_head_id
from department
end~

create procedure sp_insert_department(@dept_id int, @dept_name char(40), @dept_head_id int)
as
begin
insert into department
values (@dept_id, @dept_name, @dept_head_id)
end~

create procedure sp_update_department(@old_dept_id int, @new_dept_id int, @dept_name char(40), @dept_head_id int)
as
begin
update department
set dept_id = @new_dept_id,
dept_name = @dept_name ,
dept_head_id= @dept_head_id
where dept_id = @old_dept_id
end~

create procedure sp_delete_department(@dept_id int)
as
begin
delete from department where dept_id = @dept_id
end~

Either a stored procedure or a sql-statement can perform each of the separate SQL actions Update Insert or Delete. But if the DataWindow is not updateable, only those actions with type Stored Procedure will be carried out.

At run-time it can be verified by checking the value of the dw_control.Object.DataWindow.Table.SqlAction.Type
in which sqlaction can be either Update Insert or Delete. Also the Method (stored procedure) and the Arguments are available at run-time and can be modified after the Type has been set to SP.

The stored procedure will usually be prefixed by its owner. Most databases don't require an owner name before the stored procedure name, however you will have to verify this with your database vendor (ASE requires the owner name to prefix the stored procedure i.e. dbo.sp_update.)

The support for the stored procedure datasource hasn't been changed in PB7, so that the same script rules apply as in earlier versions.

In the following Oracle example the stored procedure for retrieval is rewritten using the PBDBMS package.

// Oracle syntax using PBDBMS
drop procedure sp_select_departments ~
drop procedure sp_insert_department ~
drop procedure sp_update_department ~
drop procedure sp_delete_department ~

create procedure sp_select_department
as
begin
PBDBMS.Put_Line('select dept_id, dept_name , dept_head_id ');
PBDBMS.Put_Line('from department');
end~


create procedure sp_insert_department(id in int, name in varchar2, head_id in int)
as
begin
insert into department
values (id, name, head_id);
end~


create procedure sp_update_department(old_id in int, new_id in int, name in varchar2, head_id in int)
as
begin
update department
set dept_id = new_id,
dept_name = name ,
dept_head_id= head_id
where dept_id = old_id;
end~


create procedure sp_delete_department(id in int)
as
begin
delete from department where dept_id = id;
end~

select * from all_errors~

A package with suitable stored procedures could also have been used, in which case the method must be prefixed by the package name (see also Tech Doc 44450 and 44553).

Note:
PowerBuilder does not retrieve a list of stored procedures that are contained within packages so that they cannot be selected in the painter. But the stored procedures can be executed at run-time.

An example script

Suppose similar sets of stored procedures exists within and ASA, ASE and Oracle Server; and the Oracle update procedures have been defined in the package SYSTEM.PKG_SPDWC. The following script fragment gives an example on how the DataWindow can call different stored procedures dependant on the database server the application is connected to. The script can be generalized in many ways, for example the names of the stored procedures don't have to be hard-coded but could also have been stored in an ini file or even in the database.

// current server
string gs_server
// list of servers
string gs_servers[] = {"my_ASA_server", "my_O84_server", "my_SYC_server")

// list of stored procedures for the different databasesdatabases.
string is_spupdates[] = {"sp_update_department", "SYSTEM.PKG_SPDWC.sp_update_department", "dbo.sp_update_department"}
string is_spinsertes[] = {"sp_insert_department", "SYSTEM.PKG_SPDWC.sp_insert_department", "dbo.sp_insert_department"}
string is_spdeletes[] = {"sp_delete_department", "SYSTEM.PKG_SPDWC.sp_delete_department", "dbo.sp_delete_department"}
string is_spselect[] = {"sp_select_department", "SYSTEM.sp_select_department", "dbo.sp_select_department"}

// list of stored procedures the DataWindow was created upon
string ls_default_update = "dbo.sp_update_department"
string ls_default_insert = "dbo.sp_insert_department"
string ls_default_delete = "dbo.sp_delete_department"
string ls_default_select = "dbo.sp_select_department"


// Replace example script :
string ls_update_method, ls_insert_method, ls_delete_method, ls_select_method
integer l_upper, li_i = 0
boolean b_found = false

//check which is the current server
li_upper = UpperBound(gs_servers)
for li_i = 1 to li_upper
if gs_servers[li_i] = gs_server then
b_found = true
exit
end if
next

//replace the names of the stored procedures
if b_found then
ls_update_method = dw_1.Object.DataWindow.Table.UPDATE.Method
ls_insert_method = dw_1.Object.DataWindow.Table.INSERT.Method
ls_delete_method = dw_1.Object.DataWindow.Table.DELETE.Method
ls_select_method = dw_1.object.DataWindow.Table.Procedure
dw_1.Object.DataWindow.Table.UPDATE.Method = gf_substitute(ls_update_method, ls_default_update, is_spupdates[li_i])
dw_1.Object.DataWindow.Table.INSERT.Method = gf_substitute(ls_insert_method, ls_default_insert, is_spinsertes[li_i])
dw_1.Object.DataWindow.Table.DELETE.Method = gf_substitute(ls_delete_method, ls_default_delete, is_spdeletes[li_i])
dw_1.object.DataWindow.Table.Procedure = gf_substitute(ls_select_method, ls_default_select, is_spselect[li_i])
else
MessageBox("Error Unknown Server", "No Match : " + gs_server)
end if


//global substitute string function
string function gf_substitute(as_1, as_2, as_3)
integer li_pos, li_1, li_2, li_3
string ls

li_pos = pos(as_1, as_2)
li_1 = len(as_1)
li_2 = len(as_2)
li_3 = len(as_3)

if li_pos > 0 and li_3 > 0 then
ls = left(as_1, li_pos - 1) + as_3 + right(as_1, li_1 - li_2 - li_pos + 1)

分享到:
评论

相关推荐

    PowerBuilder DataWindow的数据更新技术及应用.doc

    在更新DataWindow时,PowerBuilder会根据这些缓冲区的状态生成相应的SQL语句,如Update、Delete或Insert。例如,当调用Update函数时,未修改的记录不会产生任何SQL操作,已修改的记录会被Update,新添加的记录会被...

    powerbuilder中DATAWINDOW使用技巧

    以上介绍的是PowerBuilder中DataWindow的一些高级使用技巧,包括动态创建、数据导出至Excel/Word、打印设置以及版本兼容性调整等方面的知识点。通过掌握这些技巧,开发者可以更加高效地利用DataWindow来处理和展示...

    DataWindow .NET 2.5 完美破解补丁

    这个“完美破解补丁”似乎提供了对 DataWindow .NET 2.5 的未授权访问,允许在 Visual Studio 2005(VS2005)环境中使用。然而,请注意,使用未经授权的软件违反了版权法,可能引起法律问题,不建议进行此类操作。 ...

    DATAWINDOW技术参考手册

    在《DATAWINDOW技术参考手册》中,首先介绍了DataWindow组件的基本概念及其在PowerBuilder中的应用价值。DataWindow是一种高级的数据展示与处理控件,它允许开发者通过简单的拖拽操作来创建复杂的数据展示界面,并...

    DataWindow数据窗口资料

    “第07章教案.ppt”可能详细介绍了DataWindow的使用方法,包括实例演示和练习,帮助学习者逐步掌握这一强大的工具。通过这份资料,你可以学习到如何设计和使用DataWindow来展示和操作数据库中的数据,进一步提升你的...

    PB 刷新datawindow当前行的数据

    功 能: 刷新DW当前行数据,不可刷新NO update or 带arguments的DW 参数说明: adw 目标DW 返 回 值: integer 成功返回1,失败返回-1 作 者: sean 创建时间: 2010年8月18日 *******************************************...

    Datawindow 导出到PDF文件

    在IT行业中,DataWindow是一种由PowerBuilder开发工具提供的数据展示组件,它允许程序员设计和创建交互式的报告和表格。PDF(Portable Document Format)文件格式则是一种广泛应用的文档格式,能够跨平台保持一致的...

    使用DataWindow.Net在web上打印

    标题中的“使用DataWindow.Net在web上打印”指的是在Web应用程序中利用PowerBuilder的DataWindow.Net组件进行数据展示和打印的技巧。DataWindow.Net是PowerBuilder.NET框架的一部分,它允许开发者在ASP.NET环境中...

    PB-Datawindow函数大全

    在使用DataWindow时,有时我们需要确保用户只能追加新记录而不能修改现有记录。这可以通过设置`Protect`属性来实现。对于每一列,我们可以使用如下的设置: ```plaintext If(IsRowNew(), 0, 1) ``` 这里的`...

    pb 导入excle 到datawindow

    本文将深入探讨如何使用PowerBuilder将Excel数据导入到DataWindow中,这是一项常见的数据迁移任务,尤其在数据处理和分析时非常有用。 首先,我们需要理解PowerBuilder中的DataWindow组件。DataWindow是Power...

    pb12 datawindow 程序员指南

    描述DataWindow objects,介绍使用方法。英文版

    pb中xml导入datawindow源码

    本文将深入探讨如何在PowerBuilder中使用XML导入DataWindow的源码。这个过程涉及到以下几个关键知识点: 1. **XML解析**:在PB中,你可以使用内置的XML解析器(如XMLDocument对象)来读取和解析XML文件。首先,你...

    datawindow.net 2.5

    使用DataWindow.NET 2.5,开发者可以享受到以下功能: - 集成到Visual Studio IDE,提供拖放式设计体验。 - 支持多种.NET语言,如C#和VB.NET。 - 数据绑定能力,可以直接连接到.NET的数据源。 - 自动化数据处理,...

    PB datawindow保存为bmp

    接着,使用`BitBlt`函数将DataWindow的内容复制到这个新创建的DC中。最后,使用`CreateDIBSection`创建一个DIB(设备无关位图),并将其与DC关联,从而可以将位图保存到磁盘上的BMP文件。 以下是一个简单的步骤概述...

    PB12.6_DataWindow

    这个东东是什么?不需要说明了吧,如果不明白是什么东西的就不要浪费积分了,VS2012\...4、完成以上步骤后,我们可以把DataWindowControl看作一个DataGridView来使用(可以retrieve 数据表),具体使用就不详细说明了。

    datawindow.rar

    在本篇文章中,我们将深入探讨DataWindow的概念、功能、使用方法及其在实际开发中的应用。 一、DataWindow基础 DataWindow可以看作是数据库查询的容器,它封装了SQL语句的执行、结果集的管理以及数据显示的逻辑。...

    PB日常使用技巧(主要是datawindow)

    1. **打开DataWindow:** 使用DataWindow Painter打开目标DataWindow。 2. **设置自动折行:** 对于需要自动折行的列,执行以下步骤: - 双击该列以打开其属性窗口。 - 在Position标签页下,勾选`AutosizeHeight`...

    The Definitive DataWindow 2

    本书《The Definitive DataWindow 2》是由Rik Brooks撰写,是关于PowerBuilder和DataWindow使用的权威指南。Rik Brooks在1999年写作本书,并于2000年首次出版。本书第二版在原有基础上进行了更新,删除了已经过时的...

    如何在ASP.NET 开发平台中使用DataWindow.NET 开发WebForm 网站系统(wat) .pdf

    ### 如何在ASP.NET开发平台中使用DataWindow.NET开发WebForm网站系统 #### 知识点一:ASP.NET与DataWindow.NET的关系 - **ASP.NET**:是微软提供的用于构建Web应用程序的一个框架,它允许开发者利用.NET Framework...

Global site tag (gtag.js) - Google Analytics