Help Needed

  • Hi,

    we are having one table (AA) with 3 columns

    Id,Polcinumber,date

    1,s1234,04/09/2012

    2,s2345,05/06/2012

    we are having another table(BB) having 2 cloumns

    Id,Policynumber,date

    1,s71234, currentdate

    we are inserting the records from BB to AA table.in AA table the values of Polcynumbers are already existing

    i want write the query to replace the oldpolicynumbers

    can any one help on this

  • assuming the ID is the join criteria between the two tables, this will do it:

    UPDATE AA

    SET AA.Polcinumber = BB.Policynumber

    FROM BB

    WHERE AA.ID = BB.ID

    AND AA.Polcinumber <> BB.Policynumber

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowel,

    how to insert the currentdate into AA table with new policy number

  • Div Goud (9/7/2012)


    Thanks Lowel,

    how to insert the currentdate into AA table with new policy number

    just expand the number of columns being updated;

    ...

    SET AA.Polcinumber = BB.Policynumber,

    Column2 = GetDate(), --a function fromt eh server

    Column3 = 'Approved', --a static value

    Column4 = BB.SomeOtherColumn -- a value in the other table

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • DELETE aa

    FROM aa

    INNER JOIN bb

    ON bb.ID = aa.ID

    SET IDENTITY_INSERT aa ON

    INSERT INTO aa (Id,Policynumber,date)

    SELECT Id,Policynumber,date

    FROM bb

    SET IDENTITY_INSERT aa OFF

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply