Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic Field - Exception

Former Member
0 Kudos
178

Hi,

I need to raise exception in a select statement. Is it possible,

I need to check whether a particular field is present in a table rather than a value. If not, need to raise error,.

select single (P_stat)

from K810008

into (p_Stat)

where gjahr = p_year and

perde = p_period.

if sy-subrc <> 0.

MESSAGE e000 WITH text-s05.

endif.

The following gives a dump cos the field name is given wrongly, i cant capture it.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
120

Hi Krish,

Rather than catching the error after receiving it, you should look before your query in table DD03L if the field exists in this table!

Example of code :

SELECT * FROM dd03l
  WHERE tabname = 'K810008' AND
               fieldname = p_stat.
IF sy-subrc = 0.
SELECT SINGLE (p_stat)
  FROM K810008
  INTO (p_stat)
  WHERE gjahr = p_year AND
        perde = p_period.
ENDIF.

Best regards!

Samuel

5 REPLIES 5

Former Member
0 Kudos
120

Hi,

Try this:

Make sure you pass the relevant field name in CAPS to P_STAT before the select query

select single (P_stat)

from K810008

into LV_STAT " LV_STAT is a variable of type ANY

where gjahr = p_year and

perde = p_period.

if sy-subrc 0.

MESSAGE e000 WITH text-s05.

endif

Hope this helps

Regards

Shiva

Former Member
0 Kudos
120

Hi,

After you get a dump goto transaction ST22 and find the class exception of the dump you are getting.

For example

if the class exception is cx_division_by_zero.

Then code like the below,


data: error type ref to cx_division_by_zero.

try.

select single (P_stat)
from K810008
into (p_Stat)
where gjahr = p_year and
perde = p_period.

catch cx_division_by_zero into error.
endtry.

Now the reference variable error will hold the dump error which can be displayed.

Use the relevant class exception instead of cx_division_by_zero.

Regards,

Vikranth

Former Member
0 Kudos
121

Hi Krish,

Rather than catching the error after receiving it, you should look before your query in table DD03L if the field exists in this table!

Example of code :

SELECT * FROM dd03l
  WHERE tabname = 'K810008' AND
               fieldname = p_stat.
IF sy-subrc = 0.
SELECT SINGLE (p_stat)
  FROM K810008
  INTO (p_stat)
  WHERE gjahr = p_year AND
        perde = p_period.
ENDIF.

Best regards!

Samuel

Former Member
0 Kudos
120

SELECT * FROM dd03l
  WHERE tabname = 'K810008' AND
               fieldname = p_stat.

This would work only if p_stat is a single field either valid or invalid. But since its dynamic and if you are concatenating two fields, even if both fields are valid, the above select would fail.

Former Member
0 Kudos
120

Hi,

we can catch the exception with the following code:

TRY.

select single (P_stat)

from K810008

into (p_Stat)

where gjahr = p_year and

perde = p_period.

RAISE EXCEPTION TYPE CX_SY_DYNAMIC_OSQL_SEMANTICS.

CATCH CX_SY_DYNAMIC_OSQL_SEMANTICS.

WRITE:/5 'Exception caught'.

ENDTRY.

Regards,

Ashok