Welcome Guest Search | Active Topics | Members | Log In | Register

Unique index saving problem Options · View
usenet@tynemarch.co.uk
Posted: Thursday, October 18, 2007 7:54:27 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 18/10/2007 19:54:27
Date: Fri, 19 Oct 2007 02:54:27 -0700

I have a table in a database with a primary key and a unique candidate
key

PK int
CK1 int
CK2 int
plus various data fields

We change the rows that the record with one PK value gets the CK of
another and vice versa

e.g.. PK 1 CK 1 1 becomes PK 1 CK 1 2
and PK 2 CK 1 2 becomes PK 2 CK 1 1

Therefore there is no overall key violation except temporarily when
the first record is saved but the second has not yet been changed. But
SQL server says there is a constraint violation whilst saving, it
cannot "look ahead" to the next SQL statement. We are saving the
records in an transaction one at a time with a stored procedure but we
have also tried a dataset. It's a large table so we don't want to
delete and recreate the index.

Is there anyway around it with SQL Server 2005 using VB.NET 2005 that
would not impact hugely on performance?

usenet@tynemarch.co.uk
Posted: Thursday, October 18, 2007 9:12:34 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 18/10/2007 21:12:34
Date: Fri, 19 Oct 2007 04:12:34 -0700

On 19 Oct, 11:22, "David Portas"
<REMOVE_BEFORE_REPLYING_dpor...@acm.org> wrote:
> <use...@tynemarch.co.uk> wrote in message
>
> news:1192787667.116652.205360@e34g2000pro.googlegroups.com...
>
>
>
>
>
> >I have a table in a database with a primary key and a unique candidate
> > key
>
> > PK int
> > CK1 int
> > CK2 int
> > plus various data fields
>
> > We change the rows that the record with one PK value gets the CK of
> > another and vice versa
>
> > e.g.. PK 1 CK 1 1 becomes PK 1 CK 1 2
> > and PK 2 CK 1 2 becomes PK 2 CK 1 1
>
> > Therefore there is no overall key violation except temporarily when
> > the first record is saved but the second has not yet been changed. But
> > SQL server says there is a constraint violation whilst saving, it
> > cannot "look ahead" to the next SQL statement. We are saving the
> > records in an transaction one at a time with a stored procedure but we
> > have also tried a dataset. It's a large table so we don't want to
> > delete and recreate the index.
>
> > Is there anyway around it with SQL Server 2005 using VB.NET 2005 that
> > would not impact hugely on performance?
>
> Use a single UPDATE statement. For example:
>
> UPDATE tbl
> SET ck1 =
> CASE
> WHEN ck1 = @oldvalue1 THEN @newvalue1
> WHEN ck1 = @oldvalue2 THEN @newvalue2
> END
> WHERE ck1 IN (@oldvalue1,@oldvalue2);
>
> --
> David Portas- Hide quoted text -
>
> - Show quoted text -

Thanks for this and the other suggestion. The problem is your have to
dynamically generate the SQL to save all the records, there may be
many thousands of records as well. What we really want to do is
continue to have a stored procedure to save each record (or an SP to
set the candidate keys and another for everything else).

David Portas
Posted: Friday, October 19, 2007 12:22:09 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 19/10/2007 12:22:09
Date: Fri, 19 Oct 2007 11:22:09 +0100

<usenet@tynemarch.co.uk> wrote in message
news:1192787667.116652.205360@e34g2000pro.googlegroups.com...
>I have a table in a database with a primary key and a unique candidate
> key
>
> PK int
> CK1 int
> CK2 int
> plus various data fields
>
> We change the rows that the record with one PK value gets the CK of
> another and vice versa
>
> e.g.. PK 1 CK 1 1 becomes PK 1 CK 1 2
> and PK 2 CK 1 2 becomes PK 2 CK 1 1
>
> Therefore there is no overall key violation except temporarily when
> the first record is saved but the second has not yet been changed. But
> SQL server says there is a constraint violation whilst saving, it
> cannot "look ahead" to the next SQL statement. We are saving the
> records in an transaction one at a time with a stored procedure but we
> have also tried a dataset. It's a large table so we don't want to
> delete and recreate the index.
>
> Is there anyway around it with SQL Server 2005 using VB.NET 2005 that
> would not impact hugely on performance?
>

Use a single UPDATE statement. For example:

UPDATE tbl
SET ck1 =
CASE
WHEN ck1 = @oldvalue1 THEN @newvalue1
WHEN ck1 = @oldvalue2 THEN @newvalue2
END
WHERE ck1 IN (@oldvalue1,@oldvalue2);

--
David Portas


Zarko Jovanovic
Posted: Friday, October 19, 2007 2:10:56 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 19/10/2007 14:10:56
Date: Fri, 19 Oct 2007 12:10:56 +0200

usenet@tynemarch.co.uk wrote:
> I have a table in a database with a primary key and a unique candidate
> key
>
> PK int
> CK1 int
> CK2 int
> plus various data fields
>
> We change the rows that the record with one PK value gets the CK of
> another and vice versa
>
> e.g.. PK 1 CK 1 1 becomes PK 1 CK 1 2
> and PK 2 CK 1 2 becomes PK 2 CK 1 1
>
> Therefore there is no overall key violation except temporarily when
> the first record is saved but the second has not yet been changed. But
> SQL server says there is a constraint violation whilst saving, it
> cannot "look ahead" to the next SQL statement. We are saving the
> records in an transaction one at a time with a stored procedure but we
> have also tried a dataset. It's a large table so we don't want to
> delete and recreate the index.
>
> Is there anyway around it with SQL Server 2005 using VB.NET 2005 that
> would not impact hugely on performance?
>
if you can find value of CK that 100% sure will never exist.

Let's say it's -1 -1

- save PK1's CK in some variable(s)
- change PK1's row CK into -1 -1
- change PK2's row CK to 1 1 (from PK1's row)
- change PK1's row CK (fom variable value) to 1 2

ok?
David Portas
Posted: Friday, October 19, 2007 2:14:13 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 19/10/2007 14:14:13
Date: Fri, 19 Oct 2007 13:14:13 +0100

<usenet@tynemarch.co.uk> wrote in message
news:1192792354.373909.209790@q5g2000prf.googlegroups.com...
>
> Thanks for this and the other suggestion. The problem is your have to
> dynamically generate the SQL to save all the records, there may be
> many thousands of records as well. What we really want to do is
> continue to have a stored procedure to save each record (or an SP to
> set the candidate keys and another for everything else).
>

If there are many thousands of rows then surely it will be very inefficient
to save them one at a time. If you really must do that then you could first
save them to another table (temp or permanent table) and then do a single
UPDATE based on that:

UPDATE tbl
SET
col1 = tbl_temp.col1,
col2 = tbl_temp.col2,
col3 = tbl_temp.col3
FROM tbl
JOIN tbl_temp
ON tbl.key_col = tbl_temp.key_col;

--
David Portas


Mark Yudkin
Posted: Sunday, October 21, 2007 12:11:31 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 21/10/2007 12:11:31
Date: Sun, 21 Oct 2007 10:11:31 +0200

Unfortunately, MS SQL doesn't support deferred-to-commit constraint checking
(moan, grumble).
Although you say "It's a large table so we don't want to delete and recreate
the index", your only choices are
1. Create a temporary table without all the keys, copy the data over, make
the changes, then copy the data back.
2. Drop and recreate the key/index.

<usenet@tynemarch.co.uk> wrote in message
news:1192792354.373909.209790@q5g2000prf.googlegroups.com...
> On 19 Oct, 11:22, "David Portas"
> <REMOVE_BEFORE_REPLYING_dpor...@acm.org> wrote:
>> <use...@tynemarch.co.uk> wrote in message
>>
>> news:1192787667.116652.205360@e34g2000pro.googlegroups.com...
>>
>>
>>
>>
>>
>> >I have a table in a database with a primary key and a unique candidate
>> > key
>>
>> > PK int
>> > CK1 int
>> > CK2 int
>> > plus various data fields
>>
>> > We change the rows that the record with one PK value gets the CK of
>> > another and vice versa
>>
>> > e.g.. PK 1 CK 1 1 becomes PK 1 CK 1 2
>> > and PK 2 CK 1 2 becomes PK 2 CK 1 1
>>
>> > Therefore there is no overall key violation except temporarily when
>> > the first record is saved but the second has not yet been changed. But
>> > SQL server says there is a constraint violation whilst saving, it
>> > cannot "look ahead" to the next SQL statement. We are saving the
>> > records in an transaction one at a time with a stored procedure but we
>> > have also tried a dataset. It's a large table so we don't want to
>> > delete and recreate the index.
>>
>> > Is there anyway around it with SQL Server 2005 using VB.NET 2005 that
>> > would not impact hugely on performance?
>>
>> Use a single UPDATE statement. For example:
>>
>> UPDATE tbl
>> SET ck1 =
>> CASE
>> WHEN ck1 = @oldvalue1 THEN @newvalue1
>> WHEN ck1 = @oldvalue2 THEN @newvalue2
>> END
>> WHERE ck1 IN (@oldvalue1,@oldvalue2);
>>
>> --
>> David Portas- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks for this and the other suggestion. The problem is your have to
> dynamically generate the SQL to save all the records, there may be
> many thousands of records as well. What we really want to do is
> continue to have a stored procedure to save each record (or an SP to
> set the candidate keys and another for everything else).
>


Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.078 seconds.