Results 1 to 4 of 4

Thread: Clone a recordset but change the key ?!

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Question Clone a recordset but change the key ?!

    I have 17 records in a table, with a key of "10". I want to clone these, but have the key as "12", but keep all other field info the same.

    Using SQL, how would I do this ?

    insert into TableA (Field A,Field B etc)
    values (select * from TableA where key = 10)

    ... wouldn't work as theres no way of saying "but make the keys 12 and not 10.

    I guess this is probably a simple question, but I can't find an answer.

    Can anyone help ?

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Clone a recordset but change the key ?!

    All I can think of is passing though a temp table. Insert there update 10 to 12 then select insert into the other table. Drop the temp.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Clone a recordset but change the key ?!

    You could hard-code the values in the select statement, eg:
    Code:
    values (select 12, FieldB etc from TableA where key = 10)

  4. #4
    Hyperactive Member Foxer's Avatar
    Join Date
    Oct 2001
    Location
    Australia
    Posts
    278

    Re: Clone a recordset but change the key ?!

    Ya - what Si said - works fine for me

    insert into TableA (12 ,Field B)
    values (select * from TableA where key = 10)


    If you wanted to get really really yucky about it you could insert into a temporary table, run an update statement to change the 10 to a 12, and then move the rows back to TableA but why bother. (Whoops - this is exactly as GM described).
    Rate my response if I helped

    Go Hard Or Go Home


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