I need to duplicate the rows in same table , simply changing one field value

  • i need to insert into a table that has this scenario:

    insert into table1 (col1, col2, col3, col4)

    select (1,2,3,4) from table1 A join table2 B on

    A.col1 = B.col2

    **this works , but the second insert doesnt work

    insert into table1 (col1,col2,col3,col4)

    select (1,5,3,4) from table1 A join table2 B on

    A.col1 = B.col2

    where B.col2 = '5' and B.col2 is null

    **this is not working

  • where B.col2 = '5' and B.col2 is null

    -------------------------------------------

    B.col2 can't be both '5' and NULL at the same time.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • when i try and run it with only

    where B.col2 = '5'

    the error is : Violation of PRIMARY KEY constraint

    its prob looking at the column its already inserted with the same values?

  • Yes. Primary Key constraints force columns to be unique. For example, you usually have an employee ID number as a primary key for an employees table to make sure you don't accidentally give two of your employees the same id and confuse their information.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Please refer to Books On Line (BOL) ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/edc46c29-51e0-4407-94a3-e68fd658ec3c.htm which states:

    A table can have only one PRIMARY KEY constraint, and a column that participates in the PRIMARY KEY constraint cannot accept null values. Because PRIMARY KEY constraints guarantee unique data, they are frequently defined on an identity column.

    When you specify a PRIMARY KEY constraint for a table, the SQL Server 2005 Database Engine enforces data uniqueness by creating a unique index for the primary key columns.

    It would also be to your benefit to read the article in my signature block and prepare your question as the article describes so those here can give you adequate help.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Is this the same primary key violation you've already posted like 3 other times that we keep telling you you can't do?

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

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