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 select statement into a table with multiple constraints Expand / Collapse
Author
Message
Posted Thursday, June 27, 2013 10:25 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 13, 2014 1:00 PM
Points: 27, Visits: 140
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
Post #1468252
Posted Thursday, June 27, 2013 10:33 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1468258
Posted Thursday, June 27, 2013 12:28 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 12:28 PM
Points: 4,611, Visits: 4,067
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
Post #1468289
Posted Thursday, June 27, 2013 1:46 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 13, 2014 1:00 PM
Points: 27, Visits: 140
Thanks for the help. That's what I needed!

Much appreciation,
Trish
Post #1468326
Posted Thursday, June 27, 2013 1:51 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 12:28 PM
Points: 4,611, Visits: 4,067
No problem. Glad I could help.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1468332
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse