?
Solved

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

Posted on 2013-01-16
9
Medium Priority
?
373 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
[X]
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
  • 4
  • 4
9 Comments
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 1000 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 27

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 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 27

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 27

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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