Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
boybles
Starting Member
23 Posts |
Posted - 2009-05-29 : 23:51:18
|
I want to write an update statement so it updates the appropriate RequestID values in the Schedule table when joined between Request.PlanID and Schedule.PlanID asshown in "Desired Result". Please help!!Thanks,Tony [Tables]Request--------RequestID (int)PlanID (int)Name (varchar)Schedule---------ScheduleID (int)PlanID (int)RequestID (int)StartTime (datetime)[Data]Request--------------RequestID|PlanID|Name--------------10|1|EventXXX11| |EventZZZ12|2|EventYYY13| |EventAAA14|3|EventCCCSchedule------------------------------ScheduleID|PlanID|RequestID|StartTime------------------------------1|1| |12:002| | |9:303|2| |5:304|3| |8:305| | |10:30[Desired Result]Once the update statement is executed, the updated Schedule table should look like this:Schedule------------------------------ScheduleID|PlanID|RequestID|StartTime------------------------------1|1|10|12:002| | |9:303|2|12|5:304|3|14|8:305| | |10:30 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-05-30 : 00:52:05
|
| this?Update aSet a.RequestID = b.RequestIDfrom Schedule a inner join Request bon a.PlanID = b.PlanID |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2009-05-30 : 01:02:33
|
| Am I right in thinking that each plan is related to exactly one request and one schedule? If not then the update you want to do is not possible because there might be multiple RequestIDs for a given plan and you can only put one RequestID in the column in the Schedule table.However, if my assumption is correct then you don't need to put the RequestID in the Schedule table at all because you can just join the Schedule table to the Plan table and the Request table whenever you need to know what the RequestID is for a given Schedule.A join is the way you'd do the update you asked about if you decide to follow through with that design (I'd recommend against it though)UPDATE ScheduleSET Schedule.RequestID = Request.RequestIDFROM ScheduleINNER JOIN Request ON Request.PlanID = Schedule.PlanID |
 |
|
|
|
|
|