| 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 |
|
 |
Mehrdad Momeni Guest
|
Posted: Mon Dec 29, 2003 1:58 pm Post subject: Re: swapping two values in db |
|
|
Hi
You should use a temp value - some thing that never happens in real world.
If your primary keys are auto generated (like identites in MS SQL Server),
and if they start with positive numbers, then one good candidate for the
temp value is the negate of first record's key.
cheers
|
|
| 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 |
|
 |
|