I have created a CV just like this "Former Member" on this blog https://blogs.sap.com/2017/05/18/factory-calendar-transpose-in-sap-hana-studio-step-by-step/
It works like a charm!
My next requirement is to take that DATE_SAP for each record and determine the Accounting Month End; Yes I understand that that value will be the same for up to 30 days.
I have a function that works and gives me the accounting month end date but I cant seem to determine how to make it work with the HANA CV view I created.
Huge thanks in advance!
Here is the function
CREATE FUNCTION "MY_SCHEMA"."FN_DTACCTMONTHEND"
(
-- Add the parameters for the function here
dtexp date
)
RETURNS dtwkend date
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
-- Declare the return variable here
DECLARE dttemp date;
declare cnt int;
declare c_dtwkend date;
Select ADD_DAYS(to_date('19000107', 'YYYYMMDD'),(FLOOR((days_between(to_date('19000107', 'YYYYMMDD'),:dtexp) / 7)) * 7) + 7) into c_dtwkend from dummy;
cnt := 0;
while :cnt < 6 DO
dttemp := add_days(:c_dtwkend,7);
if MONTHNAME(:dttemp) = MONTHNAME(:c_dtwkend) then
c_dtwkend := dttemp;
else
dtwkend := to_date(c_dtwkend);
return;
end if;
cnt := :cnt + 1;
end while;
-- Return the result of the function
dtwkend := to_date(c_dtwkend);
return;
END;
Here is the table function:
CREATE FUNCTION "SCHEMA_NAME"."SCHEMA_NAME::FN_DTACCTMONTHEND_TEST" (DATE_SAP date )
RETURNS TABLE (DTWKEND date)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
DECLARE dttemp date;
DECLARE cnt int;
DECLARE c_dtwkend date;
DECLARE dtwkend date;
Select ADD_DAYS(to_date('19000107', 'YYYYMMDD'),(FLOOR((days_between(to_date('19000107', 'YYYYMMDD'),:DATE_SAP) / 7)) * 7) + 7) into c_dtwkend
from dummy;
cnt := 0;
while :cnt < 6 DO
dttemp := add_days(:c_dtwkend,7);
if MONTHNAME(:dttemp) = MONTHNAME(:c_dtwkend) then
c_dtwkend := dttemp;
else
dtwkend := to_date(c_dtwkend);
return;
end if;
cnt := :cnt + 1;
end while;
-- Return the result of the function
dtwkend := to_date(c_dtwkend);
RETURN
SELECT dtwkend from dummy;
END;
Error received is SAP DBTech JDBC: [2]: general error: RETURN statement with expression should be defined for table function