Results 1 to 2 of 2

Thread: insert into table, select from same table (same info, but key is different)

  1. #1
    Join Date
    May 2009
    Posts
    1

    insert into table, select from same table (same info, but key is different)

    Hi,

    I have two dependant tables, EMPtbl and BenfTbl. Both Emptbl and BenfTbl are joined by username.

    Emptbl contains below fields:
    UserName (PK)
    Enrolled program

    BenfTbl fields are:
    UserName (FK to Emptbl.username)
    BenifitType
    Cost
    .
    .

    I need to insert a few new user names after some existing users( Their new user names are different, but all their benifit info remains the same). For example, I have john with all his benift data, I need to insert john1 as new username but have all john benifit info). How would you do that?

    Sam

  2. #2
    Join Date
    Apr 2009
    Posts
    86
    Since I am not sure what database or version you are using, I can't be sure, but this should work if your database allows it:

    Insert into Emptbl
    select 'john1', EnrolledProgram
    From Emptbl
    Where UserName = 'john'
    ;
    Insert into BenfTbl
    Select 'john1, BenifitType, Cost, etc.
    From BenfTbl
    Where UserName = 'john'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •