Solved

Easy Table Update Question

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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