Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compare two identical tables?
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00506435
Message ID:
00506493
Views:
14
Steve --

I'd approach that in this way.

Assume you have tables A1 and A2, with PK named PK, and other fields O1, O2.

There are 3 possibilities for change.

1. The 2 tables each have a record with the same PK, but different values in one or more of the fields.
2. The first table has records the second table doesn't have.
3. The second table has records the first table doesn't have.

The following program uses a SQL SELECT to test for each of these conditions and create a table Difs which contains the primary key of all differences as well as an indication of what the difference is.

Then, the other tables are linked back to Difs, which is SCANned, and a log file is generated. For the 2nd and 3rd conditions, nothing special has to be done. But, for the 1st, a listing of each changed field is made.

It's possible that your specific application will require you to use the native VFP commands -- if the syntax is too convoluted for SQL. In this case, the logic is the same.

Table setup is pretty straightforward -- which you can infer from the code. Do create an index PK on the field PK for each of the tables.

Enjoy!

P.S. -- this works, but there's no error trapping. YMMV.
#DEFINE		EOL		CHR(13)

lcDifferencFile = "D:\Test\TableDifs\TableDifs.txt"
IF NOT USED ("T1")
	USE T1 IN 0
ENDIF
IF NOT USED ("T2")
	USE T2 IN 0
ENDIF

SELECT T1.PK AS KeyValue, "Changed  " AS RecType;
	FROM T1 ;
	INNER JOIN T2 ON T1.PK = T2.PK  ;
	WHERE T1.O1 <> T2.O1 OR T1.O2 <> T2.O2 ;
UNION ;
	SELECT T1.PK, "Not in T1" AS RecType ;
	FROM T1 ;
		WHERE T1.PK NOT IN (SELECT PK FROM T2) ;
UNION ;
	SELECT T2.PK, "Not in T2" AS RecType ;
	FROM T2 ;
		WHERE T2.PK NOT IN (SELECT PK FROM T1) ;
	ORDER BY 2 ;
	INTO TABLE Difs

SET ORDER TO PK IN T1
SET ORDER TO PK IN T2

SELECT Difs
INDEX ON KeyValue TAG PK of CDX
SET RELATION TO KeyValue INTO T1
SET RELATION TO KeyValue INTO T2

lcDifs = ""
SELECT Difs
SCAN
	DO CASE
		CASE 	RecType = "Not in T1"
			lcDifs = lcDifs + "Primary Key = " + KeyValue + " is not in T1." + EOL
		CASE	RecType = "Not in T2"
			lcDifs = lcDifs + "Primary Key = " + KeyValue + " is not in T2." + EOL
		CASE	RecType = "Changed  "
			lcDifs = lcDifs + "Primary Key = " + KeyValue + " has been changed: " ;
				+ IIF (T1.O1 <> T2.O1, "O2 has been changed from " ;
					+ TRANSFORM (T1.O1);
					+ " TO ";
					+ TRANSFORM (T2.O1), "") ;
				+ IIF (T1.O2 <> T2.O2, "O2 has been changed from " ;
					+ TRANSFORM (T1.O2);
					+ " TO " ;
					+ TRANSFORM (T2.O2), "")  + EOL
	ENDCASE
ENDSCAN

STRTOFILE(lcDifs, lcDifferencFile)

SET RELATION OFF INTO T1
SET RELATION OFF INTO T2	

SELECT T1
SET ORDER TO
SELECT T2
SET ORDER TO
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform