Results 1 to 12 of 12

Thread: [RESOLVED] Update help

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Resolved [RESOLVED] Update help

    Hi.
    I need some help with an update.
    I have this:
    Server1
    CODE FORMAT
    HO00105285 0000000002
    HO00105285 VS00000001

    Server2
    CODE FORMAT
    HO00105285 0000000002
    HO00105285 VS00000001

    I need to bypass those on an update and only do an update if the values have been changed on FORMAT

    Doing this:

    Code:
    	UPDATE Server2 
    	SET Format = HO.Format
    	FROM Server1  as HO inner join  Server1  FF on FF.CODE COLLATE Greek_CI_AS = HO.CODE and FF.Format COLLATE Greek_CI_AS <> HO.Format
    This will always update the above 2 lines even if they are equal on both servers on CODE's that are 2 liners or more.
    How would I go about and ignore them and only update if the Format is different ?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Update help

    Or at worse if not possible, to delete values that exist on Server1 (CODE + FORMAT) and not on Server1.
    Something like this (does not work):
    Code:
    --delete from ---
    	select CODE,Format  FROM Server1 FF WHERE  NOT EXISTS 
    	(SELECT  HO.CODE,HO.Format
    	 FROM Server2 HO Where FF.CODE  COLLATE Greek_CI_AS = HO.CODE AND FF.Format COLLATE Greek_CI_AS = FF.Format)
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Update help

    that exist on Server1 (CODE + FORMAT) and not on Server1.
    ??? Huh?

    EDIT could you give us sample data for when you want to update and when you want to skip?
    i do have an idea how to solve this, but your sample data doesn’t reflect your problem.
    a DELETE/UPDATE on inequality is usually done with a LEFT JOIN and a IS NULL check in the WHERE-clause
    but we would need to know what tocheck for (what is equal and what is not)
    Last edited by Zvoni; Apr 27th, 2023 at 10:30 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Update help

    I guess I wanted to write exist on server 1 and not server 2.
    What I can think of i as partition by , but I'll too exhausted now to think it over.

    Instead of values I think this would help more
    Server1
    X , 1
    X, 2

    Server 2
    X,1
    X,1
    OR
    X,1
    X,3

    Who to turn Server 2 to
    X,1
    X,2

    X,3 for example I think it needs to be deleted not updated because how about if I have
    X,1
    X,2
    X,3
    X,4
    I won't have a clue what to update to X,2 .
    But OK
    Supposedly just
    X,1
    X,1 and need to change X,1 to X,2 again it's cherry picking.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Update help

    This not going to work, at least not the way you want.
    do you have any other field between the two tables, which are unique but should be the same in both?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Update help

    I'm thinking to strict the field only on to one or delete and insert.
    If I can pass by the general specs an one to one then it would be fine.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Update help

    Then columns code and format should be together (!) unique, so there is only one possible combination of both.
    then it would be easy
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Update help

    In any day scenario yes but due to multiple requirements I'm not sure if I need to "let it pass" and run a query search later on or make them unique. But that is something I need to discuss with another department.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Update help

    I did some tests with the structure you provided, and i have to amend my statement:
    It's not going to be easy, if you don't have a single unique per Row identifier which is the same in both tables.

    For the structure as provided, the only thing coming to mind to just display your "Problem" would be a FULL OUTER JOIN.
    Nevermind doing a DELETE or UPDATE on it

    EDIT: Thinking about it a bit more.....
    What i could gather from your description of the "problem":
    1) You have a table "Server1" which is "clean", "correct" --> meaning it's kind of a "master"
    2) You have a table "Server2", which for whatever reason gets entries from sources unknown,
    which result in duplicate entries (your "x, 1" - "x, 1" above) and/or in entries that don't appear in "Server1" (your "x, 1" - "x, 3")
    3) You want to clean up "Server2" and bring it to the same "state" as "Server1"

    That correct?
    Last edited by Zvoni; Apr 28th, 2023 at 04:08 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  10. #10

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Update help

    Hey. After a meeting we are going to do it one liner so we don't get caught into this kind of trouble.
    If you want for the philosophical heck of it to look it up then , yes what you wrote is correct , I'm updating any differences found (and here I had the X lines issues) and Inserting any new lines (that I don't have an issue there).
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Update help

    Quote Originally Posted by sapator View Post
    Hey. After a meeting we are going to do it one liner so we don't get caught into this kind of trouble.
    If you want for the philosophical heck of it to look it up then , yes what you wrote is correct , I'm updating any differences found (and here I had the X lines issues) and Inserting any new lines (that I don't have an issue there).
    OK, what i thought.
    Right.
    As follows:
    1) De-duplicate "server2" --> Should be easy enough (combined unique or a SELECT with ROW_NUMBER Partitioned by Code and Format and then Deleting everything with a rownumber >1)
    2) IIRC, you have MS SQL_Server: Use the MERGE-Statement with Server1 being Source, and Server2 being Target. That should Insert any missing records in S2, and delete any records in S2 which are not in S1
    https://www.sqlservertutorial.net/sq...-server-merge/

    Beware:
    Since there is a DELETE involved, make sure there are no child-records in other tables depending on S2 (ON DELETE CASCADE!)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  12. #12

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Update help

    Thanks , turned out it was not THAT much double records so I did it by hand and also put a constrain (well 2Col PK to be exact) so only one record with one format can pass through.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width