请问如何在 PB中从不同的表循环选取所有数据?

[复制链接]
查看11 | 回复6 | 2009-3-27 11:58:16 | 显示全部楼层 |阅读模式
假设我有一个主表,其中包含了两个字段,分别为table_num, table_name
分别保存表编号和表名称,然后每一个表名称都具有一个子表。简单起见,假设
每个子表也只有两个字段sub_num, sub_name。
现在的问题是,要从主表的第一条记录开始循环,并开始检索第一个记录对应的
表名称的表,将这个子表的信息逐条读出来,放到另外一个新表中。直到所有的
表都读完。
我想用select选取子表中的两个字段的内容到两个变量,在insert到新表中去,
但是不知道怎么循环指定子表。麻烦哪为高手帮忙???谢谢。或者有好的办法
回复

使用道具 举报

千问 | 2009-3-27 11:58:16 | 显示全部楼层
最初由 Romance 发布
[B]假设我有一个主表,其中包含了两个字段,分别为table_num, table_name
分别保存表编号和表名称,然后每一个表名称都具有一个子表。简单起见,假设
每个子表也只有两个字段sub_num, sub_name。
[/B]

子表至少也还需要一个相应主表的字段吧。

也学会提问题提的不清不楚的了。让人看不懂你的难点在哪里。


回复

使用道具 举报

千问 | 2009-3-27 11:58:16 | 显示全部楼层
头不清!

也说不清!

基本思路缕出来了......


马上要去客户那,搞不定可有果子吃了.....


回复

使用道具 举报

千问 | 2009-3-27 11:58:16 | 显示全部楼层
你写个存储过程,来个嵌套循环就搞定了。
然后再pb里调用过程。
回复

使用道具 举报

千问 | 2009-3-27 11:58:16 | 显示全部楼层
你是主从表,先写个语句
select a.*,b.* from a.id=b.id
回复

使用道具 举报

千问 | 2009-3-27 11:58:16 | 显示全部楼层
用动态SQL
Description
Use this format to execute a SQL statement that produces a result set in which the input parameters and result set columns are known at compile time.
Syntax
DECLARE Cursor | Procedure
DYNAMIC CURSOR | PROCEDURE
FOR DynamicStagingArea ;
PREPARE DynamicStagingArea FROM SQLStatement
{USING TransactionObject} ;
OPEN DYNAMIC Cursor
{USING ParameterList} ;
EXECUTE DYNAMIC Procedure
{USING ParameterList} ;
FETCH Cursor | Procedure
INTO HostVariableList ;
CLOSE Cursor | Procedure ;
Parameter
Description
Cursor or Procedure
The name of the cursor or procedure you want to use
DynamicStagingArea
The name of the DynamicStagingArea (usually SQLSA)If you need a DynamicStagingArea variable other than SQLSA, you must declare it and instantiate it with the CREATE statement before using it
SQLStatement
A string containing a valid SQL SELECT statement The string can be a string constant or a PowerBuilder variable preceded by a colon (such as :mysql). The string must be contained on one line and cannot contain expressions Enter a question mark (?) for each parameter in the statement. Value substitution is positional; reserved word substitution is not allowed
TransactionObject (optional)
The name of the transaction object that identifies the database
ParameterList (optional)
A comma-separated list of PowerScript variables. Note that PowerScript variables are preceded by a colon (

HostVariableList
The list of PowerScript variables into which the data values will be retrieved
Usage
To specify a NULL value, use the SetNull function.
The DECLARE statement is not executable and can be declared globally.
If your DBMS supports formats of FETCH other than the customary (and default) FETCH NEXT, you can specify FETCH FIRST, FETCH PRIOR, or FETCH LAST.
The FETCH and CLOSE statements in Format 3 are the same as in standard embedded SQL.
To declare a global, shared, or instance cursor or procedure, select Global Variables, Instance Variables, or Shared Variables on the Declare menu of the PowerScript painter. To declare a local cursor, click the Paint SQL button in the PainterBar.
For information about global, instance, shared, and local scope, see "Where to declare variables ".
以上是PB带的帮助
回复

使用道具 举报

千问 | 2009-3-27 11:58:16 | 显示全部楼层
给你一个代码的例子。


DECLARE get_table CURSOR FOR
 SELECT table_name FROM master_table;
FETCH get_table INTO :ls_name;
DO WHILE SQLCA.SQLCode = 0
 ls_sql_select = "select sub_num, sub_name from "+ls_name
 PREPARE SQLSA FROM :ls_sql_select;
 DESCRIBE SQLSA INTO SQLDA;
 DECLARE cursor_table DYNAMIC CURSOR FOR SQLSA;
 OPEN DYNAMIC cursor_table USING DESCRIPTOR SQLDA;
 FETCH cursor_table USING DESCRIPTOR SQLDA;
 DO WHILE SQLCA.SQLCODE=0
  FOR i=1 TO 2
   ls_data=SQLDA.GetDynamicString( i )
   ……(处理过程)
  NEXT
  FETCH cursor_factor USING DESCRIPTOR SQLDA;
 LOOP
 CLOSE cursor_table;
LOOP
CLOSE get_table;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行