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

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?

Comments

  • 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
  • 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
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    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:
    Where clause for WidgetDev:
    JoiningDate &gt; &#40;select top 1 JoiningDate from WidgetLive.dbo.Contacts where JoiningDate &gt; '2007-09-14 16:09:22'&#41;
    

    This will filter all contacts from WidgetDev that have joined later than the first person who joined after 16:09:22 in WidgetLive.
  • 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
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    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.
  • I am using the SDK, but I still cannot find a way to do this.

    Thanks
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    You want to have a look at the selectiondelegateexample. Or perhaps the filtering overview.
Sign In or Register to comment.