Solved

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

Posted on 2013-01-16
9
371 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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