写一个abap能查询学生学号姓名成绩的查询,带ALV和有增删改查功能
时间: 2024-05-27 11:12:14 浏览: 196
以下是一个简单的ABAP代码示例,可以查询学生学号、姓名和成绩,并带有ALV和增删改查功能:
REPORT z_student_scores.
TABLES: zscores.
*定义选择屏幕
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS:
p_student_id TYPE zscores-student_id,
p_student_name TYPE zscores-student_name.
SELECTION-SCREEN END OF BLOCK b1.
*定义ALV对象
DATA: it_fieldcat TYPE lvc_t_fcat,
it_scores TYPE STANDARD TABLE OF zscores,
wa_scores TYPE zscores,
it_display TYPE lvc_t_datab,
wa_display TYPE lvc_s_datab.
*定义标志变量
DATA: lv_mode TYPE c,
lv_index TYPE i.
*初始化ALV对象
INITIALIZATION.
PERFORM init_alv.
*开始选择
START-OF-SELECTION.
PERFORM select_scores.
*&---------------------------------------------------------------------*
*& Form init_alv
*&---------------------------------------------------------------------*
FORM init_alv.
CLEAR it_fieldcat.
CLEAR wa_display.
*设置表格列
REFRESH it_fieldcat.
wa_display-fieldname = 'STUDENT_ID'.
wa_display-seltext_m = '学生学号'.
APPEND wa_display TO it_fieldcat.
CLEAR wa_display.
wa_display-fieldname = 'STUDENT_NAME'.
wa_display-seltext_m = '学生姓名'.
APPEND wa_display TO it_fieldcat.
CLEAR wa_display.
wa_display-fieldname = 'SCORE'.
wa_display-seltext_m = '成绩'.
APPEND wa_display TO it_fieldcat.
*初始化ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_display.
ENDFORM. " init_alv
*&---------------------------------------------------------------------*
*& Form select_scores
*&---------------------------------------------------------------------*
FORM select_scores.
CLEAR it_scores.
*查询数据
SELECT * FROM zscores
INTO TABLE it_scores
WHERE student_id = p_student_id
AND student_name = p_student_name.
*显示数据
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
TABLES
t_outtab = it_scores.
PERFORM process_user_command.
ENDFORM. " select_scores
*&---------------------------------------------------------------------*
*& Form process_user_command
*&---------------------------------------------------------------------*
FORM process_user_command.
DATA: lv_okcode TYPE sy-ucomm.
*等待用户命令
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_name = sy-repid
IMPORTING
e_okcode = lv_okcode.
*根据用户命令执行操作
CASE lv_okcode.
WHEN 'ADD'.
PERFORM add_score.
WHEN 'EDIT'.
PERFORM edit_score.
WHEN 'DELETE'.
PERFORM delete_score.
WHEN 'BACK'.
EXIT.
ENDCASE.
ENDFORM. " process_user_command
*&---------------------------------------------------------------------*
*& Form add_score
*&---------------------------------------------------------------------*
FORM add_score.
DATA: wa_new_score TYPE zscores.
*提示用户输入新数据
CLEAR wa_new_score.
wa_new_score-student_id = p_student_id.
wa_new_score-student_name = p_student_name.
WRITE:/ '请输入新成绩:'.
READ wa_new_score-score.
*将新数据添加到表格中
INSERT zscores FROM wa_new_score.
COMMIT WORK.
ENDFORM. " add_score
*&---------------------------------------------------------------------*
*& Form edit_score
*&---------------------------------------------------------------------*
FORM edit_score.
DATA: lv_selected_index TYPE i.
*获取用户选择的行
CALL FUNCTION 'GET_GRID_SELECTED_ROWS'
EXPORTING
i_grid_name = 'ALV'
IMPORTING
e_index = lv_selected_index.
*如果用户没有选择任何行,则退出
IF lv_selected_index IS INITIAL.
RETURN.
ENDIF.
*获取用户输入的新数据
CLEAR wa_scores.
READ TABLE it_scores INTO wa_scores INDEX lv_selected_index.
WRITE:/ '原成绩:', wa_scores-score.
WRITE:/ '请输入新成绩:'.
READ wa_scores-score.
*将新数据更新到表格中
MODIFY zscores FROM wa_scores.
COMMIT WORK.
ENDFORM. " edit_score
*&---------------------------------------------------------------------*
*& Form delete_score
*&---------------------------------------------------------------------*
FORM delete_score.
DATA: lv_selected_index TYPE i.
*获取用户选择的行
CALL FUNCTION 'GET_GRID_SELECTED_ROWS'
EXPORTING
i_grid_name = 'ALV'
IMPORTING
e_index = lv_selected_index.
*如果用户没有选择任何行,则退出
IF lv_selected_index IS INITIAL.
RETURN.
ENDIF.
*从表格中删除用户选择的行
DELETE zscores INDEX lv_selected_index.
COMMIT WORK.
ENDFORM. " delete_score
*结束程序
END-OF-PROGRAM.
阅读全文