Advertising:

Switch(ABAP keyword): Difference between revisions

From SAP Knowledge Base
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{#description2:An explanation of how the ABAP keyword SWITCH works using an example. An example with CASE is also shown to show the advantages of conditional operator SWITCH.}}
{{Template:MetaDesc|An explanation of how the ABAP keyword SWITCH works using an example. An example with CASE is also shown to show the advantages of conditional operator SWITCH.}}
[[category:keywords]]
[[category:keywords]]
Since ABAP 7.40 you can use SWITCH. In terms of syntax, this is like [[COND]] with the only difference being that it checks for equality and no condition like COND is possible.
Since ABAP 7.40 you can use SWITCH. In terms of syntax, this is like [[COND]] with the only difference being that it checks for equality and no condition like COND is possible.


=== Mit SWITCH ===
== With SWITCH ==
<syntaxhighlight lang="abap" line start="1">
<syntaxhighlight lang="abap" line start="1">
  DATA(lang) = SWITCH char02( sy-langu WHEN 'E' THEN 'EN'
  DATA(lang) = SWITCH char02( sy-langu WHEN 'E' THEN 'EN'
Line 12: Line 12:
Since "lang" is declared inline, the type char02 must be specified.
Since "lang" is declared inline, the type char02 must be specified.


=== With CASE ===
== With CASE ==
<syntaxhighlight lang="abap" line start="1">
<syntaxhighlight lang="abap" line start="1">
  DATA lang TYPE char02.
  DATA lang TYPE char02.

Latest revision as of 17:03, 31 December 2024

Since ABAP 7.40 you can use SWITCH. In terms of syntax, this is like COND with the only difference being that it checks for equality and no condition like COND is possible.

With SWITCH

 DATA(lang) = SWITCH char02( sy-langu WHEN 'E' THEN 'EN'
                                      WHEN 'D' THEN 'DE'
                                      ELSE THROW cx_... "optional
                             ).

Since "lang" is declared inline, the type char02 must be specified.

With CASE

 DATA lang TYPE char02.
 CASE sy-langu.
  WHEN 'E'.
   lang = 'EN'.
  WHEN 'D'.
   lang = 'DE'.
  WHEN OTHERS.
   RAISE EXCEPTION TYPE cx_... .
 ENDCASE.

This is a wiki created in the spare time of a private person working in the SAP ERP area. The aim is to collect knowledge for the own use. The wiki is maintained to the best of knowledge and belief.
All products shown, including in form of screenshots, belong to SAP SE. Their trademarks are, among others: SAP®,ABAP®,SAP Fiori®,SAP HANA®,SAP NetWeaver®,SAP® R/3®,SAP S/4HANA®,SAP S/4HANA® Cloud