Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compare Between Two Tables
Message
From
23/01/2003 07:18:34
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00744613
Message ID:
00744671
Views:
9
>Hi guys..
>I want to compare the records between two tables
>
>Can any of you tell me the fastest way to do it..
>
>
>thanks,

Tut,
There are multiple ways to do it and some of them rely on tables structures be 'exactly' the same and no memo/general fields exist.
Depending on also to file size it's hard to determine a 'fastest' and working under all cases way.
ie: If files are small and no memo/general and structures 'exactly' same:

select table1
copy to temp1.txt type sdf
select table2
copy to temp2.txt type sdf
llSame = (FileToStr('temp1.txt') == FileToStr('temp2.txt'))
erase temp1.txt
erase temp2.txt

OR
when tables are not in use and could be used exclusive (no memo/gen):
llSame = (FileToStr('table1.dbf') == FileToStr('table2.dbf'))

OR
when tables are not in use and could be used exclusive (memo/gen):
llSame = (FileToStr('table1.dbf') == FileToStr('table2.dbf')) AND ;
(FileToStr('table1.fpt') == FileToStr('table2.fpt'))

might be the fastest ways. However last 2 ways would even need the deleted records be same and these approaches also need physical order of entries be the same.

What's meant by structures be 'exactly' same. That's not only field count and types but also the 'field order'. No matter how you define your structure database has an impact on fields' ordinal. That's in your header looking with an hex editor you might see f1,f2,f3 but in database order might be f1,f3,f2 and copy to obeys it. Though this is a rare case for hacking tables (you don't hack tables who does < g >) keep in mind it exists.

General field existence is worst case. Assuming you have no general fields (if general exists I don't know of a simple way) :
Function AreTablesSame
Lparameters tcTable1, tcTable2, tcPKExpr, tcPKTag
Local lcAlias, lcOldDeleted, llMisMatch
lcAlias = Alias()
lcOldDeleted = Set('deleted')
* We don't want to chek if deleted match too
Set Deleted On

Use (tcTable1) In 0 Again Alias '_cmp1'
Use (tcTable2) In 0 Again Alias '_cmp2'

Local Array arrCnt1[1],arCnt2[1]
Select Cnt(*) From _cmp1 Into Array arrCnt1
Select Cnt(*) From _cmp2 Into Array arrCnt2
If arrCnt1 # arrCnt2
  llMisMatch = .T. && Reccount mismatch
Else
  Local loRecord1, loRecord2, llUseSeek, llUseLocate, luValue

  llUseSeek   = (!Empty(tcPKTag) And !Empty(tcPKExpr))
  llUseLocate = (!llUseSeek And !Empty(tcPKExpr))

  Select _cmp1
  Scan
    * If not both indexed use indexed one as table2
    * If none indexed but has unique field(s) use locate
    * If no unique field(s) then use recno instead
    Do Case
      Case llUseSeek
        If !Seek(&tcPKExpr, '_cmp2', tcPKTag)
          llMisMatch = .T.
          Exit
        Endif
      Case llUseLocate
        luValue = &tcPKExpr
        Select _cmp2
        Locate For &tcPKExpr = luValue
        If Eof()
          llMisMatch = .T.
          Exit
        Endif
      Otherwise
        Go Recno('_cmp1') In '_cmp2'
    Endcase
    Select _cmp2
    Scatter Name loRecord2 Memo
    Select _cmp1
    Scatter Name loRecord1 Memo
    If !Compobj(loRecord1, loRecord2)
      llMisMatch = .T.
      Exit
    Endif
  Endscan
Endif

Use In '_cmp1'
Use In '_cmp2'
If !Empty(lcAlias)
  Select (lcAlias)
Endif
Set Deleted &lcOldDeleted
Return !llMisMatch
Cetin
Ç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
Reply
Map
View

Click here to load this message in the networking platform