0

I need to select a field from knb1 where kunnr from knb1 is equal to kunnr in likp and assign it to field KART_KLIENT1. For a reason I can't warp my head around, it says that neither table knb1 nor likp have a column kunnr... Which both of them do.

I don't know where to look for a problem, both knb1 and likp are predefined database tables in SAP and kunnr column is there by default.

I am working in SQ02, adding the code to one of the fields.

  • If I add knb1 and lipk to TABLES section, there's an error saying they are already defined.
  • Trying to define kunnr in DATA section (TYPE or LIKE) doesn't change anything.
  • Using '~' instead of '-' in WHERE part of the SELECT doesn't change anything.

Thank you for your time.

TYPES: BEGIN OF ty_knb,
  tlfns TYPE knb1,
  END OF ty_knb.

DATA: wa_knb TYPE ty_knb.

SELECT SINGLE TLFNS
  INTO wa_knb
  FROM knb1
  WHERE knb1-kunnr = likp-kunnr.

KART_KLIENT1 = wa_knb-tlfns.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Bartosz_76
  • 77
  • 1
  • 12
  • 1
    are you running your request on the right schema – DEV Jul 19 '21 at 10:04
  • What do you mean by "schema"? I am quite new to SAP. I am working on BOT environment currently (if that's relevant). – Bartosz_76 Jul 19 '21 at 10:18
  • 1
    You say "section" concerning `TABLES` and `DATA` but I guess you mean "ABAP Keyword". According to your code, it seems that your infoset is querying the table `LIKP` primarily, in that case your code should not do a syntax error about `likp-kunnr` (but it's an error to have `knb1-kunnr` at the left of `=`, it should be either `knb1~kunnr` or `kunnr`). Please clarify which table(s) your infoset is querying primarily. Note that the codes of two Additional Fields share the same global scope, you cannot declare the same type or variable twice. – Sandra Rossi Jul 19 '21 at 17:51
  • I think that the table that is quered primarly is VTTK... I am trying to add LIKP or KNB1 to queried tables too (because the whole idea is to have WHERE knb1~kunnr = likp-kunnr), but it won't let me. I am trying to add "additional table" next to "additional fields", but when I am specyfing that KNB1 should have WHERE KUNNR = LIKP-KUNNR, it throws an error that it's "not declared" anywhere in the code and I am lost. If I try to declare it, it throws an error... because "it's already declared." – Bartosz_76 Jul 19 '21 at 18:25
  • 1
    You must give an order to each of your codes, that you can see in the Extras tab. Declare your variables in the right order, and only once. – Sandra Rossi Jul 19 '21 at 19:32
  • Thank you for all the time and input. I have added both KNB1 and LIKP in Extras Tab and now there are no errors, but the code still just doesn't work. :/ And debugger doesn't show anything, it appears to just be ignoring my Select statement and I can't see anything happening with the variables' values there. I added KNB1 with WHERE KUNNR = LIKP-KUNNR and BUKRS = 5000 (which, I believe, is correct), but adding LIKP wants me to join it with something by VBELN... I need it joined by KUNNR with KNB1, but I can't find the way to do that. So I did LIKP WHERE VBELN = LIKP-VBELN. : – Bartosz_76 Jul 20 '21 at 05:56

2 Answers2

2
TYPES: BEGIN OF ty_knb,
  tlfns TYPE knb1,
END OF ty_knb.

This creates a structure type with one field tlfns, but the type of that column is not one field but actually a whole line of the database table knb1. Or in other words, you create a structure nested within a structure, also known as a "deep structure". I doubt that this is what you want to do. It seems to me that what you actually want to do is to have a structure with a field named tlfns which is the same type as the column named tlfns of the database table knb1.

In that case, you would write it like that:

tlfns TYPE knb1-tlfns,

Regarding your select statement, try this:

WHERE kunnr = likp-kunnr.

You only need to state the table in a WHEREcondition if it's ambiguous (for example when you JOIN multiple tables). But in that case you would write it like this:

WHERE knb1~kunnr = likp-kunnr.

(assuming likp is another structure with a field kunnr containing a valid customer number).

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • Thanks for taking the time and looking at my problem, I edited that part and thank you for the explanation. Unfortunately though, it still says it has no idea what knb1-kunnr is. :-/ – Bartosz_76 Jul 19 '21 at 12:12
  • 1
    @Bartosz_76 OK, there is another mistake in your SELECT. I updated the answer. – Philipp Jul 19 '21 at 12:14
  • Thanks again. Although now it says that it has no clue what LIKP is at all. So I add the "TABLES: likp." at the top of my code. Then, when I run it, it throws an error saying LIKP is already decalred somewhere else... any ideas where should I look for its declaration? Or why, since it's supposedely already declared, it doesn't recognise it?... And doesn't let me declare it again here? – Bartosz_76 Jul 19 '21 at 12:20
  • 1
    @Bartosz_76 As I wrote: "assuming `likp` is another structure with a field `kunnr` containing a valid customer number". I am not sure what data you expect to get. But to get you going you could replace that with a string literal of a known customer number. Like `WHERE kunnr = '123456'. `. Replace the number with the number of an existing customer you can find and view in transaction FD02. – Philipp Jul 19 '21 at 13:07
  • LIKP is a table with delivery data, I believe. The whole point is that KUNNR should be the link between KNB1 and LIKP. When I take a random KUNNR from KNB1 and pass it as a string literal, it shows me the data. So I am doing something wrong while trying to connect KNB1 and LIKP. The idea is to get phone, fax and internet of a person from KNB1 where its KUNNR is the same as KUNNR in LIKP. I found the declared LIKP in another field's coding box (there's kind of a mess here) and added the code there, but it still just doesn't return any data. – Bartosz_76 Jul 19 '21 at 13:30
0

So all of a sudden I stumbled upon a solution. Within the entire query the code for many fields is put inside a few of them and I had to move from one to another... I am not sure why exactly my solution works since I am new to ABAP, but my guess is, that it had to have something to do with the ordering of the fields. I am not certain though, as previously, I was moving the entire code from that field to another one, where the majority of the code is located in order to have all the table declarations in a single place. So it's either an inner thing of SAP I don't know about yet or there was some sort of a relation between different sections of the code that required my code to be written in that exact field with its exact place in the ordering…?

Bartosz_76
  • 77
  • 1
  • 12