I have two result sets coming from two different database and I need to compare it. I want an operation like
A-B
to be performed on them.
I cannot perform row by row comparison as 1st row in A resultset can be present anywhere in B resultset.
Below is the code to do that in .NET, which is very easy and perfect .
var nonIntersecting = dtSource.AsEnumerable().Except
(
dtTarget.AsEnumerable(), DataRowComparer.Default
);
try
{
dtSrcToTgtResult = nonIntersecting.CopyToDataTable();
} catch (InvalidOperationException ex) {}
Here dtSource,dtTarget are datatables having source and target data from databases. dtSrcToTgtResult contains data present in source but not in target, which is exactly what I want.
Can same be done in JavaScript with result sets. I can also check CachedRowSet
or webRowSet
if something like this is available in it.
EDIT
For people who are giving minus votes. this is what i already did, but its not solving the problem.
private Boolean compare(ResultSet rsSrc,ResultSet rsTgt,String ExecCondition)
{
Boolean status = true;
try
{
ResultSetMetaData metaSrc = rsSrc.getMetaData();
ResultSetMetaData metaTgt = rsTgt.getMetaData();
final int columnCountSrc = metaSrc.getColumnCount();
List<DBRow> dList = new ArrayList<DBRow>();
List<DBRow> DataInSourceNotInTarget = new ArrayList<DBRow>();
List<DBRow> DataInTargetNotInSource = new ArrayList<DBRow>();
DBRow d = new DBRow();
DBRow d1 = new DBRow();
for (int column = 1; column <= columnCountSrc; column++)
{
d.Row.add(metaSrc.getColumnName(column));
d1.Row.add(metaTgt.getColumnName(column));
}
DataInSourceNotInTarget.add(d);
DataInTargetNotInSource.add(d1);
if(ExecCondition.equals("Source To Target"))
{
while(rsSrc.next())
{
if(rsTgt.next())
{
for (int column = 1; column <= columnCountSrc; column++)
{
Object valueSrc = rsSrc.getObject(column);
Object valueTgt = rsTgt.getObject(column);
if(!valueSrc.toString().equals(valueTgt.toString()))
{
status=false;
System.out.println("ValueSRC: "+v alueSrc.toString());
System.out.println("ValueTgt: "+valueTgt.toString());
}
}
}
else
{
// if target rows ends
DBRow dr = new DBRow();
for (int column = 1; column <= columnCountSrc; column++)
{
Object valueSrc = rsSrc.getObject(column);
dr.Row.add(valueSrc);
}
DataInSourceNotInTarget.add(dr);
}
}
}//exec condition if
if(ExecCondition.equals("Target To Source"))
{
while(rsTgt.next())
{
if(rsSrc.next())
{
for (int column = 1; column <= columnCountSrc; column++)
{
Object valueSrc = rsSrc.getObject(column);
Object valueTgt = rsTgt.getObject(column);
if(!valueSrc.toString().equals(valueTgt.toString()))
{
status=false;
System.out.println("ValueSRC: "+valueSrc.toString());
System.out.println("ValueTgt: "+valueTgt.toString());
}
}
}
else
{
// if Source rows ends
DBRow dr = new DBRow();
for (int column = 1; column <= columnCountSrc; column++)
{
Object valueTgt = rsTgt.getObject(column);
dr.Row.add(valueTgt);
}
DataInTargetNotInSource.add(dr);
}
}
for(DBRow obj:DataInTargetNotInSource)
{
obj.print();
}
}//exec condition if
}
catch(Exception e)
{
e.printStackTrace();
}
return status;
}
See Question&Answers more detail:os