Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Insert combo in table Expand / Collapse
Author
Message
Posted Friday, January 11, 2008 9:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 7, 2009 11:13 AM
Points: 3, Visits: 61
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.
Post #441871
Posted Friday, January 11, 2008 9:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:43 PM
Points: 11,321, Visits: 13,112
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.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #441880
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse