Solved

Easy Table Update Question

Posted on 2006-06-13
4
195 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

910 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

26 Experts available now in Live!

Get 1:1 Help Now