使用数组或游标更新具有相同结构的多个表?

时间:2014-01-16 23:54:17

标签: foxpro

使用VFP 6.0 ...我试图看看是否可以使用数组或游标中的数据字段更新多个表。因此,例如,我将创建一个游标并将txt文件中的数据附加到它:

create cursor cTemp(acct c(5), field c(5), newscript c(25))
append from file delimited with tab

...现在我想设置一个循环,使用第一个字段(acct)来查找表,然后使用字段&更新表。来自光标的新闻稿。我在使用字段作为更新命令中的字段名称时遇到问题:

UPDATE acct\table SET &field = newscript

由于该字段将根据acct \ table而改变,因此我需要它在脚本中具有灵活性。

3 个答案:

答案 0 :(得分:1)

首先需要使用内存变量(不是表中的字段)来使用宏扩展"&"。 所以从:f_name=field(1)开始,将表的第一个字段捕获到变量中。 然后围绕UPDATE table SET &f_name = value

进行循环

答案 1 :(得分:0)

Create Cursor cTemp(acct c(5), Field c(5), newscript c(25))
Append From myfile Delimited With Tab

Select cTemp

Scan

* -- Copy values of current record to an object.
    Scatter Name loThis

    * -- Assumes target table is already open. If not add code to open it.
    * -- Also assumes you want to update all records in target table.
    Replace All (loThis.Field) With loThis.newscript In (loThis.acct)

Endscan

答案 2 :(得分:0)

  

很难准确地告诉UPDATE acct \ table你的意思...我假设每个表都被称为“表”,但“acct”是文件夹。在我的例子中,你将拥有c:\ tmp \ t1 \ ctable,c:\ tmp \ t2 \ ctable,c:\ tmp \ t3 \ ctable(顺便说一句,使用像TABLE和FIELD这样的保留字来做事情并不酷比如表名和字段名)。

如果您设置了c:\ tmp,则可以按原样运行此代码。您将最终将包含“xxxxx”的每个表的1条记录更改为光标记录中列出的数据。

* I have a tmp folder. In it I created 3 subfolders: t1, t2, t3.
set default to c:\tmp
* All tables are the same name (ctable). First table in folder t1 with field name cf10
create table t1\ctable (cf10 c(25))
* Insert a record with "xxxxx" as the data
insert into ctable values ("xxxxx")
* this will show you what is in the table. escape to let the program run.
brow last
use

* 2nd table, field is called cf8.
create table t2\ctable (cf8 c(25))
insert into ctable values ("xxxxx")
brow last
use

* 3rd table, field is called cf6
create table t3\ctable (cf6 c(25))
insert into ctable values ("xxxxx")
brow last
use

* create cursor, add 3 records (this is just for testing... this would be like your appending of the text file).
create cursor cTemp(acct c(5), cfield c(5), newscript c(25))
insert into ctemp values ("t1","cf10","data for field10")
insert into ctemp values ("t2","cf8","data for field8")
insert into ctemp values ("t3","cf6","data for field6")
* so I am telling the cursor to update unique field names in a table called CTABLE with unique data
scan
    m.lcExecString="UPDATE " + alltrim(acct)+"\ctable SET "+cfield+"='"+alltrim(newscript)+"'"
    &lcExecString
    select ctable
    browse last
    * make sure to close the file you are updating
    use in select("ctable")
endscan