| Author |
Topic |
|
ravinder1483
Starting Member
9 Posts |
Posted - 2011-01-08 : 07:57:04
|
Hi,I am facing some issues while updating a table variable. Here goes the query:UPDATE fd SET fd.OutFlightNumber= CASE WHEN fd.OutFlightDepDtTm=pd.FlightDepDtTm THEN pd.FlightNumberELSE fd.OutFlightNumberEND,fd.InFlightNumber= CASE WHEN fd.InFlightDepDtTm=pd.FlightDepDtTm THEN pd.FlightNumberELSE fd.InFlightNumberEND FROM @FlightDetail fd JOIN @Passengerdetail pd ON fd.ReservationID=pd.ReservationID Here is the sample data:@Passengerdetail contains following rows:ReservationId FlightNumber FlightDepDtTm PAXCount InfantCount277812591 5375 2011-02-08 1 0277812591 5375 2011-02-09 1 0Here is the ouput of Update statement:ReservationId OutFlightNumber OutFlightDepDtTm InFlightNumber InFlightDepDtTm277812591 5375 2011-02-08 NULL 2011-02-09Now what confuses me is that why i am getting NULL in the column(InFlightNumber).Please let me know whats the issue with this query.Regards,Ravinder Pal Singh Ravinder Pal SinghEmail: ravinder1483@gmail.com |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2011-01-08 : 11:21:43
|
What does flight detail have for data for the ReservationID =277812591Did you do a SELECT on the records to be sure the join worked properly ? Always test update statements by Selecting the results so you can view them. Make sure your select includes the joining columns on both sides (and do a full outer join so you can see where they don't match up)Since you have TWO records which appear identically in your sample data, and you didn't present the row equivalent data for @Flightdetail, hard to say.Do a select of the necessary fields and check the output. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-08 : 13:05:04
|
| cant understand when this condition be true?fd.InFlightDepDtTm=pd.FlightDepDtTmas i see one table has only date and other only time. so it will be false and it will be always else condition that holds good. so if InFlightNumber was NULL before it will remain same value itself------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ravinder1483
Starting Member
9 Posts |
Posted - 2011-01-08 : 22:57:40
|
| Hi visakh16,I have updated FlightDepDtTm data. Now please let me know is this query correct or i am doing something wrong.Ravinder Pal SinghEmail: ravinder1483@gmail.com |
 |
|
|
ravinder1483
Starting Member
9 Posts |
Posted - 2011-01-08 : 23:28:10
|
| Hi dataguru1971,@Flightdetail contains following data:ReservationId OutFlightNumber OutFlightDepDtTm InFlightNumber InFlightDepDtTm277812591 NULL 2011-02-08 NULL 2011-02-09This data is just for one ReservationID for which i am getting unexpected result. This table does not contain any duplicate ReservationID.I didn't get what do we mean by " Do a SELECT on the records to be sure the join worked properly" .After Update statement, i used displayed the same columns in UPDATE query using the SELECT statement. What i got was the following:SELECT query:SELECT fd.ReservationID,OutFlightDepDtTm,CASE WHEN fd.OutFlightDepDtTm=pd.FlightDepDtTm THEN pd.FlightNumberELSE fd.OutFlightNumberEND 'OutFlightNumber',InFlightDepDtTm,CASE WHEN fd.InFlightDepDtTm=pd.FlightDepDtTm THEN pd.FlightNumberELSE fd.InFlightNumberEND 'InFlightNumber'FROM @FlightDetail fd JOIN @Passengerdetail pdON fd.ReservationID=pd.ReservationIDOutput of this query is as follows:ReservationId OutFlightNumber OutFlightDepDtTm InFlightNumber InFlightDepDtTm277812591 5375 2011-02-08 NULL 2011-02-09277812591 NULL 2011-02-08 5375 2011-02-09Please help me.Ravinder Pal SinghEmail: ravinder1483@gmail.com |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2011-01-08 : 23:44:04
|
You get TWO results with the select when you want one. You can SEE where the NULL result you don't want comes fromREplace:CASE WHEN fd.OutFlightDepDtTm=pd.FlightDepDtTm THEN pd.FlightNumberELSE fd.OutFlightNumberCASE WHEN ASE WHEN fd.OutFlightDepDtTm=pd.FlightDepDtTm THEN COALESCE (pd.FlightNumber,fd.FlightNumber) ENDAnd add a group by to the select.The key is you want only ONE result from the select statement you posted. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-09 : 01:09:01
|
I think i got your problem. You're required values are existing in two different records so using single join you wont get both values . the update has to be something likeUPDATE fdSET fd.OutFlightNumber= pd1.FlightNumber,fd.InFlightNumber= pd2.FlightNumberFROM @FlightDetail fd LEFT JOIN @Passengerdetail pd1ON fd.ReservationID=pd1.ReservationIDAND fd.OutFlightDepDtTm=pd1.FlightDepDtTmLEFT JOIN @Passengerdetail pd2ON fd.ReservationID=pd2.ReservationIDAND fd.InFlightDepDtTm=pd2.FlightDepDtTm ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ravinder1483
Starting Member
9 Posts |
Posted - 2011-01-09 : 10:29:34
|
| Hi dataguru1971,I used select statement only to check where the issue is.OK, let me re-potray my question:I have two rows with same ReservationID in table1 and only a single row in table2 with same ReservationID as in table1. Now if i run an update query with join between table1 and table2, then will update statement run only for one Row in table1 or both rows in table1?Ravinder Pal SinghEmail: ravinder1483@gmail.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-09 : 12:52:33
|
| did you try my suggestion at all?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ravinder1483
Starting Member
9 Posts |
Posted - 2011-01-10 : 10:53:28
|
| Yes visakh16.Thanks for your suggestion. Its working now. But i still have a doubt which is bothering me a lotI have two rows with same ReservationID in table1 and only a single row in table2 with same ReservationID as in table1. Now if i run an update query with join between table1 and table2, then will update statement run only for one Row in table1 or both rows in table1?Ravinder Pal SinghEmail: ravinder1483@gmail.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-10 : 10:58:45
|
| it will run for both rows in table1 and end up updating the same row in table2 twice. As you had that case statement in initial update to check for additional condition before setting the in out flight numbers, what happens is during each update only one of them holds good and other one will force the false condition ie. retain original value (NULL). so based on order of update, you will still end up with NULL in one of fields in your single record of your table2------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|