Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2003
    Posts
    19

    Unanswered: Insert composite PKeys

    I need to select a row by the composite primary key. Then I need to insert a new row to the same table, but using different primary key. Is it possible to achieve this in one sql statement?

    I am trying something of the like:

  2. #2
    Join Date
    Sep 2003
    Posts
    522
    something of the like...what?

    insert your_table select cast(cast(cast(pkey as binary) as varchar(8000)) as char(36))...

    also look into guid (uniqueidentifier), you may figure out your answer yourself.

  3. #3
    Join Date
    Nov 2003
    Posts
    19
    Sorry forgot to paste.

    Something like:

    INSERT INTO myTable
    (Field1, Field2, Field3, Field4, Field5, Field6)
    VALUES
    newPK1,
    newPK2,
    newPK3,
    select Field4 From myTable where Field1 = 'xxx' and Field2 = 'xxx' and Field3 = 'xxx',
    select Field5 From myTable where Field1 = 'xxx' and Field2 = 'xxx' and Field3 = 'xxx',
    select Field6 From myTable where Field1 = 'xxx' and Field2 = 'xxx' and Field3 = 'xxx'


    Basically I want to get Field4, Field5, Field6 and insert into myTable using new Composite Primary Key.

  4. #4
    Join Date
    Nov 2003
    Posts
    19
    NVM, I got it.

    INSERT INTO myTable
    (Field1, Field2, Field3, Field4, Field5, Field6)
    VALUES
    SELECT 'xxx', 'yyy', 'zzz', Field4, Field5, Field6 Where
    Field1 = 'xxx' AND
    Field2 = 'yyy' AND
    Field3 = 'zzz'

  5. #5
    Join Date
    Nov 2003
    Posts
    19
    Mistake in my previous post. This is what I wanted.



    INSERT INTO myTable
    (Field1, Field2, Field3, Field4, Field5, Field6)
    VALUES
    SELECT 'NEWxxx', 'NEWyyy', 'NEWzzz', Field4, Field5, Field6 Where
    Field1 = 'xxx' AND
    Field2 = 'yyy' AND
    Field3 = 'zzz'

Posting Permissions

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