“获取数据返回超出请求行数的错误 SQL HANA SAP”

huangapple go评论62阅读模式
英文:

Getting fetch returns more than requested number of rows error SQL HANA SAP

问题

抱歉,您提供的内容已经是翻译好的,没有需要额外翻译的部分。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

Hello can you please help me fix this query :

SELECT T1."Price" into pght FROM ITM1 T1 WHERE T1."PriceList" = '1';

UPDATE "ITM1"
    SET ITM1."Factor" = T2."U_COEFFICIENT" , ITM1."Price" = T2."U_COEFFICIENT" * pght
    FROM ITM1
    INNER JOIN OITM T0 ON T0."ItemCode" = ITM1."ItemCode"
    INNER JOIN OMRC T1 ON T1."FirmCode" = T0."FirmCode"
    INNER JOIN "@PR_MARQUES_ASSOCIE" T2 ON T1."FirmName" = T2."U_MARQUES"
    WHERE T2."U_MARQUES" = 'ROCHAS' AND T2."Code" = '01' 
      AND ITM1."PriceList" = '2' AND T1."FirmName" = 'ROCHAS';

This is the error I get:
> Getting fetch returns more than requested number of rows error

Actually my goal is to update the column ITM1."Price" where ITM1."PriceList" = '2' with the price Value where the "PriceList" = '1' * factor

For example:

ITM1."Price" and "PriceList" = '1' = 40 $ 
ITM1."Factor" = T2."U_COEFFICIENT" = 4

Then

ITM1."Price" and "PriceList" = '2' = 160 $ 

I tried the above query (in the body) and I think it's not correct.

UPDATE :
The message returned is the one in the title : 'Getting fetch returns more than requested number of rows error' .
The lookup need to take account the item from ITM1 .
It should take the price list = '1' which represent the 'purchase price' and multiplie with the factor .
"U_COEFFICIENT" is used in another table to manipulate and update the column "Factor" from table ITM1

Exemple in Excel Format
Exemple in Excel Format 2

答案1

得分: 0

以下是翻译好的部分:

错误消息是由以下语句引起的:

SELECT T1."Price" INTO pght FROM ITM1 T1 WHERE T1."PriceList" = '1';

与表格 ITM1 中的数据组合在一起。
SELECT INTO 需要由 SELECT 语句返回的标量 - 即单个值。如果有更多(或根本没有值),则会返回错误消息。

为避免此错误,有几个选项:

  • 使筛选条件非常具体,以仅返回单个记录。
  • 使用聚合,例如 MAX(T1."Price") 仅返回单个值。
  • 确保表格中的数据不对筛选条件具有多个记录。

根据列名和表名,我猜测此查询的目的是使用 "Pricelist"=1 中的相同项目的值乘以某个因子来更新表 ITM1"Pricelist"=2 的项目的价格。如果确实如此,关联子查询或联接将是可能的解决方法之一。

以下是外部连接的示例:

UPDATE "ITM1"
    SET ITM1."Factor" = T2."U_COEFFICIENT" 
      , ITM1."Price" = T2."U_COEFFICIENT" * COALESCE(ITM_P1."Price", 1.0)
    FROM ITM1
    LEFT OUTER JOIN ITM1 ITM_P1
       ON ITM1."ItemCode" = ITM_P1."ItemCode"
       AND ITM1."PriceList" = '2'
       AND ITM_P1."PriceList" = '1'
    INNER JOIN OITM T0 
       ON T0."ItemCode" = ITM1."ItemCode"
    INNER JOIN OMRC T1 
       ON T1."FirmCode" = T0."FirmCode"
    INNER JOIN "@PR_MARQUES_ASSOCIE" T2 
       ON T1."FirmName" = T2."U_MARQUES"
    WHERE 
       T2."U_MARQUES" = 'ROCHAS' 
       AND T2."Code" = '01' 
       AND ITM1."PriceList" = '2' 
       AND T1."FirmName" = 'ROCHAS';

注意如何将 ITM1 与自身进行外部连接 - 外部连接允许处理那些没有 "PriceList"=1 记录的项目的情况。对于这些不匹配的情况,将返回 NULL,并且 COALESCE() 函数确保在这些情况下,项目价格将乘以 1.0。

英文:

The error message is caused by this statement:

SELECT T1."Price" INTO pght FROM ITM1 T1 WHERE T1."PriceList" = '1';

in combination with the data in table ITM1.
There are more than a single record in ITM1 that match the condition.

SELECT INTO requires a scalar - that is a single value - to be returned by the SELECT statement. If there are more (or no values at all) an error message is returned.

To avoid this error, there are a few options:

  • make the filter conditions so specific that only a single record gets returned.
  • use an aggregation, e.g. MAX(T1."Price") to return only a single value.
  • make sure that the data in the table does not have multiple records for the filter condition.

Based on the column and table names I am guessing that the purpose for this query is to update the price of items in table ITM1 with "Pricelist"=2 with the value for the same item with "Pricelist=1" multiplied by some factor.
If that is indeed the case, a correlated subquery or a join would be a possible solution approaches.

Here is an example of how an outer join could work:

UPDATE "ITM1"
    SET ITM1."Factor" = T2."U_COEFFICIENT" 
      , ITM1."Price" = T2."U_COEFFICIENT" * COALESCE(ITM_P1."Price", 1.0)
    FROM ITM1
    LEFT OUTER JOIN ITM1 ITM_P1
       ON ITM1."ItemCode" = ITM_P1."ItemCode"
       AND ITM1."PriceList" = '2'
       AND ITM_P1."PriceList" = '1'
    INNER JOIN OITM T0 
       ON T0."ItemCode" = ITM1."ItemCode"
    INNER JOIN OMRC T1 
       ON T1."FirmCode" = T0."FirmCode"
    INNER JOIN "@PR_MARQUES_ASSOCIE" T2 
       ON T1."FirmName" = T2."U_MARQUES"
    WHERE 
       T2."U_MARQUES" = 'ROCHAS' 
       AND T2."Code" = '01' 
       AND ITM1."PriceList" = '2' 
       AND T1."FirmName" = 'ROCHAS';

Note how ITM1 is outer joined with itself - an outer join to allow for cases, where an item does not have a record with "PriceList"=1. For those non-matches a NULL will be returned and the COALESCE() function ensures that in those cases, the item price will be multiplied with 1.0.

huangapple
  • 本文由 发表于 2023年6月12日 07:37:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定