Usinga where to compare a value in the src and dst tables.

Automate and integrate using the SQL comparison API

Moderator: Chris Auckland

Usinga where to compare a value in the src and dst tables.

Postby MarkLFT » Fri Apr 22, 2011 4:13 am

I am trying to sync to databases, they have identical structures, and every table has a createdOn, updatedOn, and deletedOn field.

What I would like to do is use a where clause to only update data where the src.UpdatedOn > dst.UpdatedOn.

All of the examples I can find for where clauses only seem to apply to one table, i.e. id > 3 etc.

Is there a way to filter based on a comparison of fields in both the source and destination tables?
MarkLFT
 
Posts: 10
Joined: Fri Apr 22, 2011 3:59 am
Location: Bali, Indonesia

Postby peter.peart » Mon Apr 25, 2011 2:41 pm

Hi there,

Thanks for your post. You should be able to apply a WHERE clause to both tables, and an example is viewable on the below page:

http://sdk.red-gate.com/index.php/WhereExample

HTH!

Pete
Peter Peart
Red Gate Software Ltd
+44 (0)870 160 0037 ext. 8569
1 866 RED GATE ext. 8569
peter.peart
Site Admin
 
Posts: 362
Joined: Tue Sep 02, 2008 9:58 am
Location: Top floor, RG towers with the cool kids

Postby MarkLFT » Thu May 05, 2011 3:49 am

Hi Peter,

Thanks for the suggestion, however I think you may have misunderstood my question. What I am looking to do is compare values within the two tables, i.e. both tables have a UpdatedOn field. I only want to update a tabe based on which which tables UpdatedOn field is the newest?

I run a sync job every minute or so, and normally the data is only changed on one place at a time. so when I run the push part i.e. DbA -> DbB, I only want to update records in the table where DbA.TableA.UpdatedOn is greater than DbB.TableA.UpdatedOn. Then a minute later it runs a Pull job i.e. DbA <- DbB, and this timne I only want to update the table in DbA is the UpdatedOn in DbB is greater than DbA.

Bother systems use a common time source, so there should not be any time differences except where data has been updated.

Many thanks

Mark
MarkLFT
 
Posts: 10
Joined: Fri Apr 22, 2011 3:59 am
Location: Bali, Indonesia

Postby Brian Donahue » Thu May 05, 2011 10:22 am

Hello,

You can use another table as the basis for a WHERE clause but you must be really careful and test the results as it may have unintended consequences.

In the "worked example" databases WidgetDev and WidgetLive, you can use a distributed query to use WidgetLive's Contacts table as a WHERE clause for the WidgetDev's contact table:
Code: Select all
Where clause for WidgetDev:
JoiningDate > (select top 1 JoiningDate from WidgetLive.dbo.Contacts where JoiningDate > '2007-09-14 16:09:22')


This will filter all contacts from WidgetDev that have joined later than the first person who joined after 16:09:22 in WidgetLive.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby MarkLFT » Thu May 05, 2011 10:37 am

Hi, it may be me not fully understanding your solution, but I think this will not work.

What I am looking for is each row is compared on a one to one basis. i.e. If the row exists in Dev but not Live we copy row from Dev to Live, if the row exists in Live but not Dev, we delete it from Live.

If exists in both (same key field) , the the UpdatedOn in matching rows is compared, and if the row in Dev is newer than the row in Live, the row is copied, however if both UpdatedOne are the same or the row in Live has a newer UpdatedOn than the corresponding row in Dev, the row is not copied.

This evaluation must be done on a row by row basis, not just compared to a set value.

Thanks
MarkLFT
 
Posts: 10
Joined: Fri Apr 22, 2011 3:59 am
Location: Bali, Indonesia

Postby Brian Donahue » Thu May 05, 2011 11:15 am

Using the SDK and writing a C# program may be your only solution, then. When the tables are compared, you can use a SelectionDelegate to filter out the updates you don't want to run.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby MarkLFT » Thu May 05, 2011 11:18 am

I am using the SDK, but I still cannot find a way to do this.

Thanks
MarkLFT
 
Posts: 10
Joined: Fri Apr 22, 2011 3:59 am
Location: Bali, Indonesia

Postby Brian Donahue » Thu May 05, 2011 2:34 pm

You want to have a look at the selectiondelegateexample. Or perhaps the filtering overview.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am


Return to SQL Comparison SDK Previous Versions

Who is online

Users browsing this forum: No registered users and 0 guests