Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cannot create containts

Author  Topic 

shiyam198
Yak Posting Veteran

94 Posts

Posted - 2012-04-27 : 14:07:08
Hi,

I am running a set of upgrade scripts, it does a lot of things but main parts are:

1.
It drops the containts in this table, which is a compisite Primary key created on 2 colummsn

ALTER TABLE [dbo].[ts_order_charge_audit] DROP CONSTRAINT [PK_ts_order_charge_audit]

2.
It takes the data out, change the schema, modify data and put it all back in.

3.
It build the contraints again:

ALTER TABLE [dbo].[ts_order_charge_audit] ADD CONSTRAINT [PK_ts_order_charge_audit] PRIMARY KEY CLUSTERED
(
[ordercharge_id] ASC,
[ordercharge_version] ASC
)

Now, this errors out saying "Cannot create constraints.

So, first thing I thought was, there are duplicate records on the combinatins of columns [ordercharge_id] and [ordercharge_version].

So, I checked with the following script:

SELECT a.*
FROM [ts_order_charge_audit] As [a]
INNER
JOIN
(
SELECT [ordercharge_id]
, [ordercharge_version]
FROM [ts_order_charge_audit]
GROUP
BY [ordercharge_id]
, [ordercharge_version]
HAVING Count(*) >1
) As [b]
ON a.[ordercharge_id] = b.[ordercharge_id]
AND a.[ordercharge_version] = b.[ordercharge_version]

It doesn't show any results. Which means there are no duplicates.

Why can I NOT create the constaints again?


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-27 : 14:31:10
Show us the exact error and also run this:

SELECT [ordercharge_id], [ordercharge_version], COUNT(*) AS DupeCount
FROM [ts_order_charge_audit]
GROUP BY [ordercharge_id], [ordercharge_version]
HAVING Count(*) > 1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

shiyam198
Yak Posting Veteran

94 Posts

Posted - 2012-04-27 : 14:58:02
Hi Tara,

I ran the following and no records. Really puzzling.

select ordercharge_id, ordercharge_version, COUNT(*)
from ts_order_charge_audit
group by ordercharge_id, ordercharge_version
HAVING (COUNT(*) > 1)

If it is not the duplicate records, I am not sure what it is..

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-27 : 15:00:48
You need to show us the full error.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -