注册 登录
编程论坛 VFP论坛

b1分别复制到b2

yd1954 发布于 2025-05-03 10:33, 659 次点击
b1分别复制到b2

表名:b1,b2

将b1分三部分复制到b2.

1.将b1中的xh,qihao,h1,h2,h3字段,且h1=0的字段,复制到b2的相同字段中,也就是说,b1中的所有h1=0的那一行的xh,qihao,h1,h2,h3字段的内容复制到b2的相同字段。

2.将b1的h1=0的上一行的xh字段复制到b2的xh2字段中,放在与1.复制xh的同一行。b2中的xh的值与xh2的值是:xh-1=xh2.且在同一行。

3.b1的其它字段(指的是b1中h1=0的上一行的其它字段,不是h1=0的其它字段,且不包括qihao,h1,h2,h3)的内容,复制到b2的相同字段中,放在xh2的同一行。

注意:第一行h1=0,它的上一行不存在,可忽略。

程序运行后,
b2中的h1的值全都0,:
b2中的xh的值与xh2的值是:xh-1=xh2.且在同一行。
b1中的其它字段复制到b2的相同字段。
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录
11 回复
#2
csyx2025-05-03 11:27
程序代码:
Close Databases
Use b1 In 0
Use b2 In 0 Exclusive
Zap in b2

Insert into b2 (xh,qihao,h1,h2,h3,xh2) select xh,qihao,h1,h2,h3,xh-1 from b1 where h1 = 0

For ii = 1 to AFields(aFlds, 'b1')
    cc = aFlds[ii,1]
    If !(','+Lower(cc)+',' $ ',xh,qihao,h1,h2,h3,xh2,')
        Update b2 set b2.&cc = b1.&cc from b2 inner join b1 on b2.xh2 == b1.xh
    EndIf
EndFor
#3
csyx2025-05-03 14:31
或者这样,执行效率更高些
程序代码:
Close Databases
Use b1 In 0
Use b2 In 0 Exclusive

Select b1.*, b1.xh as xh2 from b1 inner join (select xh from b1 where h1 == 0) b3 on b1.xh == b3.xh-1 into cursor temp
Select b2
Zap
Append From Dbf('temp')
Use in temp
Update b2 set b2.xh = b1.xh, b2.qihao = b1.qihao, b2.h1 = b1.h1, b2.h2 = b1.h2, b2.h3 = b1.h3 from b2 inner join b1 on b2.xh2 == b1.xh-1




[此贴子已经被作者于2025-5-3 14:36编辑过]

#4
schtg2025-05-03 14:40
回复 2楼 csyx
不知我的理解对否?
只有本站会员才能查看附件,请 登录

程序代码:
select * from b1 into cursor t2 readwrite
alter table t2 drop column qihao
alter table t2 drop column h1
alter table t2 drop column h2
alter table t2 drop column h3
alter table t2 rename column xh to xh2
select xh,h1,h2,h3 from b1 where h1 = 0 into cursor t1
select t1.*,t2.* from t1 left join t2 on t1.xh - 1 = t2.xh2 into table b2
#5
yd19542025-05-03 16:18
回复 3楼 csyx
非常感谢老师的帮助,
待下步运行并核实数据。
#6
yd19542025-05-03 16:20
回复 4楼 schtg
感谢!感谢!
接下来是运行、核实数据。
#7
yd19542025-05-04 09:59
回复 3楼 csyx
csyx老师你好!
非常感谢您昨天帮助写的代码。
第一段代码如下:
Close Databases
Use b1 In 0
Use b2 In 0 Exclusive
Zap in b2

Insert into b2 (xh,qihao,h1,h2,h3,xh2) select
xh,qihao,h1,h2,h3,xh-1 from b1 where h1 = 0

For ii = 1 to AFields(aFlds, 'b1')
    cc = aFlds[ii,1]
    If !(','+Lower(cc)+',' $
',xh,qihao,h1,h2,h3,xh2,')
        Update b2 set b2.&cc = b1.&cc from b2 inner
join b1 on b2.xh2 == b1.xh
    EndIf
EndFor

第一段代码运行后
“Insert into b2 (xh,qihao,h1,h2,h3,xh2) select
xh,qihao,h1,h2,h3,xh-1 from b1 where h1 = 0”
这句显示“语法错误”
“Update b2 set b2.&cc = b1.&cc from b2 inner join b1
on b2.xh2 == b1.xh”
这句显示“命令中有不能识别的短语或关键字”

----------------------------------------

第二段代码如下:
Close Databases
Use b1 In 0
Use b2 In 0 Exclusive

Select b1.*, b1.xh as xh2 from b1 inner join (select
xh from b1 where h1 == 0) b3 on b1.xh == b3.xh-1 into
cursor temp
Select b2
Zap
Append From Dbf('temp')
Use in temp
Update b2 set b2.xh = b1.xh, b2.qihao = b1.qihao,
b2.h1 = b1.h1, b2.h2 = b1.h2, b2.h3 = b1.h3 from b2
inner join b1 on b2.xh2 == b1.xh-1
第二段代码运行后,
“Select b1.*, b1.xh as xh2 from b1 inner join (select
xh from b1 where h1 == 0) b3 on b1.xh == b3.xh-1 into
cursor temp”
这句显示“语法错误”
“Append From Dbf('temp')”
这句显示“找不到别名”
“Use in temp”
这句显示“找不到别名”
“Update b2 set b2.xh = b1.xh, b2.qihao = b1.qihao,
b2.h1 = b1.h1, b2.h2 = b1.h2, b2.h3 = b1.h3 from b2
inner join b1 on b2.xh2 == b1.xh-1”
这句显示“命令中有不能识别的短语或关键字”

希望老师帮助解决上述二段代码的问题。谢谢!
#8
yd19542025-05-04 10:00
回复 4楼 schtg
sehtg老师你好!
非常感谢您昨天帮助写的代码。
代码如下:
 clear all
 use b1 shared
use b2
select * from b1 into cursor t2 readwrite
alter table t2 drop column qihao
alter table t2 drop column h1
alter table t2 drop column h2
alter table t2 drop column h3
alter table t2 rename column xh to xh2
select xh,h1,h2,h3 from b1 where h1 = 0 into cursor t1
select t1.*,t2.* from t1 left join t2 on t1.xh - 1 =
t2.xh2 into table b2

我在前面加了三句。
代码运行后,
“select * from b1 into cursor t2 readwrite”
这句显示“命令中有不能识别的短语或关键字”

希望老师帮助解决上述代码的问题。谢谢!
#9
csyx2025-05-04 10:53
以下是引用yd1954在2025-5-4 09:59:34的发言:
这句显示“语法错误”
这句显示“命令中有不能识别的短语或关键字”
这句显示“找不到别名”
这句显示“命令中有不能识别的短语或关键字”

亲测无误,难不成你用的不是 vfp9?
如果不是 vfp9 我就无能为力了,自从17年开始接触 vfp,就已经是 vfp9 了,之前版本有哪些限制恕我一无所知
#10
yd19542025-05-04 11:03
回复 9楼 csyx
老师你好!
我用的是VFP6.0版。论坛里的9,0安装了几次,都不成功。抱歉,
#11
schtg2025-05-04 15:45
回复 8楼 yd1954
改成如下代码,试一试哈
程序代码:
clear all
use b1 shared
use b2
select * from b1 into table _t2
alter table _t2 drop column qihao
alter table _t2 drop column h1
alter table _t2 drop column h2
alter table _t2 drop column h3
alter table _t2 rename column xh to xh2
select xh,h1,h2,h3 from b1 where h1 = 0 into table _t1
select _t1.*,_t2.* from _t1 left join _t2 on _t1.xh - 1 = _t2.xh2 into table B2
#12
yd19542025-05-05 11:15
回复 11楼 schtg
再次感谢老师的帮助。谢谢。
1