|
Is it possible to use DATEADD with the values from a table to add different numbers of months to the current date? I am trying to write an update statement, which sets one column equal to another for a number of months, where the number of months to be updated for each record is held in a different table what I have is...
Sample data:
(comments are locked)
|
|
Yes.
gives
where the current date and time was 13 April 2010 17:15:12.817 update: now we have some sample data Ignoring the fact that non of the sample data actually falls within the given where clause, see what happens when we join the two tables and calculate some start and end 'periods'
gives us
and if we add a slightly modified where clause
we get one row :
now we can turn this into an update:
so you can see there is no need to set the variables outside the update, in fact this is where your syntax error was That looks great but I am still struggling to apply it in what I am trying to do (getting a "you must declare scalar variable" message).
Apr 13 '10 at 03:32 PM
Andy 1
This is what I have
declare @periods CUST_FCST (ship_to int, sold_to int, periods int)
Set @CurrFcstPeriod = left(convert(varchar(8),getdate(),112),6)
Set @EndFcstPeriod = LEFT(convert(varchar(8),dateadd(month, @values, GETDATE()),112),6)
select * from SCP_FCST_TIME_SERIES t inner join SCP_FCST_ROOT r on t.SCP_SEQ_NBR = r.SCP_SEQ_NBR inner join CUST_FCST c on r.fcst_2_id = c.Sold_To and r.fcst_3_id = c.Ship_To
update SCP_FCST_TIME_SERIES
set FCST_RSLT_QTY = ads1_fcst_rslt_qty,
MGMT_CODE = 'R'
where FCST_YR_PRD > @CurrFcstPeriod
and FCST_YR_PRD < @EndFcstPeriod
Apr 13 '10 at 03:33 PM
Andy 1
could you re-paste that into your original question -it doesn't make much sense, and maybe some sample data? That error message means simply that you haven't declared your variables, and I can see straight away that
Apr 13 '10 at 04:15 PM
Kev Riley ♦♦
you need to supply a data type for @periods
Apr 13 '10 at 08:54 PM
Scot Hauder
Awesome, thanks so much for your help with this!
Apr 14 '10 at 10:49 AM
Andy 1
(comments are locked)
|

