GUID(Globally Unique Identifier)全局唯一标识符,其广泛应用于各种程序中,在一些场景下,我们经常使用GUID作为主键。那么,在ABAP中,生成GUID的方式有哪些呢?
1. GUID做主键
使用GUID做主键时,其data type应该是RAW(16)类型,常用的data element 是GUID。
2. S/4版本上生成GUID
在高版本的Netweaver上,ABAP中的系统class cl_uuid_factory提供了生成各种格式的GUID的算法,包含GUID的生成,以及不同GUID格式间的转换。其用法如下:
DATA lo_uuid TYPE REF TO if_system_uuid.
DATA lv_uuid_x16 TYPE sysuuid_x16. " raw(16) -> data element GUID
DATA lv_uuid_c22 TYPE sysuuid_c22.
DATA lv_uuid_c32 TYPE sysuuid_c32.
DATA lv_uuid_c26 TYPE sysuuid_c26.
TRY.
lo_uuid = cl_uuid_factory=>create_system_uuid( ).
lv_uuid_x16 = lo_uuid->create_uuid_x16( ).
lo_uuid->convert_uuid_x16(
EXPORTING
uuid = lv_uuid_x16
IMPORTING
uuid_c22 = lv_uuid_c22
uuid_c32 = lv_uuid_c32
uuid_c26 = lv_uuid_c26 ).
CATCH cx_uuid_error. " Error Class for UUID Processing Errors
ENDTRY.
WRITE: / 'uuid_x16:', lv_uuid_x16.
WRITE: / 'uuid_c22:', lv_uuid_c22.
WRITE: / 'uuid_c32:', lv_uuid_c32.
WRITE: / 'uuid_c26:', lv_uuid_c26.
运行结果:
3. 低版本以及ECC上
若当前ABAP的系统中,没有cl_uuid_factory, 则可以使用其他两种方式替代:
- 调用class cl_system_uuid的静态方法create_uuid_x16_static( )
- 调用函数GUID_CREATE
示例代码如下:
DATA lv_guid1 TYPE sysuuid_x16.
DATA lv_guid2 TYPE sysuuid_x16.
DATA lv_guid3 TYPE sysuuid_x16.
" option 1
lv_guid1 = cl_uuid_factory=>create_system_uuid( )->create_uuid_x16( ).
" option 2
lv_guid2 = cl_system_uuid=>create_uuid_x16_static( ).
" option 3 - ECC
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_16 = lv_guid3 .
* ev_guid_22 =
* ev_guid_32 =.
本博客专注于技术分享,干货满满,持续更新。 欢迎关注❤️、点赞👍、转发📣!