Duplicating a Group of Records while changing only one field

  • I need to create a SP to duplicated a schedule of items for a given QuoteID.  When i duplicate the items though, i need to update the QuoteID to a new value.  For example, quote 1 might have 3 entries in its schedule which i need to duplicate whilst changing the quoteid of the records that i'm dupicating to something else.  I'll try to explain better with example values below:

    QuoteID     Sector      Country

    1                 1             England

    1                 2             France

    1                 3             Germany

    When the duplicate button is hit the above table should look like below:

    QuoteID     Sector      Country

    1                 1             England

    1                 2             France

    1                 3             Germany

    2                 1             England

    2                 2             France

    2                 3             Germany

     

    The new quoteid can be a parameter in the stored proc as it may not be just 1 bigger than the previous quote.Is this possible? 

    Many thanks,

    Chris Evans

  • create proc dbo.DuplicateStuff @NextQuoteID as int, @CopyQuoteID as int

    As

    SET NOCOUNT ON

    Insert into dbo.MyTable (QuoteId, Sector, Country) Select @NextQuoteID, Sector, Country from dbo.MyTable where QuoteId = @CopyQuoteID

    SET NOCOUNT OFF

  • CREATE PROC dupQuote (@oldQuoteID int

     , @NewQuoteID int)

    AS

    INSERT INTO test (QuoteID, Sector, Country)

    SELECT @NewQuoteID, Sector, Country

    FROM test

    WHERE QuoteID = @OldQuoteID

     

    /*****************

    If most people are not willing to see the difficulty, this is mainly because, consciously or unconsciously, they assume that it will be they who will settle these questions for the others, and because they are convinced of their own capacity to do this. -Friedrich August von Hayek

    *****************/

Viewing 3 posts - 1 through 3 (of 3 total)

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