473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL - Update statement

I have two tables...Table1(name, date); Table2(appointment_name, name,
appointment_date).

I want to update appointment_date in Table2 with date from Table1
(business requirement). I am trying to write a Update statement, I
don't seem to get it:

UPDATE Table2 t2 SET appointment_date = (SELECT date FROM Table1 t1
WHERE t1.name = t2.name) -- wrong
UPDATE Table2 t2 SET appointment_date = (SELECT date FROM Table1 t1)
where t2.name = t1.name -- wrong

any help will be greatly appreciated...

Thanks

Oct 1 '07 #1
3 6337
an*************@gmail.com wrote:
I have two tables...Table1(name, date); Table2(appointment_name, name,
appointment_date).

I want to update appointment_date in Table2 with date from Table1
(business requirement). I am trying to write a Update statement, I
don't seem to get it:

UPDATE Table2 t2 SET appointment_date = (SELECT date FROM Table1 t1
WHERE t1.name = t2.name) -- wrong
UPDATE Table2 t2 SET appointment_date = (SELECT date FROM Table1 t1)
where t2.name = t1.name -- wrong
UPDATE table2 t2 SET appointment_date = (select date from table1 t1
where t1.name = t2.name)
where exists(select 1 from table1 t1 where t1.name = t2.name)

or (on DB2 for LUW)
MERGE INTO table2 t2 using table1 t1 on t1.name = t2.name
when matched then update set appointment_date = date

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Oct 1 '07 #2
On Oct 1, 5:54 pm, Serge Rielau <srie...@ca.ibm.comwrote:
annecarterfr...@gmail.com wrote:
I have two tables...Table1(name, date); Table2(appointment_name, name,
appointment_date).
I want to update appointment_date in Table2 with date from Table1
(business requirement). I am trying to write a Update statement, I
don't seem to get it:
UPDATE Table2 t2 SET appointment_date = (SELECT date FROM Table1 t1
WHERE t1.name = t2.name) -- wrong
UPDATE Table2 t2 SET appointment_date = (SELECT date FROM Table1 t1)
where t2.name = t1.name -- wrong

UPDATE table2 t2 SET appointment_date = (select date from table1 t1
where t1.name = t2.name)
where exists(select 1 from table1 t1 where t1.name = t2.name)

or (on DB2 for LUW)
MERGE INTO table2 t2 using table1 t1 on t1.name = t2.name
when matched then update set appointment_date = date

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Thanks a lot!
UPDATE table2 t2 SET appointment_date = (select date from table1 t1
where t1.name = t2.name)
where exists(select 1 from table1 t1 where t1.name = t2.name)
Could you please tell me why we need "where exists(select 1 from
table1 t1 where t1.name = t2.name)" in the query?

Thanks again!

Oct 2 '07 #3
On Oct 2, 3:56 pm, "annecarterfr...@gmail.com"
<annecarterfr...@gmail.comwrote:
[...]
UPDATE table2 t2 SET appointment_date = (select date from table1 t1
where t1.name = t2.name)
where exists(select 1 from table1 t1 where t1.name = t2.name)

Could you please tell me why we need "where exists(select 1 from
table1 t1 where t1.name = t2.name)" in the query?
Assume:

UPDATE table2 t2 SET appointment_date = (select date from table1 t1
where t1.name = t2.name)

What rows in t2 is affected by this stmt compared to Serge's stmt?:

UPDATE table2 t2 SET appointment_date = (select date from table1 t1
where t1.name = t2.name)
where exists(select 1 from table1 t1 where t1.name = t2.name)

What is the new value for appointment_date in the rows affected by the
first stmt but not the second stmt?
HTH
/Lennart

Oct 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark A Framness | last post by:
Greetings, I am working on a project and we need to write a conversion script to initialize a new field on a table. The number of records on this table is on the order of millions so routine...
7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
8
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
1
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.