Solved

Easy Table Update Question

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 Lag Function With Group Levels 1 27
SQL Server 2012 Database Restore 4 39
MS SQL: Create User Function to Remove Long Words 5 32
CDC and AOG on MS SQL 2012 13 24
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…

829 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