| Author |
Topic |
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-09 : 10:07:13
|
I am manually updating a column called targetdate in our db using the below logic providing bunch of reqnohow to create a new computed column in the existing customerinfo table and have this logic update the new column rather than me doing it manuallyPlease adviseManual logic that i have been usingSET NOCOUNT ONGO DECLARE @reqno bigint,@targetdate datetime,@tat intDECLARE db_cursor CURSOR FOR SELECT RequestId,caseWhen Availabledt is null then approvedtWhen Availabledt > approvedt then availabledtWhen Availabledt < approvedt then approvedtELSE approvedt END,TAT from CustomerInfo where reqno in (1134567)OPEN db_cursor FETCH NEXT FROM db_cursor INTO @reqno,@targetdate,@tatWHILE @@FETCH_STATUS = 0 BEGIN DECLARE @n as int DECLARE @date2 AS DateTime SET @n = @tat SET @date2 = @targetdate WHILE (@n > 0) BEGIN SET @date2 = DATEADD(day,1,@date2) WHILE DATEPART(dw,@date2) IN (1,7) BEGIN SET @date2 = DATEADD(day,1,@date2) END SET @n = @n - 1 END UPDATE customerinfoSET targetdate = @date2 where reqno = @requestno --do work here FETCH NEXT FROM db_cursor INTO @reqno,@targetdate,@tatENDCLOSE db_cursor DEALLOCATE db_cursor thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-09 : 11:04:12
|
| can you explain the logic you're using in loop for date generation in words?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-09 : 12:12:20
|
| in this first loop when condition n(tat)> 0 is true the targetdate or date2 variable is being updated by adding a day to itIn the second loop when date2 day is 1 or 7 then it gets incremented by 1 more dayHope my explanation made senseThanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-09 : 12:24:54
|
why do you need a loop for that? wont below suffice?UPDATE cSET c.targetdate = DATEADD(dd,DATEDIFF(wk,c1.TargetDt,c1.FinalDt)*2,c1.FinalDt)FROM customerinfo cCROSS APPLY (SELECT caseWhen Availabledt is null then approvedtWhen Availabledt > approvedt then availabledtWhen Availabledt < approvedt then approvedtELSE approvedt END AS TargetDt,DATEADD(dd,TAT,caseWhen Availabledt is null then approvedtWhen Availabledt > approvedt then availabledtWhen Availabledt < approvedt then approvedtELSE approvedt END) AS FinalDtFROM customerinfo WHERE requestid= c.requestid)c1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-09 : 12:57:17
|
| testing the logic you gave me now.I will get back after doing some thorogh testing |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-09 : 16:13:54
|
| it worked great.How do i use this logic for creating a new column in the customerinfo table |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-09 : 21:47:53
|
| what has this to do with creation of column? you already have a column targetdate isnt it which you're trying to update by above statement?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-12 : 09:01:16
|
| we already have a column targetdt in the customerinfo table and i have to do manual updates to this column peridicallyhaving a separate column which will automatcially update when ever there is a change in approvedt,avaiabledt,tat would elimate the manulaupdates |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 09:56:51
|
quote: Originally posted by jim_jim we already have a column targetdt in the customerinfo table and i have to do manual updates to this column peridicallyhaving a separate column which will automatcially update when ever there is a change in approvedt,avaiabledt,tat would elimate the manulaupdates
for adding new columns use syntaxALTER TABLE <tablename> ADD <columnname> <datatype> ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-12 : 10:27:28
|
| how to make the column use the logic |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 10:35:15
|
quote: Originally posted by jim_jim how to make the column use the logic
column use what logic?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-12 : 11:12:45
|
| The new column(targetdt1) that i wanted to add to the existing table customerinfo should be a computed column which should use the logic you have provided.Instead of me running the equery everytime to update the existing targetdt columnUPDATE cSET c.targetdate = DATEADD(dd,DATEDIFF(wk,c1.TargetDt,c1.FinalDt)*2,c1.FinalDt)FROM customerinfo cCROSS APPLY (SELECT caseWhen Availabledt is null then approvedtWhen Availabledt > approvedt then availabledtWhen Availabledt < approvedt then approvedtELSE approvedt END AS TargetDt,DATEADD(dd,TAT,caseWhen Availabledt is null then approvedtWhen Availabledt > approvedt then availabledtWhen Availabledt < approvedt then approvedtELSE approvedt END) AS FinalDtFROM customerinfo WHERE requestid= c.requestid)c1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 11:27:19
|
ok. then best thing you can do is to create a udf for this logic likeCREATE FUNCTION GetTargetDate(@RequestID int)RETURNS datetimeASBEGINDECLARE @TargetDate datetimeSELECT @TargetDate = DATEADD(dd,DATEDIFF(wk,c1.TargetDt,c1.FinalDt)*2,c1.FinalDt)FROM(SELECT caseWhen Availabledt is null then approvedtWhen Availabledt > approvedt then availabledtWhen Availabledt < approvedt then approvedtELSE approvedt END AS TargetDt,DATEADD(dd,TAT,caseWhen Availabledt is null then approvedtWhen Availabledt > approvedt then availabledtWhen Availabledt < approvedt then approvedtELSE approvedt END) AS FinalDtFROM customerinfo WHERE requestid= @RequestID)tRETURN @TargetDate ENDthen use it likeALTER TABLE <tablename> ADD <columnname> AS dbo.GetTargetDate(requestid) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-12 : 15:04:18
|
| in my testing i found belowthis worked perfectly for requests which have a single record in the customerinfo table but when there are more than 1 record for a single reqest with differant values for tat,approvedt and availabledt then the logic is failing |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 15:09:55
|
| show me a data sample where it fails. post data and explain please------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-13 : 09:03:58
|
| I created a new column(targetdate) in customerinfo table which uses the new function and below are the results for a request in cusomterinfo tablereqno tat approvedt availabledt targetdate146957 10 03/13/2012 04/14/2011 05/28/2012146957 20 03/13/2012 05/14/2011 05/28/2012146957 30 03/13/2012 06/14/2011 05/28/2012146957 40 03/13/2012 07/14/2011 05/28/2012146957 50 03/13/2012 08/14/2011 05/28/2012146957 60 03/13/2012 09/14/2011 05/28/2012All the targetdates are showing as 5/28 |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-13 : 09:19:56
|
| I added rptdesc in my cursor logic and got the update to work correctly.How to add rptdesc in the function logic you have providedSET NOCOUNT ONGO DECLARE @requestid bigint,@targetdt datetime,@tat int,@rptdesc intDECLARE db_cursor CURSOR FOR SELECT RequestId,caseWhen DateAvailable is null then uwapprovedtWhen DateAvailable > uwapprovedt then dateavailableWhen DateAvailable < uwapprovedt then uwapprovedtELSE uwapprovedt END,TAT,rptdesc from selected_report where requestid in ()OPEN db_cursor FETCH NEXT FROM db_cursor INTO @requestid,@targetdt,@tat,@rptdescWHILE @@FETCH_STATUS = 0 BEGIN DECLARE @n as int DECLARE @date2 AS DateTime SET @n = @tat SET @date2 = @targetdt WHILE (@n > 0) BEGIN SET @date2 = DATEADD(day,1,@date2) WHILE DATEPART(dw,@date2) IN (1,7) BEGIN SET @date2 = DATEADD(day,1,@date2) END SET @n = @n - 1 END UPDATE SELECTED_REPORTSET targetdt = @date2 where requestid = @requestid and rptdesc = @rptdesc --do work here FETCH NEXT FROM db_cursor INTO @requestid,@targetdt,@tat,@rptdescENDCLOSE db_cursor DEALLOCATE db_cursor |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-03-13 : 10:38:37
|
| Thank you so much VisakhI altered the function logic and got what i want |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-13 : 15:06:57
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|