Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert combo in table


Insert combo in table

Author
Message
ltmnm
ltmnm
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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.
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12200 Visits: 14861
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search