Insert a new record with existing record data into same table except single field

Answered Insert a new record with existing record data into same table except single field

  • Thursday, September 20, 2012 5:59 PM
     
     

    HI,

    I have a table with an identity column and datetime column and some varchar columns say,

    ID (identity), StartDate (DATETIME), firstname (VARCHAR(20)), lastname (VARCHAR(20)), and so on...

    I have a stored procedure with all the input parameters (even ID). When i send the ID(identity) as NULL then i'm inserting a new record with all input params,

    but what I need is, when I send ID( identity) value to stored procedure other than null (say, 10 which is already present in table),

    then I need to insert a new record into the same table with all the values present for ID=10(new identity shud be generated) except the DATE( i should insert the StartDate which comes as input parameter).

    Thanks & Regards,

    Anand

All Replies

  • Thursday, September 20, 2012 6:03 PM
    Moderator
     
      Has Code
    Your are not doing insert but UPDATE.
    The syntax is:
    UPDATE yourtable
    SET StartDate=@StartDate
    WHERE ID=@ID

  • Thursday, September 20, 2012 6:06 PM
     
     
    I need to insert a new record with all existing data except date, so that it creates a new identity so that old identity remains as history.

    Thanks & Regards, Anand

  • Thursday, September 20, 2012 6:12 PM
    Moderator
     
     Answered

    Try

    insert into MyTable (StartDate, FirstName, LastName, ...)

    SELECT @StartDate, FirstName, LastName, ..

    from MyTable where ID = @Id


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Marked As Answer by Anand Reddy Thursday, September 20, 2012 7:15 PM
    •