Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    1

    Unanswered: INSERT INTO - SELECT with pk in table

    Is it possible to use the INSERT INTO - SELECT syntax with a pk in the target table that has no auto increment?

    The tables structure is:

    CREATE TABLE objectlang (
    objectlangID integer NOT NULL, <== PK
    objectID integer NOT NULL,
    langID integer NOT NULL,
    somedata integer NOT NULL,
    [[...more columns here...]]
    )

    The statement should look like:

    INSERT INTO objectlang
    SELECT [[??set pk here??]], o.objectID, l.langID, 1, [[...more data here...]]
    FROM object o, lang l
    WHERE [[...conditions...]]

    I thought of something like reading the current max value of objectlangID and adding a value,
    but no idea where this value should come from.

    I need this for more than one project in mssqlserver 2000/2005, postgresql and oracle.

    tia

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Define a temporary table with an IDENTITY value. Insert your records into it, and then copy them to your production table.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    ...or, in ORACLE you could use the ROWNUM value. In SQLSVR 2005 there are similar row number values available.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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