14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Top-Down Development 15.2

To recap, you’re going to define the name of each view field and how it is to be

filled together, one at a time. Instead of listing all the fields after the SELECT statement

as you would do in an SQL query, the list of fields and how to fill them

comes in the body of the code, after the SELECT and JOIN criteria but before the

WHERE clause. This list of fields is enclosed in curly-wurly brackets ({ }), as shown

in Listing 15.3.

{

Key monster_header.monster_number AS monster_number,

Monster_header.Monster_name AS monster_name

LPAD( monster_header.hat_size, 10, "0" ) AS hat_size

SUBSTRING( monster_header.monster_name, 0, 1 ) AS first_initial

}

Listing 15.3 List of Fields for the CDS View to Get from the Database

Once again, you are required to declare an alias every time (e.g., AS monster_

number). The target field has no explicit type declaration; it is typed according to

the value that is being passed into it. You will notice in Listing 15.3 that there are

some built-in functions: for when you want to return a hat size of 3 as

0000000003 and return the first initial of a monster called Fred as “F”. There are

bucket loads of such functions availabl e, and more creep in with each support

stack.

Back in Chapter 2, you saw that CASE statements had crept into OpenSQL statements

in ABAP, and much as you might shout “Get back! Get back! Get back to

where you once belonged!” at them, it’s a fact of life in ABAP 7.4 that CASE statements

go where they please when they please (swaggering, arrogant bullies that

they are). Here they can be used inside a CDS view, as demonstrated in Listing

15.4. You will note they can also be nested.

CASE monster_header.sanity_description

WHEN 'BONKERS' THEN 'REALLY SCARY'

WHEN 'MAD' THEN

CASE monster_header.strength_description

WHEN 'REALLY STRONG' THEN 'SCARY'

ELSE 'NOT SO SCARY REALLY'

END

WHEN 'SLIGHTLY MAD' THEN 'SLIGHTLY SCARY'

ELSE 'NOT REALLY SCARY AT ALL'

END AS scariness

}

Listing 15.4 CASE Statement within a CDS View

665

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!