Application Development and Automation 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: 
Read only

Dynamic insert statement

Former Member
0 Likes
786

Hi all

I had problem in insert statement.

I know the column of the table, tablename and the value to be insert.

My code:

Data: column(20) type c. -


>refer to the tablecolumn

Data: tablename(20) type c. -


>refer to table name

Data: value(20) type c. -


>value to be insert to the column in the table

column = 'Admino'.

tablename = 'Student'.

value = '123456'.

<b>insert <column> into <tablename> values <value>.</b>

i know there something wrong with my insert statement. How to do it in abap insert code where i can put in all the parameter in the insert statement.

Anycode or sample to show. Many thank.

3 REPLIES 3
Read only

Former Member
0 Likes
590

try this example:

=====================================

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

  • Build fieldcat

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SYST'

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

MOVE-CORRESPONDING is_fcat TO is_fieldcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

ENDLOOP.

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

  • Create a new Line with the same structure of the table.

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

  • Test it...

DO 30 TIMES.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

<l_field> = sy-index.

INSERT <l_line> INTO TABLE <l_table>.

ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

WRITE <l_field>.

ENDLOOP.

Please reward points if helpful.

Read only

0 Likes
590

From f1 help....

EXTRACT

Basic form

EXTRACT fg.

Effect

Writes all fields of the field group fg (FIELD-GROUPS) as an entry in asequential dataset. If you have defined a field group HEADER,its fields precede each entry as a sort key. Afterwards, you canuse SORT and LOOP ... ENDLOOP to sort or process the datasetrespectively. No further EXTRACT statements are possible afterthis.

Notes

General:

As soon as you have extracted a dataset using EXTRACT, you canno longer extend the field group using INSERT. In particular, you cannot change the HEADERfield group at all after the first EXTRACT (regardless of thefield group to which it applied).

Large extract datasets are not stored in main memory. Instead, theyare kept in an external auxiliary file. You can set the directory inwhich this file is created using the SAP profile parameterDIR_EXTRACT. The default directory is the SAP data directory(SAP profile parameter DIR_DATA).

Notes

Runtime errors:

EXTRACT_AFTER_SORT/LOOP: EXTRACT after SORT, orLOOP. EXTRACT_BUFFER_NO_ROLL: Unable to create the required main

EXTRACT_FIELD_TOO_LARGE: Occupied length of a field is toolarge.

EXTRACT_HEADER_NOT_UNIQUE: Field group HEADER wasmodified after an EXTRACT statement.

EXTRACT_OPEN_EXTRACTFILE_OPEN:

Error opening the external extract dataset file.

EXTRACT_RESOURCEHANDLER_FAILED: Error deleting the externalextract dataset file.

EXTRACT_TOO_LARGE: Total length of the entry for extraction(including HEADER fields) is too large.

Additional help

Filling anExtract with Data

Extracts

Since internal tables have fixed line structures, they are not suited to handle data sets with varying structures. Instead, you can use extract datasets for this purpose.

An extract is a sequential dataset in the memory area of the program. You can only address the entries in the dataset within a special loop. The index or key access permitted with internal tables is not allowed. You may only create one extract in any ABAP program. The size of an extract dataset is, in principle, unlimited. Extracts larger than 500KB are stored in operating system files. The practical size of an extract is up to 2GB, as long as there is enough space in the filesystem.

An extract dataset consists of a sequence of records of a pre-defined structure. However, the structure need not be identical for all records. In one extract dataset, you can store records of different length and structure one after the other. You need not create an individual dataset for each different structure you want to store. This fact reduces the maintenance effort considerably.

In contrast to internal tables, the system partly compresses extract datasets when storing them. This reduces the storage space required. In addition, you need not specify the structure of an extract dataset at the beginning of the program, but you can determine it dynamically during the flow of the program.

You can use control level processing with extracts just as you can with internal tables. The internal administration for extract datasets is optimized so that it is quicker to use an extract for control level processing than an internal table.

Procedure for creating an extract:

Define the record types that you want to use in your extract by declaring them as field groups. The structure is defined by including fields in each field group.

Defining an Extract

Fill the extract line by line by extracting the required data.

Filling an Extract with Data

Once you have filled the extract, you can sort it and process it in a loop. At this stage, you can no longer change the contents of the extract.

Processing Extracts

INSERT Statement

The INSERT statement is used to insert values into a single database table.

<insert statement> ::= INSERT INTO <table name> <insert column list> <insert source>.

<insert source> ::= VALUES '(' <value> ( ',' <value> )* ')'

| <query specification>.

<value> ::= <value expression>

| <dynamic parameter specification>

| NULL.

<insert column list> ::= '(' <column name> ( ',' <column name> )* ')'.

In Open SQL the <insert column list> is not optional.

You cannot specify string literals as values for CLOB columns. Hex literals are not supported in Open SQL.

Examples

INSERT INTO employees (employee_id, employee_name)

VALUES (4711, 'John Smith')

Inserting Values. A new row is inserted into the table employees with the values 4711 and 'John Smith' for the columns employee_id and employee_name respectively.

INSERT INTO well_paid_employees (employee_id, salary)

SELECT employee_id, salary

FROM employees

WHERE salary > ?

Inserting the Result of a Query. The employee_idand the salaryof all employees from table employeeswith a salary exceeding a certain value are inserted into the table well_paid_employees.

Please reward points if helpful.

Read only

0 Likes
590

Hi. i run your code. Where is your data insert to which table?