Solved

Easy Table Update Question

Posted on 2006-06-13
4
194 Views
Last Modified: 2006-11-18
Hello everyone,

I am using MS SQL 2000 and I actually had a question about the below piece of SQL code.  I guess I am having trouble grasping how it works (and whether it does what I intend to do).  

update Table A
set someColumn = 'Yes'
where id in (select id from Table B)
and date in (select date from Table B)
and someColumn is null

What I really want to do is to set someColumn to 'Yes' only when the id and date on the same row match the id and date from Table B also on a single row.  Each id could occur multiple times in either table with different dates.  However, I am worried that SQL Server might pick an id and a date from separate rows in Table B and update someColumn to 'Yes' in Table A since that combination happened to fall on one row.  Is that what's happening here?  

My apologies if the question above was unclear in any way.  I'd be happy to explain in greater detail if necessary.  Thanks for all your help!
0
Comment
Question by:sviriyal
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 16894330
Hi sviriyal,

Yes is the simple answer, it will match providing there is a row matching the id and a row matching the date in table B. These need not necessarily be the same row.

You need:

Update TableA Set SomeColumn = 'Yes' From TableA Inner Join TableB On TableA.ID = TableB.ID and TableA.Date = TableA.Date Where TableA.SomeColumn Is Null

Though I assume these are not the real field names, you should be aware that "Date" is a reserved word and not advised to be used for naming your column.

Tim Cottee
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16894338
update Table A
set someColumn = 'Yes'
from tableA
JOIN tableB
  on tableA.id =TableB.id
and tabeA.date = tableB.date
and someColumn is null
0
 

Author Comment

by:sviriyal
ID: 16894468
Thank you both for your comments.  However, since Tim was the first to respond with a working answer, I think the points should go to him.  Any objections angelIII?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16894487
no objects, as additional information (reserved words) were given.
Glad we could help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

746 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

13 Experts available now in Live!

Get 1:1 Help Now