Here is structure of my table CustInfo
Cm_ID | Cust_ID | StartDate | EndDate | Status
1 | 1020 | 05/09/2013 | 20/09/2013 | Off
2 | 1027 | 16/09/2013 | **31/12/2099** | **On**
3 | 1020 | 21/09/2013 | 31/12/2099 | On
I want to do that whenever I will insert a new row for Cust_ID=1027 It should first update the [EndDate] to new [StartDate] i.e Today and Set [Status]='Off'.
After inserting new row it should look like this
Cm_ID | Cust_ID | StartDate | EndDate | Status
1 | 1020 | 05/09/2013 | 20/09/2013 | Off
2 | 1027 | 16/09/2013 | **30/09/2013** | **Off**
3 | 1020 | 21/09/2013 | 31/12/2099 | On
4 | 1027 | 01/10/2013 | 31/12/2099 | On
I have done this using 2 different queries as follows.
Update CustInfo SET EndDate = '30/09/2013' ,Status='Off'
WHERE Cm_ID=(SELECT MAX(Cm_ID) FROM CustInfo WHERE EndDate='12/31/2099' AND Cust_ID=1027)
INSERT INTO CustInfo ([Cust_ID], [StartDate], [EndDate], [CurrentStatus])
VALUES(1027,'01/10/2013','12/31/2099','On')
Now i want to do this using a single query or a Stored Procedure. But I dont know how to do this?