Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Update and insert if _tall = 0
Message
From
26/05/2015 11:58:10
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01620194
Message ID:
01620203
Views:
69
Tamar already mentioned the Merge Command for this situation, which I mentally classify as "Upsert".

For your special case I would create either a common "Upsert" function or a special Upsert-Object with several small Upsert_Table_UseCase methods.
If you were to port to any SQL variant supporting EITHER Merge or Upsert syntax, you already have encapsulated the areas needing mainainance as that syntax is not only clearer but faster as well, and would have to code only the methods for that specific dialect used at the backend new among your customers. The vfp SQL, while runnable as SQL, is NOT the best solution for vfp and clearly not for any SQL backend supporting own Merge/Upsert syntax.

If you instantiate such an object via facade/factory call according to backend dialect, calling code can totally stay in place

As I am sometimes guilty of aiming for (premature opimized) performance, if an identical method signature can be found for xBase Seek/Locate and SQL, I'd leave seek() in the vfp relevant implementation of the upsert-functionality ;-))

But you could already code a "vfp-SQL" upsert variant of thisobject to be in place to override with the specific merge syntax.
Even then I'd be veeery leery to blindly fire such an update as it could fire for more than one record, wheras your previous xBase was guaranteed only to modify 1 record.

to get an idea:
Select (table_pk_name) as _ID from (datatable) into cursor T_PK Where &tcWhereClause
Do Case
    case Reccount("T_PK") = 0
        insert...

    case Reccount("T_PK") = 1
        update (datatable) set &tcSet_ReplaceList Where &table_pk_name = T_PK._ID
    OTHERWISE
        *****
EndCase
HTH and is clear

thomas


>Hi all,
>
>normally I would do a seek on a table that has a 2 field index, if I find the entry I update a field, if not I create a new record.
>I was thinking about changing this to an update command, but wondering if there are any issues, speed mainly, with this.
>
>update table1 set field3 = "some data" where field1 = "ABC" and field2 = "DEF"
>if _tally = 0
>    insert into table1 (...) values(...)
>endif
>
>it doesn't seem to slow things down as against a seek but I'm wondering if this is the dont thing.
>the reason I would like to move to an update command is to modify code to be more like .net and sql which we're migrating to.
>
>~M
Previous
Reply
Map
View

Click here to load this message in the networking platform