I have a table @ROOMS_BY_DEPA_KEY1
like this:
DECLARE @ROOMS_BY_DEPA_KEY1 TABLE
(
OPRO_KEY INT,
KAPACITETA INT,
WEEK_1 DECIMAL(8,2),
WEEK_2 DECIMAL(8,2),
WEEK_3 DECIMAL(8,2),
WEEK_4 DECIMAL(8,2),
WEEK_5 DECIMAL(8,2),
WEEK_6 DECIMAL(8,2)
)
INSERT INTO @ROOMS_BY_DEPA_KEY1(OPRO_KEY)
VALUES (160000014),(160000015),(160000016),(160000017),(160000018),(160000019),(160000020)
So after filling some data table looks like this:
I must update each rows columns WEEK_1
, WEEK_2
, WEEK_3
, WEEK_4
, WEEK_5
, WEEK_6
with results from this table, which is being generated from OPRO_KEY
and KAPACITETA
:
DECLARE @WEEKS_MONTH AS DBO.WEEKS_MONTH_Type
(
WEEKOFMONTH INT,
WEEKSTART DATE,
WEEKEND DATE,
NO_DAYS INT,
AVERAGE_AVAI DECIMAL(8,2)
)
(it's table type because it's being used as functions parameter). Results from that table looks like this:
To get this second table I have a UDF:
dbo.f_SCAVAI_GetWeekAverage(@iOPRO_KEY, @WEEKS_MONTH, '1', @iKAPACITETA)
Table @WEEKS_MONTH
is filled with some values (weekofmonth, datestart, dateend, no_days) and it stays the same for each call:
My problem is how to call this function for each OPRO_KEY
and how to update week columns from returning table?
Columns are updated according to WEEKOFMONTH
. Example: WEEK_1
update where @WEEK_MONTHS.WEEK_OF_MONTH
= 1, WEEK_2
where @WEEK_MONTHS
= 2
See Question&Answers more detail:
os