Skip to content
Advertisement

Select with update statement sap hana

I’m trying to combine select and update in one statement

In select I’m trying to find what is the current Fiscal year fiscal period based on table "month_dim" and by using current date. its running fine since it has corresponding values. But I need to pass values from select to update statement to same table to update values of CY,PY and YTD. Currently in update statements I’m doing hard coding and have 3 separate update statements. it will good if it can be combined in one as well.

select fiscal_period, fiscal_year from "schema_name"."month_dim" where date_sql = current_date

update statements are below:-

UPDATE "Schema_name"."month_dim" SET

  YTD = case when fiscal_period <= '1'

       then 'Y' else 'N' end;
UPDATE " Schema_name "."month_dim" SET

    CY = case when fiscal_year >= '2021'

       then 'Y' else 'N' end;
UPDATE " Schema_name "."month_dim" SET

    PY = case when fiscal_year <= '2021'

       then 'Y' else 'N' end;

Please help

Thanks

Advertisement

Answer

Is this what you are looking for? :

DO
BEGIN
DECLARE FISCALPERIOD NVARCHAR(3); 
DECLARE FISCALYEAR NVARCHAR(4);

    SELECT 
            fiscal_period, fiscal_year 
        INTO fiscalperiod, fiscalyear 
    FROM 
            schema_name.month_dim
    WHERE 
           date_sql = current_date;

    UPDATE schema_name.month_dim
    SET
        YTD = CASE
                  WHEN fiscal_period <= :fiscalperiod THEN 'Y'
                  ELSE 'N'
              END
      , CY = CASE
                 WHEN fiscal_year >= :fiscalyear THEN 'Y'
                 ELSE 'N'
             END
      , PY = CASE
                 WHEN fiscal_year <= :fiscalyear THEN 'Y'
                 ELSE 'N'
             END;
END;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement