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


Insert select statement into a table with multiple constraints


Insert select statement into a table with multiple constraints

Author
Message
tmummert
tmummert
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 142
I'm trying to insert rows into a table that has multiple constraints. My statement is failing because SQL is looking at the first constraint field and sees that the value already exists, even though the second constraint is different. An example is VCHRNMBR='xyz' is already in the table with DOCTYPE=1 and I need to add a record to the table for VCHRNMBR='xyz' with DOCTYPE=6. Is there a way to accomplish this without modifying the constraints on the table prior to the insert?

Here's the code:
insert into PM30200
select
a.VCHRNMBR
,a.DOCTYPE
from PM30300 a
where a.VCHRNMBR not in (select VCHRNMBR from PM30200 where DOCTYPE=6) and DOCTYPE=6


All help is appreciated! Thanks!
Trish
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
tmummert (6/27/2013)
I'm trying to insert rows into a table that has multiple constraints. My statement is failing because SQL is looking at the first constraint field and sees that the value already exists, even though the second constraint is different. An example is VCHRNMBR='xyz' is already in the table with DOCTYPE=1 and I need to add a record to the table for VCHRNMBR='xyz' with DOCTYPE=6. Is there a way to accomplish this without modifying the constraints on the table prior to the insert?

Here's the code:
insert into PM30200
select
a.VCHRNMBR
,a.DOCTYPE
from PM30300 a
where a.VCHRNMBR not in (select VCHRNMBR from PM30200 where DOCTYPE=6) and DOCTYPE=6


All help is appreciated! Thanks!
Trish


Is the first constraint stating some column must be unique? It doesn't matter what any of the other data is. The whole point of constraints is for data validation and whatever you are trying to insert violates those rules.

Can you post the ddl for the table including the constraints and the data you are trying to insert?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10318 Visits: 9595
tmummert (6/27/2013)
I'm trying to insert rows into a table that has multiple constraints. My statement is failing because SQL is looking at the first constraint field and sees that the value already exists, even though the second constraint is different. An example is VCHRNMBR='xyz' is already in the table with DOCTYPE=1 and I need to add a record to the table for VCHRNMBR='xyz' with DOCTYPE=6. Is there a way to accomplish this without modifying the constraints on the table prior to the insert?


If you have a unique constraint on a single field, uniqueness is enforced on that field alone. If you want the combination of two fields to be unique, you need to create a unique constraint on the combination of the two fields. For example:

alter table PM30200 add constraint PM30200_UQ unique (VCHRNMBR, DOCTYPE);




Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
tmummert
tmummert
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 142
Thanks for the help. That's what I needed!

Much appreciation,
Trish
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10318 Visits: 9595
No problem. Glad I could help.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
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