Insert combo in table

  • Hello all,

    I have an insert statement where I need to pull data from two tables and I want to insert values. Here is the code that I have so far. I am very new to sql and sql server 2005.

    INSERT INTO dbo.risk_Violations(ID, ViolationCode, AccountNo, CellID, ContractLimit, PositionTotal, EODFeedDate, ViolationDate, NotificationFlag)

    SELECT (@acctNum + 'CL'), 'CL', @acctNum, @manager, @contractLmt, t.totContracts, n.EODFeedDate, getdate(), 'Y'

    FROM dbo.new_Positions n

    INNER JOIN @TmpTrader_Tot t

    ON n.CellID = @manager

    Hopefully you can see what I am trying to do. Is there a better way to do this? When I run the sp I get this error message:

    Violation of PRIMARY KEY constraint 'PK_risk_Violations'. Cannot insert duplicate key in object 'dbo.risk_Violations'.

    The statement has been terminated.

    But the table is empty so I don't understand why it says that it is a duplicate key?

    Thank you for all your help.

  • Remoive the insert statement from your code so you can see the results of your query. You will see that you are getting multiple records returned by your select that contain duplicate key values. If all the data is the same you can use either select distinct or a group by to eliminate the error. If you are getting different results with the same ID then you have a data issue or design issue.

Viewing 2 posts - 1 through 1 (of 1 total)

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