Solved

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

Posted on 2013-01-16
9
364 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
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 38783541
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now