Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Insert while scan
Message
 
To
26/03/2001 17:00:29
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00488698
Message ID:
00488795
Views:
13
>>>>I would like to copy a set of records but change the key when
>>>>inserting them.
>>>>
>>>>
>>>>select employee
>>>>scan for employee.empid=1234
>>>>  insert into employee(empid,task);
>>>>     values (NewEmpId,employee.task)
>>>>endscan
>>>>
>>>>
>>>>I can't make this work. It appears to get stuck in a loop.
>>>>What am I doing wrong?
>>>
>>>Is this your actual code? It's hard to tell what you're doing here. First off, I'd make sure you're using a different ALIAS for the table you're INSERTing into (assuming it's different from the one you're SCANning).
>>
>>It's not different. I want to insert into the table that I'm scanning.
>>Can that be done?
>
>What is the value of NewEmpID? If it's 1234, you're getting in a loop because you're adding records that match the SCAN filter, so SCAN always has another record to process, which leads to another one being created...
>
>You have a bit of a conflict here. You're expecting to use SCAN to process ALL records in the Employee table with an ID of 1234. SCAN positions the record pointer for you. However, the INSERT - SQL command positions the record pointer at the newly created record. The location of that new record will depend on any index order being set. For example, if your master tag is on EmpID ascending, and you use a lower number for NewEmpID, then the record pointer will be positioned ABOVE your original 1234 record. SCAN then will find the same record again and repeat the process ad infinitum. One way around this would be to temporarily set Employee to "natural" order for the duration of the SCAN:
select employee
><b>SET ORDER TO 0 IN Employee</b>
>scan for employee.empid=1234
>  insert into employee(empid,task);
>     values (NewEmpId,employee.task)
>endscan


Ahhh!   Ok...  What about this...
(btw - NewEmpId would never match the original. 1234 in this case)

<pre>
NewEmpID=5678
select employee
scan for employee.empid=1234
  currec=recno()
  insert into employee(empid,task);
     values (NewEmpId,employee.task)
  goto currec
endscan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform