Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2013-01-16
9
370 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 143

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to limit User Input 2 27
Help with Oracle IF statment 5 24
RAISERROR WITH NOWAIT 2 16
How to repeat the data 4 17
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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