Re: Performance Question Stored Procedure

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 12/14/04

  • Next message: simon: "Distributed transaction"
    Date: Tue, 14 Dec 2004 10:21:19 +0100
    
    

    On Tue, 14 Dec 2004 14:13:00 +1000, Paul Say wrote:

    >I am not sure exactly what you mean, do you have a simple example.

    Hi Paul,

    Assuming that your tables are like this:

    CREATE TABLE TableA (KeyCol int NOT NULL PRIMARY KEY,
                         OtherCol char(7) NOT NULL,
                         LastCol datetime)
    CREATE TABLE TableB (KeyCol int NOT NULL PRIMARY KEY,
                         OtherCol char(7) NOT NULL,
                         LastCol datetime)

    and that you have to update TableA with rows from TableB, you should use:

    -- First, update rows already in TableA
    UPDATE a
    SET a.OtherCol = b.OtherCol,
                a.LastCol = b.LastCol
    FROM TableB AS b
    INNER JOIN TableA AS a
          ON a.KeyCol = b.KeyCol
    -- Optionally:
    WHERE a.OtherCol <> b.OtherCol
    AND ( NULLIF (a.LastCol, b.LastCol) IS NOT NULL
          OR NULLIF (b.LastCol, a.LastCol) IS NOT NULL)

    -- Next, insert rows that were not already in TableA
    INSERT INTO TableA (KeyCol, OtherCol, LastCol)
    SELECT b.KeyCol, b.OtherCol, b.LastCol
    FROM TableB AS b
    LEFT JOIN TableA AS a
         ON a.KeyCol = b.KeyCol
    WHERE a.KeyCol IS NULL

    NOTES:
    * Both queries are untested, so they might have typos
    * The UPDATE syntax I used is proprietary T-SQL format. To do the same in
    ANSI-compliant SQL, you need a longer statement with the same subquery
    repeated several times; unfortunately, these are not optimized well by SQL
    Server. Only use this UPDATE syntax if you are positive that each row in
    the updated table matches exactly one row in the other table(s) used in
    the query, unless you like undefined behaviour.
    * The INSERT statement could also be rewritten to use a NOT EXISTS clause.
    The choice between NOT EXISTS or LEFT JOIN is purely by personal
    preference.

    Best, Hugo

    -- 
    (Remove _NO_ and _SPAM_ to get my e-mail address)
    

  • Next message: simon: "Distributed transaction"

    Relevant Pages

    • Re: How to trim a record when using DB_OPEN_TABLE
      ... and TableB contains some items that need to be updated in TableA. ... EmplID ... QtrDate (primary key) ...
      (microsoft.public.access.modulesdaovba)
    • Re: sql minus
      ... What do you do in the case of a composite primary key? ... FROM TableA LEFT JOIN TableB ... SELECT * FROM TableA MINUS SELECT * FROM TableB; ...
      (microsoft.public.access.queries)
    • Re: Subselect Query Problem
      ... tableA LEFT JOIN tableB ... the result (of the inner join), it is logically re-introduced into the ... result, but since nothing match in tableB, whatever comes from tableB is ... > is Table B that has the first three fields as the primary key, ...
      (microsoft.public.access.queries)
    • Re: How to trim a record when using DB_OPEN_TABLE
      ... QtrDate (primary key) ... Right now, I am taking the first record in TableB, searching TableA for a ... there are extra spaces at the end of some data in the TableA.EmplID. ...
      (microsoft.public.access.modulesdaovba)
    • Re: Which column is taken in JOIN with multiple columns and different content ?
      ... This column ist not he primary key. ... FROM tablea a, tableb b ... Which values will be for the questions marks? ...
      (comp.databases.oracle.misc)