简体   繁体   English

选择带有更新语句 sap hana

[英]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.在选择中,我试图根据表"month_dim"并使用当前日期查找当前财政年度会计期间是什么。 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 .但我需要将值从 select 到 update 语句传递到同一个表以更新CYPYYTD Currently in update statements I'm doing hard coding and have 3 separate update statements.目前在更新语句中我正在做硬编码并且有 3 个单独的更新语句。 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谢谢

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM