 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pascal Schmidt-Volkmar Guest
|
Posted: Mon Dec 29, 2003 7:21 am Post subject: swapping two values in db |
|
|
Hi there,
I have entries in my db, each having a unique number. Now I would like to
change two entries so they just swap their numbers.
example: entry A with number 2
entry B with number 3
later: entry A with number 3
entry B with number 2
How could this be done? I tried a stored procedure but I failed because
after changing the first of both values, the primary key was violated. Maybe
I do need a third variable to store one of the two numbers....
Thanks for any help!!
Pascal
|
|
| Back to top |
|
 |
Bill Todd (TeamB) Guest
|
Posted: Mon Dec 29, 2003 12:43 pm Post subject: Re: swapping two values in db |
|
|
Please do not multipost or cross post your messages. It violates
Borland's guidelines for using their newsgroups and wastes everyone's
time. Please post one message in the most appropriate newsgroup and
please take a moment to read the newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html
Change A to 99.
Change B to 2.
Change A to 3.
--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
|
|
| Back to top |
|
 |
Dmeister Guest
|
Posted: Tue Dec 30, 2003 12:52 pm Post subject: Re: swapping two values in db |
|
|
You've violated a basice database design rule by associating intelligence
with your Primary key. Your primary key should never be touched once
created. You should have a completely agnostic PK in addition to your
unique number field which you can manipulate to your heart's content.
"Pascal Schmidt-Volkmar" <pascalsv (AT) nospam_tauth (DOT) com> wrote
| Quote: | Hi there,
I have entries in my db, each having a unique number. Now I would like to
change two entries so they just swap their numbers.
example: entry A with number 2
entry B with number 3
later: entry A with number 3
entry B with number 2
How could this be done? I tried a stored procedure but I failed because
after changing the first of both values, the primary key was violated.
Maybe
I do need a third variable to store one of the two numbers....
Thanks for any help!!
Pascal
|
|
|
| Back to top |
|
 |
Brett Watters Guest
|
Posted: Tue Dec 30, 2003 6:58 pm Post subject: Re: swapping two values in db |
|
|
Pascal,
Create column C.
Set C = A
then
Set A = B and
B = C
finally,
Drop column C.
Thanks,
Brett
"Pascal Schmidt-Volkmar" <pascalsv (AT) nospam_tauth (DOT) com> wrote
| Quote: | Hi there,
I have entries in my db, each having a unique number. Now I would like to
change two entries so they just swap their numbers.
example: entry A with number 2
entry B with number 3
later: entry A with number 3
entry B with number 2
How could this be done? I tried a stored procedure but I failed because
after changing the first of both values, the primary key was violated.
Maybe
I do need a third variable to store one of the two numbers....
Thanks for any help!!
Pascal
|
|
|
| Back to top |
|
 |
Tony J Hopkinson Guest
|
Posted: Tue Dec 30, 2003 9:03 pm Post subject: Re: swapping two values in db |
|
|
On Mon, 29 Dec 2003 08:21:34 +0100, "Pascal Schmidt-Volkmar"
<pascalsv (AT) nospam_tauth (DOT) com> wrote:
| Quote: | Hi there,
I have entries in my db, each having a unique number. Now I would like to
change two entries so they just swap their numbers.
example: entry A with number 2
entry B with number 3
later: entry A with number 3
entry B with number 2
How could this be done? I tried a stored procedure but I failed because
after changing the first of both values, the primary key was violated. Maybe
I do need a third variable to store one of the two numbers....
Thanks for any help!!
Pascal
In a stored procedure. |
Begin a transaction
Just select both records into variables if there aren't too many
fields (if there are use a temporary table).
Delete both records.
insert them back in swapped over
commit the transaction
or on an error rollback the transaction
another method would be to select the data into variables / temporary
table. the execute update statements from the saved data essentially
overwriting everthing but the primary key for each record to be
swapped.
Wrap the above in a transaction as well as a fail midway hrough the
swap will mess your table up considerably.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|