I need to do is if the insert is successfull imported to tableD run the update statement and if the insert fails at any point the update statment should fail.Please Correct my query to get the desired output.Thanks for your help in advance!!!!!!!SET dateformat dmyDECLARE @TableA TABLE( Pdate datetime, SID int, type int, Er int, Ac int, status char(3))INSERT INTO @TableASELECT '7-6-2007', 100, 1, 52, 26,'N' UNION ALLSELECT '8-6-2007', 100, 1, 52, 47,'N' UNION ALLSELECT '7-6-2007', 101, 1, 54, 27,'N' UNION ALLSELECT '8-6-2007', 102, 1, 52, 28,'N' UNION ALLSELECT '8-6-2007', 105, 1, 62, 38,'N' UNION ALLSELECT '8-6-2007', 106, 1, 62, 38,'N'DECLARE @TableB TABLE( Pdate datetime, SID int, type int, nt1 varchar(2), nt2 varchar(3),status char(3))INSERT INTO @TableBSELECT '7-6-2007', 100, 2, 'PR', 'RP1','N' UNION ALLSELECT '8-6-2007', 101, 2, 'PR', 'SP1','N' UNION ALLSELECT '10-6-2007', 103, 2, '0p', 'DT1','N'DECLARE @TableC TABLE( Pdate datetime, SID int, type int, FT1 varchar(2), FT2 varchar(2),status char(3))INSERT INTO @TableCSELECT '7-6-2007', 100, 3, 'KR', '23','N' UNION ALLSELECT '7-6-2007', 101, 3, '1R', '21','N' UNION ALLSELECT '8-6-2007', 102, 3, 'TR', '31','N' UNION ALLSELECT '9-6-2007', 102, 3, 'TP', '31','N' UNION ALLSELECT '11-6-2007', 104, 3, 'Rp', 'VT','N'DECLARE @TableD TABLE( Pdate datetime, SID int, type int, Er int, Ac int, type2 int, nt1 varchar(2), nt2 varchar(3), type3 int, FT1 varchar(2), FT2 varchar(2) )IF EXISTS (select SID = coalesce(a.SID, b.SID, c.SID) from @TableA A,@TableB B,@TableC c where SID not in (select SID from @tableD ))BeginBegin transactionINSERT INTO @TableDSELECT Pdate = a.Pdate, SID = coalesce(a.SID, b.SID, c.SID), a_type = a.type, a.Er, a.Ac, b_type = b.type, b.nt1, b.nt2, c_type = c.type, c.FT1, c.FT2FROM ( SELECT Pdate, SID, type, Er, Ac FROM @TableA t WHERE Pdate = (SELECT MAX(Pdate) FROM @TableA x WHERE x.SID = t.SID) and status='N' AND not exists (select * from @tabled D where D.sid = t.SID) ) a FULL OUTER JOIN --@TableB b( SELECT Pdate, SID, type, nt1, nt2 FROM @Tableb t WHERE Pdate = (SELECT MAX(Pdate) FROM @TableB x WHERE x.SID = t.SID) and status='N' AND not exists (select * from @tabled D where D.sid = t.SID) ) b ON a.SID = b.SID FULL OUTER JOIN --@TableC c( SELECT Pdate, SID, type, Ft1, Ft2 FROM @Tablec t WHERE Pdate = (SELECT MAX(Pdate) FROM @TableC x WHERE x.SID = t.SID) and status='N' AND not exists (select * from @tabled D where D.sid = t.SID) ) C ON a.SID = c.SIDORDER BY SID /* If the data is imported to table4 from three tables(TablesA,TableB,TableC) then status in three tables of respective records should be updated to 'IMP' */ UPDATE @tableA t SET status = 'IMP' WHERE status='N' AND exists (select * from @tableD D where D.sid = t.SID) UPDATE @tableB t SET status = 'IMP' WHERE status='N' AND exists (select * from @tableD D where D.sid = t.SID) UPDATE @tableC t SET status = 'IMP' WHERE status='N' AND exists (select * from @tableD D where D.sid = t.SID) --IF (@@Error == 0) --commit --else --BEGIN --rollback transaction--put other error handling scripts hereENDselect * from @tableaselect * from @tablebselect * from @tablecselect * from @tabledExpected Output:TableA:Pdate SID type Er Ac status ------------------------------------------------------ ----------- ----------- ----------- ----------- ------ 2007-06-07 00:00:00.000 100 1 52 26 IMP 2007-06-08 00:00:00.000 100 1 52 47 IMP 2007-06-07 00:00:00.000 101 1 54 27 IMP 2007-06-08 00:00:00.000 102 1 52 28 IMP 2007-06-08 00:00:00.000 105 1 62 38 IMP 2007-06-08 00:00:00.000 106 1 62 38 IMP TableB:Pdate SID type nt1 nt2 status ------------------------------------------------------ ----------- ----------- ---- ---- ------ 2007-06-07 00:00:00.000 100 2 PR RP1 IMP 2007-06-08 00:00:00.000 101 2 PR SP1 IMP 2007-06-10 00:00:00.000 103 2 0p DT1 IMPTableC:Pdate SID type FT1 FT2 status ------------------------------------------------------ ----------- ----------- ---- ---- ------ 2007-06-07 00:00:00.000 100 3 KR 23 IMP 2007-06-07 00:00:00.000 101 3 1R 21 IMP 2007-06-08 00:00:00.000 102 3 TR 31 IMP 2007-06-09 00:00:00.000 102 3 TP 31 IMP 2007-06-11 00:00:00.000 104 3 Rp VT IMPTableD:Pdate SID type Er Ac type2 nt1 nt2 type3 FT1 FT2 ------------------------------------------------------ ----------- ----------- ----------- ----------- ----------- ---- ---- ----------- ---- ---- 2007-06-08 00:00:00.000 100 1 52 47 2 PR RP1 3 KR 232007-06-07 00:00:00.000 101 1 54 27 2 PR SP1 3 1R 212007-06-08 00:00:00.000 102 1 52 28 NULL NULL NULL 3 TP 31NULL 103 NULL NULL NULL 2 0p DT1 NULL NULL NULLNULL 104 NULL NULL NULL NULL NULL NULL 3 Rp VT2007-06-08 00:00:00.000 105 1 62 38 NULL NULL NULL NULL NULL NULL2007-06-08 00:00:00.000 106 1 62 38 NULL NULL NULL NULL NULL NULL