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:
  #1  
Old September 24th, 2009, 10:43 PM
anop anop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 6 anop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 11 m 42 sec
Reputation Power: 0
How to Update Column from column of another table

I have table A and B, A is subset of B. all of A's Primary Key field are in Table B. I need to update some fields in Table A from those fields in Table B.

Structure of Table A
ID
FIELD1
FIELD2

Structure of Table B
ID
FIELD1
FIELD2

Sample Data of Table A

ID FIELD1 FIELD2
1 11 12
2 21 22
3 31 32

Sample Data of Table B

ID FIELD1 FIELD2
1 111 122
2 211 222
3 311 322
4 411 422
5 511 522

How to write the Update command to have the Table A updated to result below?

Table A after Updated

ID FIELD1 FIELD2
1 111 122
2 211 222
3 311 322

Reply With Quote
  #2  
Old September 25th, 2009, 01:53 AM
anop anop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 6 anop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 11 m 42 sec
Reputation Power: 0
There is a way to write SQL update in MSSQL like

UPDATE dbo.Table2
SET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB
FROM dbo.Table2
INNER JOIN dbo.Table1
ON (dbo.Table2.ColA = dbo.Table1.ColA);

Is there any equivalent to do as MSSQL did or I have to write store procedure to this update ?

Reply With Quote
  #3  
Old September 25th, 2009, 02:17 AM
mIRCata mIRCata is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 72 mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 13 m 26 sec
Reputation Power: 7
Quote:
Originally Posted by anop
I have table A and B, A is subset of B. all of A's Primary Key field are in Table B. I need to update some fields in Table A from those fields in Table B.

Structure of Table A
ID
FIELD1
FIELD2

Structure of Table B
ID
FIELD1
FIELD2

Sample Data of Table A

ID FIELD1 FIELD2
1 11 12
2 21 22
3 31 32

Sample Data of Table B

ID FIELD1 FIELD2
1 111 122
2 211 222
3 311 322
4 411 422
5 511 522

How to write the Update command to have the Table A updated to result below?

Table A after Updated

ID FIELD1 FIELD2
1 111 122
2 211 222
3 311 322

UPDATE TABLEA SET
FIELD1 = (SELECT TABLEB.FIELD1 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID),
FIELD2= (SELECT TABLEB.FIELD2 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID)

Reply With Quote
  #4  
Old September 25th, 2009, 02:37 AM
anop anop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 6 anop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 11 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by mIRCata
UPDATE TABLEA SET
FIELD1 = (SELECT TABLEB.FIELD1 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID),
FIELD2= (SELECT TABLEB.FIELD2 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID)


Thank you for your valuable reply. I have 26000 rows in Table B and 2800 rows in Table A. I use a command per your suggestion it takes so long to get it done but it works. Is there any way to make it run faster.

Reply With Quote
  #5  
Old September 25th, 2009, 03:03 AM
mIRCata mIRCata is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 72 mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 13 m 26 sec
Reputation Power: 7
Quote:
Originally Posted by anop
Thank you for your valuable reply. I have 26000 rows in Table B and 2800 rows in Table A. I use a command per your suggestion it takes so long to get it done but it works. Is there any way to make it run faster.

Are there indexes for TableA.ID and TableB.ID?

Reply With Quote
  #6  
Old September 25th, 2009, 04:14 AM
anop anop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 6 anop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 11 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by mIRCata
Are there indexes for TableA.ID and TableB.ID?


I got it. Table B has no index. That why it so slow. Actually ID Field of Table A and B is the primary key. But I import the table from other database and I forgot to set the Primary. Now it work so fast. Thank to asking me about the index.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > How to Update Column from column of another 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




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek