Solved

Easy Table Update Question

Posted on 2006-06-13
4
200 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
[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
  • 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 143

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 143

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

615 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