Solved

Updating a table,what's the best approach to fix it

Posted on 2013-01-16
9
369 Views
Last Modified: 2013-01-16
I have 2 tbls. A & B

In table B, I have the primary key pID and a column for TSN. Both are populated.

In table A, I have some  rows with no pID or TSN values. Also I have some rows with pID but no TSN.

For table A,I think I will not be able to do anything with the ones with no pID or TSN  because there will not be a link,(if there is a way,let me know) but I will be able to get the pID for those with TSN.

In table A, I added a column to tag it as "Yes" if an update occurs.

What is the best approach to update it?

Is it better to do it in VB.net by looping to each record and update one row at a time or do it straight to the backend and do a mass update thru an Update statement?
0
Comment
Question by:zachvaldez
  • 4
  • 4
9 Comments
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 250 total points
ID: 38783431
This will update those that match and set your flag, very fast and efficient.
UPDATE A
SET TSN = B.TSN, TagColumn = 'Yes'
FROM A
INNER JOIN B ON A.pID = B.pID

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 38783477
If I set the tagcolumn as bit data type, can I put "Y"?
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38783501
A BIT data type can only be 0 or 1 ( or NULL, yuck) not anything else.  Many programs, like Excel, will automatically convert BIT to Y/N, but you do not store the "Y".
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 38783541
0
 

Author Comment

by:zachvaldez
ID: 38783603
UPDATE A
SET TSN = B.TSN, TagColumn = 'Yes'
FROM A
INNER JOIN B ON A.pID = B.pID
and REW=2 >> 'IS THIS ADDITION LINE CORRECT
0
 

Author Comment

by:zachvaldez
ID: 38783620
SHOULD i USE AND OR WHERE IN LAST LINE?
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38783642
Logically I would use WHERE, but actually in the case of an INNER JOIN it works the same either way, an OUTER JOIN would make a big difference.
IF REW is in both tables make sure you add the Table prefix.
0
 

Author Comment

by:zachvaldez
ID: 38783760
It is only on one table.
Im kind confuse a bit using INNER JOINS compared to TSQL which is Im used to because in TSQL
after the WHERE clause, all additional lines precedes with 'AND' is used.
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38783805
That is all TSQL just joining 2 tables.  Try this select to see what it is doing, it will show you it is matching on the pID column and showing the TSN from both tables.
SELECT A.pID, A.TSN, B.TSN, TagColumn = 'Yes'
FROM A
INNER JOIN B ON A.pID = B.pID
WHERE A.TSN IS NULL
AND B.TSN IS NOT NULL
AND REW=2

Open in new window

I did forget on piece on your update, I added to the WHERE to only update if TSN is NULL in A and NOT NULL in B so you do not set the flag unless it is really changing the TSN value.
UPDATE A
SET TSN = B.TSN, TagColumn = 'Yes'
FROM A
INNER JOIN B ON A.pID = B.pID
WHERE A.TSN IS NULL
AND B.TSN IS NOT NULL
AND REW=2

Open in new window

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get row value in vb.net 4 14
string fuctions 4 26
VB.Net Tranfer data between to VB.Net apps 8 26
Setting Macro in Access to Automate Running an Append at a Certain Time 2 24
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question