2009 Sep 14 1:46 PM
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.
2009 Sep 14 2:07 PM
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
2009 Sep 14 1:56 PM
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
2009 Sep 14 1:56 PM
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
2009 Sep 14 2:07 PM
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
2009 Sep 14 2:22 PM
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.
2009 Sep 14 2:24 PM
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