注册 登录
编程论坛 VFP论坛

关于&表.号码的求助

sharpex1 发布于 2021-12-14 11:53, 3171 次点击
代码
表1=路径+'表1'
update &表1 set 字段=表2.字段 from 表2 wher &表1.号码=表2.号码
提示无法识别,问题应该就出在划线处
请教下,这个 &表1.号码=表2.号码 应该怎么改,感谢
21 回复
#2
sdta2021-12-14 12:02
&表1..号码=表2.号码
#3
sharpex12021-12-14 12:27
回复 2楼 sdta
是两个.吧?还是提示无法识别
#4
mywisdom882021-12-14 15:02
你先测试这样的标准格式,理解UPDATE语句后,再用 宏&
create cursor t1(id i,f1 c(10))
create cursor t2(id i,f1 c(10))
insert into t1(id,f1) values (1,"")
insert into t1(id,f1) values (2,"")
insert into t1(id,f1) values (3,"")

insert into t2(id,f1) values (1,"t1")
insert into t2(id,f1) values (2,"t2")

update t1 set t1.f1=t2.f1 from t1,t2 where t1.id = t2.id

[此贴子已经被作者于2021-12-14 15:05编辑过]

#5
吹水佬2021-12-14 15:12
回复 楼主 sharpex1
是动态表名,试试
&表1..号码=表2.号码
#6
sharpex12021-12-14 15:18
回复 5楼 吹水佬
&表1..号码=表2.号码
还是提示无法识别
#7
mywisdom882021-12-14 15:21
还有 ,表1=路径+'表1'
直接用
表1.字段
就可以,因为 表1是当前打开的别名
t1 = "c:\" + "t1.dbf"
t2 = "c:\" + "t2.dbf"
*!*    create TABLE  &t1(id i,f1 c(10))
*!*    create TABLE &t2(id i,f1 c(10))

*!*    insert into t1(id,f1) values (1,"")
*!*    insert into t1(id,f1) values (2,"")
*!*    insert into t1(id,f1) values (3,"")

*!*    insert into t2(id,f1) values (1,"t1")
*!*    insert into t2(id,f1) values (2,"t2")

update t1 set t1.f1=t2.f1 from &t1,&t2 where t1.id = t2.id
#8
sharpex12021-12-14 15:26
回复 7楼 mywisdom88
你好,我知道表1.号码可以,但是表分布在各不同的文件夹里面,全部放在同一个文件夹会很乱,所以只能用 宏表名
update t1 set t1.f1=t2.f1 from &t1,&t2 where t1.id = t2.id
确实是可以的,但是用到表1名称
我的想法是如果后面要修改,只修改前面定义的表1这个变量,也就是说表1=路径+'表1',修改后 表1=路径+'表3',那么这种写法要找到代码处修改表1 改为表3,如果&表1.号码能实现,就不用改代码了,直接改前面定义就行了

[此贴子已经被作者于2021-12-14 15:46编辑过]

#9
mywisdom882021-12-14 15:36
回复 8楼 sharpex1

t1 = "c:\" + "aba\t1.dbf"
t2 = "d:\" + "bca\t2.dbf"
update t1 set t1.f1=t2.f1 from &t1,&t2 where t1.id = t2.id
#10
mywisdom882021-12-14 15:39
就像这样
use c:\aba\t1.dbf in 0
use d:\bca\t2.dbf in 0
是放在不同的地方,但打开后,在VFP中,他的表就以别名 t1,t2的名称打开了
select t1
brow
select t2
brow
等等,直接用表名称
#11
sharpex12021-12-14 15:49
回复 10楼 mywisdom88
受教了,谢谢
#12
mywisdom882021-12-14 15:52
*!* 建立测试表
CLOSE DATABASES
t1 = "c:\" + "t1.dbf"
t2 = "d:\" + "t2.dbf"

create TABLE &t1(id i,f1 c(10))
create TABLE &t2(id i,f1 c(10))

insert into t1(id,f1) values (1,"")
insert into t1(id,f1) values (2,"")
insert into t1(id,f1) values (3,"")

insert into t2(id,f1) values (1,"t1")
insert into t2(id,f1) values (2,"t2")
insert into t2(id,f1) values (4,"t4")

CLOSE DATABASES && 关闭建立的表,此时VFP环境中,没有打开的表了。

*!* 假设不知道2个表的名称,用别名,表路径,用宏
update p1 set p1.f1=p2.f1 from &t1 as p1 ,&t2 as p2 where p1.id = p2.id


BROWSE
只有本站会员才能查看附件,请 登录
#13
mywisdom882021-12-14 15:54
回复 8楼 sharpex1
看12楼,应该是你要的
这种做法,表名称可以是任何不同的名称,和位置,
但要求表的结构要相同的

[此贴子已经被作者于2021-12-14 15:56编辑过]

#14
laowan0012021-12-14 16:45
表2是实表名吗?如果不是的话,需要  &表2
#15
吹水佬2021-12-14 18:14
回复 6楼 sharpex1
&表1.号码
这个“表1”变量名表示的是表名(或别名),不是文件名(路径+表文件名)
#16
mywisdom882021-12-15 10:27
*!* 建立测试表
CLOSE DATABASES
t1 = "c:\" + "tt1"
t2 = "d:\" + "tt2"
a1 = "tt1.dbf"
a2 = "tt1"

create TABLE &t1(id i,f1 c(10))
create TABLE &t2(id i,f1 c(10))

insert into &t1(id,f1) values (1,"")
insert into &t1(id,f1) values (2,"")
insert into &t1(id,f1) values (3,"")

insert into &t2(id,f1) values (1,"t1")
insert into &t2(id,f1) values (2,"t2")
insert into &t2(id,f1) values (4,"t4")

CLOSE DATABASES && 关闭建立的表,此时VFP环境中,没有打开的表了。

*!* 这里,不需要知道表名称和路径,利用别名,也不理后缀名称是否带 Dbf t1 = "c:\" + "tt1" 或者 t1 = "c:\" + "tt1.dbf" 都可以
update p1 set p1.f1=p2.f1 from &t1 as p1 ,&t2 as p2 where p1.id = p2.id


?&t1..id  && 报错误,不识别
?&a1..id  && 报错误,找不到对象Tt1;这里是 a1 = "tt1.dbf",表名,带后缀.DBF
?&a2..id  && 正确,这里是 a2 = "tt1",表名称,不带后缀


#17
sharpex12021-12-16 16:26
回复 16楼 mywisdom88
受教受教
#18
kiff2021-12-16 16:41
尽量不要用 &
USE (路径+'表1') ALIAS 表1 IN 0
update 表1 set 字段=表2.字段 from 表2 wher 表1.号码=表2.号码
#19
sharpex12021-12-16 16:49
回复 18楼 kiff
为啥尽量不用& ?有啥更好好办法吗师兄。
我的数据分散在各个文件夹中,各种分析时经常要引用,如果程序前面定义好各数据路径,我无论在哪个文件夹做分析,都可以直接用&表名引用,不用每次写路径
#20
kiff2021-12-16 17:16
回复 19楼 sharpex1
方法一,在主程序设置搜索路径:
SET PATH TO 路径1,路径2,路径3...
之后可以直接使用数据表(不用加路径)
如:
update 表1 set 字段1=表2.字段1 from 表2 where 表1.字段2=表2.字段2

方法二,设置路径变量,然后引用路径变量打开数据表,再用引表名操作。
如:
use (路径变量+'表1') in 0
use (路径变量+'表2') in 0
update 表1 set 字段1=表2.字段1 from 表2 where 表1.字段2=表2.字段2


也可以这样:
update (路径变量+'表1') set 字段1=表2.字段1 from (路径变量+'表2') where 表1.字段2=表2.字段2






[此贴子已经被作者于2021-12-16 17:53编辑过]

#21
kiff2021-12-16 17:49
以下是引用sharpex1在2021-12-14 11:53:59的发言:

代码
表1=路径+'表1'
update &表1 set 字段=表2.字段 from 表2 wher &表1.号码=表2.号码
提示无法识别,问题应该就出在划线处
请教下,这个 &表1.号码=表2.号码 应该怎么改,感谢



修正你代码:
表1=路径+'表1'
update &表1 set 字段=表2.字段 from 表2 where 表1.号码=表2.号码


#22
sharpex12021-12-17 10:23
回复 20楼 kiff
编辑掉

[此贴子已经被作者于2021-12-17 10:27编辑过]

1