June 27, 2013 at 10:25 am
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
June 27, 2013 at 10:33 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 27, 2013 at 12:28 pm
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);
June 27, 2013 at 1:46 pm
Thanks for the help. That's what I needed!
Much appreciation,
Trish
June 27, 2013 at 1:51 pm
No problem. Glad I could help.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply