Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SET RELATION TO to update a cursor
Message
From
15/08/2019 16:46:41
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01670104
Message ID:
01670110
Views:
50
>Hi,
>
>I found a problem when updating one cursor from another, using SET RELATION approach. The names are:
>TARGET cursor is the one being updated
>SOURCE cursor is the one from which the data is taken for update
>
>
>CREATE CURSOR TARGET (pk_field i, task_no n(6,0), week_fr n(6,0), week_to n(6,0), week_no n(6,0), employee c(10) )
>INSERT INTO TARGET (pk_field, task_no, week_fr, week_to, week_no, employee ) VALUES ;
>	(1, 10, 1, 52, 4, '')
>INSERT INTO TARGET (pk_field, task_no, week_fr, week_to, week_no, employee ) VALUES ;
>	(2, 10, 1, 52, 4, '')
>INSERT INTO TARGET (pk_field, task_no, week_fr, week_to, week_no, employee ) VALUES ;
>	(3, 10, 1, 52, 4, '')
>
>SELECT TARGET
>INDEX ON pk_field TAG pk_field
>SET ORDER TO TAG pk_field
>
>*--  create SOURCE cursor
>CREATE CURSOR SOURCE (pk_field i, task_no n(6,0), week_fr n(6,0), week_to n(6,0), week_no n(6,0), employee c(10) )
>
>INSERT INTO SOURCE (pk_field, task_no, week_fr, week_to, week_no, employee ) VALUES ;
>	(5, 10, 1, 52, 4, '')
>INSERT INTO SOURCE (pk_field, task_no, week_fr, week_to, week_no, employee ) VALUES ;
>	(2, 10, 1, 52, 23, '')
>
>SELECT TARGET
>SET RELATION TO PK_FIELD INTO SOURCE
>GO TOP 
>replace week_no WITH SOURCE.week_no, week_fr WITH SOURCE.week_fr, week_to WITH SOURCE.week_to  all
>
>BROWSE TITLE "TARGET after update" 
>
>
>Note that above, because the SOURCE has fewer records than a TARGET (real case), the last record
>of the TARGET is set to 0 (zero) all fields.
>
>Is this how the code is supposed to work or I am doing something wrong?

You are doing it wrong, and it is easy to go wrong with set relation. Explained before you don't need it.
You are creating index on the wrong cursor (it doesn't matter if TARGET is indexed or not), source must be indexed.
Second, you are not filtering only those records that should be updated. The ones with no match would be at EOF where values are 0.
Create Cursor Target (pk_field i, task_no N(6,0), week_fr N(6,0), week_to N(6,0), week_no N(6,0), employee c(10) )
Insert Into Target (pk_field, task_no, week_fr, week_to, week_no, employee ) Values ;
	(1, 10, 1, 52, 4, '')
Insert Into Target (pk_field, task_no, week_fr, week_to, week_no, employee ) Values ;
	(2, 10, 1, 52, 4, '')
Insert Into Target (pk_field, task_no, week_fr, week_to, week_no, employee ) Values ;
	(3, 10, 1, 52, 4, '')

Select Target
Index On pk_field Tag pk_field
Set Order To Tag pk_field

*--  create SOURCE cursor
Create Cursor Source (pk_field i, task_no N(6,0), week_fr N(6,0), week_to N(6,0), week_no N(6,0), employee c(10) )

Insert Into Source (pk_field, task_no, week_fr, week_to, week_no, employee ) Values ;
	(5, 10, 1, 52, 4, '')
Insert Into Source (pk_field, task_no, week_fr, week_to, week_no, employee ) Values ;
	(2, 10, 1, 52, 23, '')
Index On pk_field Tag pk_field
Set Order To Tag pk_field

Select Target
Set Relation To pk_field Into Source
Scan For Seek(pk_field,'source','pk_field')
	Replace week_no With Source.week_no, week_fr With Source.week_fr, week_to With Source.week_to
Endscan
Browse Title "TARGET after update"
Again, I would repeat do not follow this path of set relation for a simple update. That just creates a mess.
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform