Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old March 23rd, 2008, 01:39 AM
peterturcan peterturcan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 4 peterturcan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 sec
Reputation Power: 0
Copying records whithin table

Hello,

I need to copy all records whithin one table with change of primary key.

table, for example looks like this:
test.pkey
test.name
test.description

I need do (
Code:
select * from test where pkey = 1
) then change pkey=2 and insert those records back to 'test' table.

It means. copy all records with pkey=1 to the same table with another primary key.

In fact I have more complex table and I don't want to change this code after change of structure 'test' table.
(I don't want to list all fields from table)

thank you
Peter

Reply With Quote
  #2  
Old March 23rd, 2008, 03:03 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,707 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 3 h 1 m 35 sec
Reputation Power: 259
Looks like you want and update ...

Reply With Quote
  #3  
Old March 23rd, 2008, 03:10 AM
peterturcan peterturcan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 4 peterturcan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 sec
Reputation Power: 0
Quote:
Originally Posted by pabloj
Looks like you want and update ...


No, I don't want to change primary key value. I need copy all records with (let say) primary key = 1 to new records with primary key = 2.
I need copy records, not move. so new records will contain the same values as old ones except primary key value.

Reply With Quote
  #4  
Old March 23rd, 2008, 04:02 AM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 970 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 38 m 15 sec
Reputation Power: 57
Quote:
Originally Posted by peterturcan
It means. copy all records with pkey=1 to the same table with another primary key
You mean the one (and only) record with pkey=1, right? If pkey is indeed the primary key there cannot be more than one with pkey = 1

Try this:
Code:
INSERT INTO test (pkey, name, description)
SELECT 2, t2.name, t2.description
FROM test t2
WHERE t2.pkey = 1
Comments on this post
pabloj agrees!

Reply With Quote
  #5  
Old March 23rd, 2008, 01:47 PM
peterturcan peterturcan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 4 peterturcan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 sec
Reputation Power: 0
Quote:
Originally Posted by shammat
You mean the one (and only) record with pkey=1, right? If pkey is indeed the primary key there cannot be more than one with pkey = 1

Try this:
Code:
INSERT INTO test (pkey, name, description)
SELECT 2, t2.name, t2.description
FROM test t2
WHERE t2.pkey = 1


thank you, this is only way, how to do it? because after changing the structure it is necessary to change "copy" code also. is it possible to use '*' instead of listing all fields in some way?

Reply With Quote
  #6  
Old March 23rd, 2008, 01:53 PM
peterturcan peterturcan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 4 peterturcan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 sec
Reputation Power: 0
Quote:
Originally Posted by shammat
You mean the one (and only) record with pkey=1, right? If pkey is indeed the primary key there cannot be more than one with pkey = 1

ops. of course. so primary key (in this case) is pkey+name

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Copying records whithin table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway