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 |
|
cwfontan
Yak Posting Veteran
87 Posts |
Posted - 2010-03-31 : 18:23:49
|
Inserting a new record ID=1, neweffDate=1/7/10,newexpDate=NULLupdate the previous if expdate > neweffDate. (expDate = newEffDate)if newExpDate IS NULL and there is a recod with effdate > newEffDate (update newExpDate = effdate closest to the neweffdate)if newExpDate IS NOT NULL and there is a recod with effdate < newExpDate (update newExpDate = effdate closest to the neweffdate)tbl_Zones ( sample data )ID effDate expDate1 1/1/10 1/5/101 1/5/10 1/10/10 <---Update expDate = 1/7/10 <---Insert 1 1/7/10 NULL <---Update expDate = 1/1/111 1/1/11 1/5/111 1/5/11 NULL pls help! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
cwfontan
Yak Posting Veteran
87 Posts |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-04-01 : 09:41:40
|
| Hi, Using the trigger you can do this way:create table Test(id int, effdate datetime,expdate datetime)create trigger trg_teston Testafter insertasBeginDeclare @CurDate datetimeDeclare @lastDate datetimeSelect @CurDate = effdate from insertedselect @lastdate = max(test.expdate) from test inner join inserted on test.id = inserted.id and test.effdate <> @CurDateprint @lastdateupdate Test set expdate = dateadd(dd,-1,@CurDate) -- '2010-2-23' From Test t inner join inserted i on t.id = i.id and t.expdate = @lastDateendinsert into TestValues (1,'2010-01-01','2010-01-31')insert into TestValues (1,'2010-01-15','2010-02-28')insert into TestValues (2,'2010-01-15','2010-02-28')Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
cwfontan
Yak Posting Veteran
87 Posts |
Posted - 2010-04-01 : 15:24:54
|
| very nice this definatley got me to my solution!Much appreciated!! |
 |
|
|
|
|
|
|
|