简体   繁体   English

Select 来自 2 个表的单个值

[英]Select single value from 2 tables

Im newbie at Sap/ABAP programming so i have question now I have 2 tables have same value, i need to do is check first table for a value if the table dont have the value check the second one and get the value there,我是 Sap/ABAP 编程的新手,所以我现在有疑问,我有 2 个表具有相同的值,我需要做的是检查第一个表的值,如果表没有值,检查第二个表并在那里获取值,

i have exprience in c and java a little but ABAP kind of different some how.我在 c 和 java 有一点经验,但 ABAP 有点不同。 And my code look like this.我的代码看起来像这样。

IF NOT ATEXT is  initial. 
    SELECT SINGLE ATEXT FROM tablea WHERE aname EQ .....
    INTO DATA(a).
ELSE
   SELECT SINGLE BTEXT FROM  tableb  WHERE aname EQ .....
    INTO DATA(a).
ENDIF

1)can we do this with just joins or unıons? 1)我们可以只用连接或联合来做到这一点吗?

2)or after first select single if there is no value return a if sy-subrc=4 and get another single select work out? 2) 或者在第一个 select single 如果没有值返回 a if sy-subrc=4 并得到另一个 single select 算出来? i just think this solutions but cant figure out how...我只是想这个解决方案,但无法弄清楚如何......

Thanks for the answers.感谢您的回答。 Best Regards.最好的祝福。

You already know the correct way.你已经知道正确的方法了。

You need first to select from tablea .您首先需要从tablea到 select 。 After, you check if sy-subrc = 4 (row not found) then it means that you need to select from tableb.之后,您检查是否 sy-subrc = 4(未找到行),这意味着您需要从 tableb 获取 select。

Basically your code would look like this:基本上你的代码看起来像这样:

SELECT SINGLE ATEXT FROM tablea WHERE aname EQ .....INTO DATA(a).
IF sy-subrc = 4. "Row not found
   SELECT SINGLE BTEXT FROM  tableb  WHERE aname EQ .....INTO DATA(a).
ELSE.
  "Value found in tablea
ENDIF.

Alternatively you can check if sy-subrc = 0 after first select (meaning row was found), else you select from tableb .或者,您可以在第一个 select (表示找到行)之后检查 sy-subrc = 0 ,否则您可以从 tableb 中检查 select

Ideally you also would check the sy-subrc after select on tableb (but this depends on the logic that consumes this data).理想情况下,您还可以在 tableb 上的 select 之后检查 sy- subrc (但这取决于使用此数据的逻辑)。

Overall, there are several ways to structure the code in this case (you can check for sy-subrc = 0, sy-subrc <> 0, sy-subrc = 4, check if the variable a is INITIAL or not after the select), so you will need to choose one.总的来说,这种情况下代码的结构有几种方式(可以检查 sy-subrc = 0, sy-subrc <> 0, sy-subrc = 4,在select之后检查变量a是否为INITIAL) ,所以你需要选择一个。

Also don't forget to test sy-subrc directly after the SELECT (don't execute any other code in between since other statements can also set the value of the sy-subrc).另外不要忘记在 SELECT 之后直接测试 sy-subrc(不要在其间执行任何其他代码,因为其他语句也可以设置 sy-subrc 的值)。

You almost got it.你几乎明白了。

  1. Select the 1st table whether it got the value specified Select 第一张表是否得到指定值

SELECT SINGLE ATEXT FROM tablea WHERE aname EQ..... INTO DATA(a).

  1. The next approach can be either check if the select successfully return row of data (by subrc) or just simply check the internal table tablea itself if it contains value after the SELECT query.下一个方法可以是检查 select 是否成功返回数据行(通过 subrc),或者只是检查内部表 tablea 本身是否包含 SELECT 查询后的值。

Either with IF sy-subrc NE 0 or IF tablea IS INITIAL使用IF sy-subrc NE 0IF tablea IS INITIAL

The second command INITIAL means the internal table empty第二条命令INITIAL表示内表为空

  1. Select the second table Select 第二张表

SELECT SINGLE BTEXT FROM tableb WHERE aname EQ..... INTO DATA(a).

So overall the code looks something like this所以总的来说代码看起来像这样

SELECT SINGLE ATEXT FROM tablea WHERE aname EQ .....
INTO DATA(a).

IF tablea IS INITIAL. "if tablea is empty
    SELECT SINGLE BTEXT FROM  tableb  WHERE aname EQ .....
    INTO DATA(a).
ENDIF.

or或者

SELECT SINGLE ATEXT FROM tablea WHERE aname EQ .....
INTO DATA(a).

IF sy-subrc NE 0. "Apart from 0 means previous command not executed properly
    SELECT SINGLE BTEXT FROM  tableb  WHERE aname EQ .....
    INTO DATA(a).
ENDIF.

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

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